#include using namespace std; class SCC{ public: int siz = 0,name = 0,nowc = 0; vector already; vector ord,low,belong,topo; vector> Graph,cycle; stack St; void dfs(int pos){ low.at(pos) = (ord.at(pos) = name++); St.push(pos); for(auto to : Graph.at(pos)){ if(ord.at(to) == -1){ dfs(to); low.at(pos) = min(low.at(pos),low.at(to)); } else if(!already.at(to)) low.at(pos) = min(low.at(pos),ord.at(to)); } if(low.at(pos) == ord.at(pos)){ cycle.push_back({}); while(true){ int s = St.top(); St.pop(); cycle.back().push_back(s); belong.at(s) = nowc; already.at(s) = true; if(s == pos) break; } nowc++; } } void make(vector> &G){ Graph = G; siz = G.size(); ord.resize(siz,-1); low.resize(siz,1001001001); already.resize(siz); belong.resize(siz); for(int i=0; i> N; vector> Graph(N); for(int i=0; i> K; for(int k=0; k> to; to--; Graph.at(i).push_back(to); } } SCC Z; Z.make(Graph); if(Z.belong.at(0) != 0){cout << "No" << endl; return 0;} reverse(Z.cycle.begin(),Z.cycle.end()); set S; bool end = true; for(auto &Vs : Z.cycle){ set S2; for(auto pos : Vs) S2.insert(pos); if(end){end = false; S = S2; continue;} bool ok = false; for(auto pos : Vs){ for(auto to : Graph.at(pos)) if(S.count(to)){ok = true; break;} if(ok) break; } if(!ok){cout << "No" << endl; return 0;} S = S2; continue; } cout << "Yes" << endl; }