Pagini recente » Cod sursa (job #2489409) | Cod sursa (job #1185663) | Cod sursa (job #2230593) | Cod sursa (job #1955568) | Cod sursa (job #2593497)
#include <vector>
#include <fstream>
std::ifstream fin("andrei.in");
std::ofstream fout("andrei.out");
const int NMAX = 2e5 + 1;
int n, m;
std::vector<int> adG[NMAX], adT[NMAX];
int top, topo[NMAX];
bool vis[NMAX], ans[NMAX];
inline int non(int x) {
return (x > n ? x - n : x + n);
}
void addProp(int x, int y) {
if (x < 0) x = n - x;
if (y < 0) y = n - y;
adG[non(x)].push_back(y);
adT[y].push_back(non(x));
adG[non(y)].push_back(x);
adT[x].push_back(non(y));
}
void dfsG(int node) {
vis[node] = true;
for (int nghb : adG[node])
if (!vis[nghb])
dfsG(nghb);
topo[top++] = node;
}
void dfsT(int node) {
vis[node] = false;
ans[non(node)] = true;
for (int nghb : adT[node])
if (vis[nghb])
dfsT(nghb);
}
int main() {
fin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y, t; fin >> x >> y >> t;
if (t == 0)
addProp(+x, +y);
else if (t == 1)
addProp(-x, -y);
else {
addProp(-x, +y);
addProp(+x, -y);
}
}
for (int i = 1; i <= 2 * n; i++)
if (!vis[i])
dfsG(i);
for (int i = 2 * n - 1; i >= 0; i--)
if (vis[topo[i]] && vis[non(topo[i])])
dfsT(topo[i]);
for (int i = 1; i <= n; i++)
fout << ans[i] << ' ';
fout << '\n';
fout.close();
return 0;
}