/*    ∫ ∫ ∫    ノヽ   (_  )  (_    ) (______ )  ヽ(´・ω・)ノ     |  /    UU */ #pragma region macro #include typedef long long int64; using namespace std; using P = pair; typedef vector vi; const int MOD = (int)1e9 + 7; const int64 INF = 1LL << 62; const int inf = 1<<30; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b ostream& operator<<(ostream& os, const vector &V){ int N = V.size(); REP(i,N){ os << V[i]; if (i!=N-1) os << " "; } os << "\n"; return os; } template ostream& operator<<(ostream& os, pair const&P){ os << "("; os << P.first; os << " , "; os << P.second; os << ")"; return os; } template ostream& operator<<(ostream& os, set &S){ auto it=S.begin(); while(it!=S.end()){ os << *it; os << " "; it++; } os << "\n"; return os; } template ostream& operator<<(ostream& os, deque &q){ for(auto it=q.begin();it ostream& operator<<(ostream& os, map const&M){ for(auto e:M){ os<> dxdy = {mp(0,1),mp(1,0),mp(-1,0),mp(0,-1)}; #pragma endregion //fixed< node; UnionFind(){} UnionFind(int N):N(N){ node.assign(N,-1); } int get_root(int x){ if (node[x]<0){ return x; } node[x] = get_root(node[x]); return node[x]; } void unite(int x,int y){ int root_x = get_root(x); int root_y = get_root(y); int larger,smaller; if(root_x != root_y){ if(node[root_x] < node[root_y]){ //size of x is lager than one of y larger = root_x; smaller = root_y; }else{ larger = root_y; smaller = root_x; } node[larger] += node[smaller]; node[smaller] = larger; } } int size(int x){ return -node[x]; } bool same(int x,int y){ return get_root(x) == get_root(y); } }; struct StronglyConnectedComponents{ vector topological_index; //属する強連結成分のトポロジカル順序 vector visited; vector> edge, edge_rev; vector post_order; int N; int scc_size = 0; //強連結成分の数 //O(N+M) StronglyConnectedComponents(vector>& edge):edge(edge){ N = edge.size(); edge_rev.resize(N); for(int v=0;v> build_graph(){ vector> new_edge(N); for(int i=0;i> N >> M; vector> directed; UnionFind UF(N); int a,b,c; REP(i,M){ cin >> a >> b >> c; if(c==1){ if(UF.same(--a,--b)){ cout << "Yes" << bn; return 0; } UF.unite(a,b); }else{ directed.emplace_back(--a,--b); } } vector> edge(N); for(auto e:directed){ tie(a,b) = e; if(UF.same(a,b)){ cout << "Yes" << bn; return 0; } edge[UF.get_root(a)].emplace_back(UF.get_root(b)); } StronglyConnectedComponents SCC(edge); if(SCC.get_scc_size() != N){ cout << "Yes" << bn; }else{ cout << "No" << bn; } }