标签:des style blog java color strong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6655 Accepted Submission(s): 2973
1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <vector> 5 #include <stack> 6 using namespace std; 7 8 int n, m; 9 vector<int>ve[10005]; 10 stack<int>s; 11 int dfn[10005], low[10005]; 12 bool instack[10005]; 13 int dfn_clock, num; 14 15 void init() //init 16 { 17 int i, j; 18 while(!s.empty()) 19 s.pop(); 20 for(i=1;i<=n;i++) 21 { 22 ve[i].clear(); 23 instack[i]=true; 24 } 25 memset(dfn,0,sizeof(dfn)); 26 memset(low,0,sizeof(low)); 27 } 28 29 void tarjan(int u) //tarjan 30 { 31 int v; 32 s.push(u); 33 dfn[u]=low[u]=++dfn_clock; 34 for(int i=0;i<ve[u].size();i++) 35 { 36 v=ve[u][i]; 37 if(!dfn[v]) 38 { 39 tarjan(v); 40 low[u]=min(low[u],low[v]); 41 } 42 else if(instack[v]) 43 { 44 low[u]=min(low[u],dfn[v]); 45 } 46 } 47 if(low[u]==dfn[u]) 48 { 49 num++; 50 while(1) 51 { 52 v=s.top(); 53 s.pop(); 54 instack[v]=false; 55 if(v==u) break; 56 } 57 } 58 } 59 main() //main 60 { 61 while(scanf("%d %d",&n,&m)==2) 62 { 63 if(n==0&&m==0) break; 64 int i, j, x, y; 65 init(); 66 for(i=1;i<=m;i++) 67 { 68 scanf("%d %d",&x,&y); 69 ve[x].push_back(y); 70 } 71 num=dfn_clock=0; 72 for(i=1;i<=n;i++) 73 if(!dfn[i]) 74 tarjan(i); 75 if(num>1) printf("No\n"); 76 else printf("Yes\n"); 77 } 78 }
HDU 1269 强连通分量tarjan算法,布布扣,bubuko.com
标签:des style blog java color strong
原文地址:http://www.cnblogs.com/qq1012662902/p/3839834.html