1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include<bits/stdc++.h> using namespace std; #define rep(i,a,n) for(int i=a;i<n;i++) #define repd(i,a,n) for(int i=n-1;i>=a;i--) #define CRL(a,x) memset(a,x,sizeof(a)) const int N=1e5+5;
int DFN[N],Low[N],Clock,Begin[N],ans;bool InStack[N]; struct node{int To,Next;}; vector<node> Edge; stack <int> S;
void Init(){ CRL(DFN,0);CRL(Low,0); CRL(Begin,-1);CRL(InStack,false); while(!S.empty())S.pop(); Edge.clear(); ans=Clock=0; }
void Tarjan(int u){ S.push(u);InStack[u]=true; DFN[u]=Low[u]=++Clock; for(int i=Begin[u];i!=-1;i=Edge[i].Next){ if(!DFN[Edge[i].To]){ Tarjan(Edge[i].To); Low[u]=min(Low[u],Low[Edge[i].To]); } else if(InStack[Edge[i].To]) Low[u]=min(Low[u],DFN[Edge[i].To]); } if(DFN[u]==Low[u]){ ans++;int tem; do{ tem=S.top(); S.pop(); InStack[tem]=false; }while(tem!=u); } }
int main() { std::ios::sync_with_stdio(false); int n,m,a,b; while(cin>>n>>m&&(n||m)){ Init(); rep(i,0,m){ cin>>a>>b; Edge.push_back(node {b,Begin[a]}); Begin[a]=Edge.size()-1; }
rep(i,1,n+1) if(!DFN[i]) Tarjan(i);
puts(ans==1? "Yes":"No"); } return 0; }
|