Pagini recente » Cod sursa (job #51121) | Cod sursa (job #2911418) | Cod sursa (job #1658951) | Cod sursa (job #746235) | Cod sursa (job #613992)
Cod sursa(job #613992)
#include<stdio.h>
#include<vector>
#include<stack>
using namespace std;
#define NMAX 256
#define pb push_back
vector< int > G[NMAX],GT[NMAX];
stack< int > Q;
bool viz[NMAX],SOL[NMAX];
int N,M,ANS;
inline int not( int value )
{
return value<=N ? value+N : value-N;
}
inline void push_edge( int node1, int node2 )
{
G[node1].pb( node2 );
GT[node2].pb( node1 );
}
void DF1( int node )
{
viz[node]=1;
int i,v;
for(i=0; i<G[node].size(); ++i)
{
v=G[node][i];
if( !viz[v] )
DF1(v);
}
Q.push( node );
}
void DF2( int node )
{
viz[node]=0;
SOL[ not(node) ]=1;
SOL[node]=0;
int i,v;
for(i=0; i<GT[node].size(); ++i)
{
v=GT[node][i];
if( viz[v] )
DF2(v);
}
}
int main()
{
freopen("party.in","r",stdin);
freopen("party.out","w",stdout);
scanf("%d%d",&N,&M);
int i,a1,a2,a3;
for(i=1; i<=M; ++i)
{
scanf("%d%d%d",&a1,&a2,&a3);
switch( a3 )
{
case 0:
{
push_edge( not(a1), a2 );
push_edge( not(a2), a1 );
break;
}
case 1:
{
push_edge( not(a1), not(a2) );
push_edge( a2, a1 );
break;
}
case 2:
{
push_edge( not(a2), not(a1) );
push_edge( a1, a2 );
break;
}
case 3:
{
push_edge( a1, not(a2) );
push_edge( a2, not(a1) );
break;
}
}
}
for(i=1; i<=(N<<1); ++i)
{
if( !viz[i] )
DF1( i );
}
int node;
while( !Q.empty() )
{
node=Q.top();
Q.pop();
if( viz[node] && viz[ not(node) ] )
DF2( node );
}
for(i=1; i<=N; ++i)
ANS+=SOL[i];
printf("%d\n",ANS);
for(i=1; i<=N; ++i)
if( SOL[i] )
printf("%d\n",i);
return 0;
}