inlinevoidadd_edge(int x, int y, int p, int w) { if(id[x][w] && v[x][w] == y) { int q = id[x][w]; pre[p] = pre[q] = 0; id[x][w] = id[y][w] = 0; ans[p] = 1, ans[q] = dir[y][w]; returnvoid(); } bool flag = true; if(id[x][w]) { flag = false; int q = id[x][w]; id[x][w] = 0; pre[q] = tot; rev[q] = dir[x][w]; x = v[x][w]; } if(id[y][w]) { flag = false; int q = id[y][w]; id[y][w] = 0; pre[q] = tot; rev[q] = dir[y][w]^1; y = v[y][w]; } v[x][w] = y, v[y][w] = x; dir[x][w] = 1, dir[y][w] = 0; if(flag) id[x][w] = id[y][w] = p; else pre[p] = id[x][w] = id[y][w] = tot++; }
inlinevoidsolve(int st) { vis[st] = true; for(int x = st, w = 0; id[x][w]; w ^= 1) { ans[id[x][w]] = dir[x][w]; int y = v[x][w]; if(vis[y]) return; vis[x = y] = true; } for(int x = st, w = 1; id[x][w]; w ^= 1) { ans[id[x][w]] = dir[x][w]^1; int y = v[x][w]; vis[x = y] = true; } }
intmain() { cin >> n >> m; tot = m+1; for(int i = 1; i <= m; ++i) { int x, y, w; cin >> x >> y >> w; add_edge(x+1, y+1, i, w-1); } for(int i = 1; i <= n; ++i) if(!vis[i]) solve(i); for(int i = tot-1; i >= 1; --i) if(pre[i]) ans[i] = ans[pre[i]]^rev[i]; for(int i = 1; i <= m; ++i) putchar(ans[i] ? '0' : '1'); }