Pagini recente » Cod sursa (job #2964173) | Cod sursa (job #2033406) | Cod sursa (job #2699310) | Cod sursa (job #1830904) | Cod sursa (job #1105559)
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
const int NMAX = 200010;
int N, M, X, Y, C, TopSort[NMAX], Val[NMAX];
bool Used[NMAX];
vector<int> G[NMAX], GT[NMAX];
int Neg(int X)
{
if (X <= N) return X + N;
else return X - N;
}
void DFP(int Node)
{
Used[Node] = 1;
for (vector<int> :: iterator it = G[Node].begin(); it != G[Node].end(); ++ it)
if (!Used[*it])
DFP(*it);
TopSort[ ++ TopSort[0]] = Node;
}
void DFM(int Node)
{
Val[ Neg(Node) ] = 1;
Used[Node] = 0;
for (vector<int> :: iterator it = GT[Node].begin(); it != GT[Node].end(); ++ it)
if (Used[*it])
DFM(*it);
}
void Add_Edges(int X, int Y)
{
G[Neg(X)].push_back(Y);
G[Neg(Y)].push_back(X);
GT[X].push_back(Neg(Y));
GT[Y].push_back(Neg(X));
}
int main()
{
freopen("andrei.in", "r", stdin);
freopen("andrei.out", "w", stdout);
scanf("%i %i", &N, &M);
for (int i = 1; i <= M; ++ i)
{
scanf("%i %i %i", &X, &Y, &C);
if (C == 0) Add_Edges( X, Y);
else if (C == 1) Add_Edges( Neg(X), Neg(Y) );
else
{
Add_Edges( X, Neg(Y) );
Add_Edges( Neg(X), Y );
}
}
for (int i = 1; i <= 2 * N; ++ i)
if (!Used[i])
DFP(i);
reverse(TopSort + 1, TopSort + TopSort[0] + 1);
for (int i = 1; i <= 2 * N; ++ i)
if (Used[ TopSort[i] ] && Used[ Neg(TopSort[i]) ])
DFM(TopSort[i]);
for (int i = 1; i <= N; ++ i) printf("%i ", Val[i]);
}