Pagini recente » Cod sursa (job #2878488) | Cod sursa (job #2883045) | Cod sursa (job #2334487) | Cod sursa (job #1867545) | Cod sursa (job #2593489)
#include <vector>
#include <fstream>
using namespace std;
ifstream fin("andrei.in");
ofstream fout("andrei.out");
// Începe să-mi placă mai mult CodeForces decât InfoArena.
// Acolo nu iei MLE pentru simplul fapt că folosești STL sau OOP...
const int NMAX = 2e5 + 1;
int n, m;
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);
reverse(topo, topo + 2 * n);
for (int node : topo)
if (vis[node] && vis[non(node)])
dfsT(node);
for (int i = 1; i <= n; i++)
fout << ans[i] << ' ';
fout << '\n';
fout.close();
return 0;
}