Pagini recente » Borderou de evaluare (job #1235087) | Cod sursa (job #857096) | Cod sursa (job #1950711) | Cod sursa (job #1067479) | Cod sursa (job #597491)
Cod sursa(job #597491)
#include <cstdio>
#include <vector>
using namespace std;
const int N = 200005;
int n, m, post[N], value[N], nr, ok, v[N];
vector<int> a[N], at[N];
inline int neg(int a){
if(a > n)
return a - n;
return a + n;
}
void dfs(int k) {
int i;
v[k] = 1;
for(i = 0; i < a[k].size(); ++i)
if(!v[a[k][i]])
dfs(a[k][i]);
post[++nr] = k;
}
void dfst(int k) {
int i;
v[k] = 0;
if(value[k])
ok = 0;
value[k] = 0; value[neg(k)] = 1;
for(i = 0 ; i < at[k].size(); ++i)
if(v[at[k][i]])
dfst(at[k][i]);
}
int main() {
freopen("andrei.in", "r", stdin);
freopen("andrei.out", "w", stdout);
int i, x, y, c;
scanf("%d %d", &n, &m);
for(i = 1; i <= m; ++i) {
scanf("%d %d %d", &x, &y, &c);
if(c == 0){
a[neg(x)].push_back(y);
at[y].push_back(neg(x));
a[neg(y)].push_back(x);
at[x].push_back(neg(y));
}
if(c == 1) {
a[x].push_back(neg(y));
at[neg(y)].push_back(x);
a[y].push_back(neg(x));
at[neg(x)].push_back(y);
}
if(c == 2) {
a[x].push_back(y);
at[y].push_back(x);
a[neg(y)].push_back(neg(x));
at[neg(x)].push_back(neg(y));
a[neg(y)].push_back(neg(x));
at[neg(x)].push_back(neg(y));
a[y].push_back(x);
at[x].push_back(y);
}
}
ok = 1;
for(i = 1; i <= 2 * n; ++i)
if(!v[i])
dfs(i);
for(i = nr; i >= 1; --i)
if(v[post[i]] && v[neg(post[i])])
dfst(post[i]);
for(i = 1;i <= n; ++i)
printf("%d ", value[i]);
printf("\n");
return 0;
}