Pagini recente » Cod sursa (job #2378366) | Cod sursa (job #1822882) | Cod sursa (job #2666882) | Cod sursa (job #402526) | Cod sursa (job #1652099)
#include<fstream>
#include<vector>
#include<cstring>
using namespace std;
ifstream fin( "party.in" ); ofstream fout( "party.out" );
typedef vector< int > graf;
const int nmax = 100;
int n, nr_comp;
int c[ 2 * nmax + 1 ];
bool viz[ 2 * nmax + 1 ];
graf g[ 2 * nmax + 1 ][ 2 ];
vector< int > ord, ans;
void muchie( int x, int y ) {
g[ x ][ 0 ].push_back( y );
g[ y ][ 1 ].push_back( x );
}
void dfs( int nod, int ind ) {
viz[ nod ] = 1;
for( graf::iterator it = g[ nod ][ ind ].begin(); it != g[ nod ][ ind ].end(); ++ it ) {
if ( viz[ *it ] == 0 ) {
dfs( *it, ind );
}
}
if ( ind == 0 ) {
ord.push_back( nod );
} else {
c[ nod ] = nr_comp;
}
}
void plus_minus() {
for( int i = 1; i <= 2 * n; ++ i ) {
if ( viz[ i ] == 0 ) {
dfs( i, 0 );
}
}
memset( viz, 0, sizeof( viz ) );
nr_comp = 0;
for( int i = ord.size() - 1; i >= 0; -- i ) {
if ( viz[ ord[ i ] ] == 0 ) {
++ nr_comp;
dfs( ord[ i ], 1 );
}
}
}
int main() {
int m;
fin >> n >> m;
for( int i = 0; i < m; ++ i ) {
int x, y, z;
fin >> x >> y >> z;
if ( z == 0 ) {
muchie( x + n, y );
muchie( y + n, x );
} else if ( z == 1 ) {
muchie( x + n, y + n );
muchie( y, x + n );
} else if ( z == 2 ) {
muchie( y + n, x + n );
muchie( x, y + n );
} else {
muchie( x, y + n );
muchie( y, x + n );
}
}
plus_minus();
for( int i = 1; i <= n; ++ i ) {
if ( c[ i ] > c[ i + n ] ) {
ans.push_back( i );
}
}
fout << ( int )ans.size() << "\n";
for( int i = 0; i < ( int )ans.size(); ++ i ) {
fout << ans[ i ] << "\n";
}
fin.close();
fout.close();
return 0;
}