Pagini recente » Cod sursa (job #2497668) | Cod sursa (job #2278966) | Cod sursa (job #2485635) | Cod sursa (job #2224003) | Cod sursa (job #1218990)
#include<stdio.h>
#include<string.h>
#include<vector>
#include<stack>
using namespace std;
const int NMAX = 2e5;
stack <int> st;
vector <int> graph[NMAX + 5], trans[NMAX + 5];
int n;
bool vis[NMAX + 5], val[NMAX + 5];
inline int neg (int x) {
return x > n ? x - n : x + n;
}
void sau (int x, int y) {
graph[neg(x)].push_back(y);
graph[neg(y)].push_back(x);
trans[y].push_back(neg(x));
trans[x].push_back(neg(y));
}
void dfsPlus (int node) {
vis[node] = 1;
for(vector <int>::iterator it = graph[node].begin(); it != graph[node].end(); ++ it)
if(!vis[*it])
dfsPlus(*it);
st.push(node);
}
void dfsMinus (int node) {
val[neg(node)] = 1;
vis[node] = vis[neg(node)] = 1;
for(vector <int>::iterator it = trans[node].begin(); it != trans[node].end(); ++ it)
if(!vis[*it])
dfsMinus(*it);
}
int main() {
freopen("andrei.in", "r", stdin);
freopen("andrei.out", "w", stdout);
int m, i, x, y, color;
scanf("%d%d", &n, &m);
for(i = 1; i <= m; ++ i) {
scanf("%d%d%d", &x, &y, &color);
if(color == 0)
sau(x, y);
if(color == 1)
sau(neg(x), neg(y));
if(color == 2)
sau(x, y),
sau(neg(x), neg(y));
}
for(i = 1; i <= n; ++ i)
if(!vis[i])
dfsPlus(i);
memset(vis, 0, sizeof(vis));
while(!st.empty()) {
if(vis[st.top()]) {
st.pop();
continue;
}
dfsMinus(st.top());
st.pop();
}
for(i = 1; i <= n; ++ i)
printf("%d ", val[i]);
return 0;
}