Pagini recente » Cod sursa (job #285141) | Cod sursa (job #2337786) | Cod sursa (job #1657880) | Cod sursa (job #2828248) | Cod sursa (job #2172133)
#include <memory.h>
#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#include <stack>
using namespace std;
ifstream in("andrei.in");
ofstream out("andrei.out");
const int NMAX = 2 * 2 * 1e5;
int n, m;
vector < int > g[1 + NMAX];
vector < int > gi[1 + NMAX];
bitset < 1 + NMAX > vis;
bitset < 1 + NMAX > val;
stack < int > st;
int nott(int x) {
if(x > n)
return x - n;
else
return x + n;
}
void addedge(int x, int y) {
g[x].push_back(y);
gi[y].push_back(x);
}
void dfs1(int node) {
vis[node] = 1;
for(int i = 0; i < g[node].size(); i++) {
int to = g[node][i];
if(vis[to] == 0)
dfs1(to);
}
st.push(node);
}
void dfs2(int node) {
vis[node] = 1;
val[nott(node)] = 1;
for(int i = 0; i < gi[node].size(); i++) {
int to = g[node][i];
if(vis[to] == 0)
dfs2(to);
}
}
void setup(int x, int y) {
addedge(nott(x), y);
addedge(nott(y), x);
}
int main()
{
in >> n >> m;
for(int i = 1; i <= m; i++) {
int x, y, col;
in >> x >> y >> col;
if(col == 0) {
setup(x, y);
} else if(col == 1) {
setup(nott(x), nott(y));
} else {
setup(x, nott(y));
setup(nott(x), y);
}
}
for(int i = 1; i <= 2 * n; i++)
if(vis[i] == 0)
dfs1(i);
vis = 0;
while(!st.empty()) {
int from = st.top();
st.pop();
if(vis[from] == 0 && vis[nott(from)] == 0)
dfs2(from);
}
for(int i = 1; i <= n; i++)
out << val[i] << ' ';
out << '\n';
in.close();
out.close();
return 0;
}