1 //tarjan裸题 2 #include <iostream> 3 #include <vector> 4 #include <stack> 5 #include <cstring> 6 using namespace std; 7 const int maxn = 1e4+5; 8 stack<int> s; 9 vector<int> v[maxn]; 10 int dfn[maxn],low[maxn],ins[maxn]; 11 int ant,cnt; 12 int tarjan(int n) 13 { 14 dfn[n] = low[n] = ++ant; 15 s.push(n); 16 for(int i = 0; i < v[n].size(); ++i) 17 { 18 int x = v[n][i]; 19 if(!dfn[x]) 20 low[n] = min(low[n],tarjan(x)); 21 else if(!ins[x]) 22 low[n] = min(low[n],dfn[x]); 23 } 24 if(low[n] == dfn[n]) 25 { 26 ++cnt; 27 int x; 28 do 29 { 30 x = s.top(); s.pop(); 31 ins[x] = cnt; 32 }while(x != n); 33 } 34 return low[n]; 35 } 36 int main() 37 { 38 int n,m,i; 39 while(cin >> n >> m && n+m) 40 { 41 ant = cnt = 0; 42 memset(dfn,0,sizeof dfn); 43 memset(low,0,sizeof low); 44 memset(ins,0,sizeof ins); 45 for(i = 0; i <= n; ++i) 46 v[i].clear(); 47 while(m--) 48 { 49 int x,y; 50 cin >> x >> y; 51 v[x].push_back(y); 52 } 53 tarjan(1); 54 for(i = 1; i < n; ++i) 55 if(ins[i] != ins[i+1]) 56 break; 57 if(i == n) 58 cout << "Yes" << endl; 59 else 60 cout << "No" << endl; 61 } 62 return 0; 63 }