#include using namespace std; struct UnionFind { vector par; vector sz; UnionFind(int n=0){ if(n>0) initialize(n); } void initialize(int n){ par.resize(n); sz.assign(n, 1); for(int i=0; i sz[y]) swap(x, y); par[x] = y; sz[y] += sz[x]; return true; } bool same(int x, int y){ return find(x) == find(y); } int size(int x){ return sz[find(x)]; } }; struct SCC{ int N; vector used; vector vs; vector cmp; void dfs(int v, const vector G[]){ used[v] = true; for(auto u : G[v]){ if(!used[u]) dfs(u, G); } vs.push_back(v); } void rdfs(int v, int k, const vector>& rG){ used[v] = true; cmp[v] = k; for(auto u : rG[v]){ if(!used[u]) rdfs(u, k, rG); } } SCC(int N, const vector G[]):N(N){ vector> rG(N); for(int i=0; i=0; i--){ if(!used[vs[i]]) rdfs(vs[i], k++, rG); } } int dump_graph(const vector G[], vector edges2[]){ for(int i=0; i> N >> M; vector> es; bool loop = false; UnionFind uf(N); for(int i=0; i> a >> b >> c; a--; b--; if(c == 1){ if(!uf.unite(a, b)) loop = true; }else{ es.emplace_back(a, b); } } if(loop) fin(true); vector edges[100000]; for(auto [a, b] : es){ if(uf.same(a, b)) fin(true); edges[uf.find(a)].push_back(uf.find(b)); } SCC scc(N, edges); vector cmpnum(N); for(int i=0; i 1); return 0; }