1 /****************************************************** 2 迷宫城堡(hdu 1269) 3 强连通入门题 4 5 ******************************************************/ 6 7 8 #include<cstdio> 9 #include<cstring> 10 #include<algorithm> 11 #include<stack> 12 #include<vector> 13 using namespace std; 14 15 const int mx=10005; 16 stack<int>s; 17 vector<int>g[mx]; 18 int dfn[mx],low[mx]; 19 int sccno[mx],vs[mx];///sccno记录每个节点是第几个强连通分量 20 int cut_dfs,cut_scc; ///cut_dfn记录dfs每个节点遍历循序, 21 ///cut_scc记录强连通分量的个数 22 23 void dfs(int u) 24 { 25 s.push(u); 26 vs[u]=1; 27 dfn[u]=low[u]=++cut_dfs; 28 for (int i=0;i<g[u].size();i++) 29 { 30 int v=g[u][i]; 31 if (!vs[v]) 32 { 33 dfs(v); 34 low[u]=min(low[u],low[v]); 35 } 36 else low[u]=min(low[u],dfn[v]); 37 } 38 if (low[u]==dfn[u]) 39 { 40 cut_scc++; 41 while (1) 42 { 43 int x=s.top(); 44 s.pop(); 45 sccno[x]=cut_scc; 46 if (x==u) break; 47 } 48 } 49 } 50 51 int main() 52 { 53 int n,m,a,b; 54 while (~scanf("%d%d",&n,&m)&&n+m) 55 { 56 for (int i=1;i<=n;i++) g[i].clear(); 57 while (m--) 58 { 59 scanf("%d%d",&a,&b); 60 g[a].push_back(b); 61 } 62 while (!s.empty()) s.pop(); 63 cut_dfs=cut_scc=0; 64 memset(dfn,0,sizeof(dfn)); 65 memset(low,0,sizeof(low)); 66 memset(vs,0,sizeof(vs)); 67 for (int i=1;i<=n;i++) 68 { 69 if (!dfn[i]) dfs(i); 70 } 71 if (cut_scc==1) printf("Yes\n"); 72 else printf("No\n"); 73 } 74 }