Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6826 Accepted Submission(s): 3034
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define INF 0x3f3f3f3f 15 using namespace std; 16 const int maxv = 10010; 17 const int maxe = 100010; 18 int dfn[maxv],low[maxv],n,m,index,scc; 19 vector<int>g[maxv]; 20 bool instack[maxv]; 21 stack<int>s; 22 void tarjan(int u){ 23 s.push(u); 24 instack[u] = true; 25 dfn[u] = low[u] = ++index; 26 for(int i = 0; i < g[u].size(); i++){ 27 int v = g[u][i]; 28 if(!dfn[v]){ 29 tarjan(v); 30 low[u] = min(low[u],low[v]); 31 }else if(instack[u] && low[u] > dfn[v]) low[u] = dfn[v]; 32 } 33 if(dfn[u] == low[u]){ 34 int v; 35 scc++; 36 do{ 37 v = s.top(); 38 s.pop(); 39 }while(v != u); 40 } 41 } 42 int main(){ 43 int i,j,u,v; 44 while(scanf("%d%d",&n,&m),n+m){ 45 for(i = 1; i <= n; i++) 46 g[i].clear(); 47 memset(dfn,0,sizeof(dfn)); 48 memset(low,0,sizeof(low)); 49 memset(instack,false,sizeof(instack)); 50 for(i = 0; i < m; i++){ 51 scanf("%d%d",&u,&v); 52 g[u].push_back(v); 53 } 54 while(!s.empty()) s.pop(); 55 index = scc = 0; 56 for(i = 1; i <= n; i++) 57 if(!dfn[i]) tarjan(i); 58 scc==1?puts("Yes"):puts("No"); 59 } 60 return 0; 61 }
上面的代码有点傻逼,冗余了一些计算。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define INF 0x3f3f3f3f 15 using namespace std; 16 const int maxv = 10010; 17 const int maxe = 100010; 18 int dfn[maxv],low[maxv],n,m,index,scc; 19 vector<int>g[maxv]; 20 bool instack[maxv],flag; 21 stack<int>s; 22 void tarjan(int u){ 23 s.push(u); 24 instack[u] = true; 25 dfn[u] = low[u] = ++index; 26 for(int i = 0; i < g[u].size(); i++){ 27 int v = g[u][i]; 28 if(!dfn[v]){ 29 tarjan(v); 30 low[u] = min(low[u],low[v]); 31 }else if(instack[u] && low[u] > dfn[v]) low[u] = dfn[v]; 32 } 33 if(dfn[u] == low[u]){ 34 int v,o = 0; 35 do{ 36 v = s.top(); 37 s.pop(); 38 o++; 39 }while(v != u); 40 if(o == n) flag = true; 41 } 42 } 43 int main(){ 44 int i,j,u,v; 45 while(scanf("%d%d",&n,&m),n+m){ 46 for(i = 1; i <= n; i++){ 47 g[i].clear(); 48 dfn[i] = 0; 49 low[i] = 0; 50 instack[i] = false; 51 } 52 for(i = 0; i < m; i++){ 53 scanf("%d%d",&u,&v); 54 g[u].push_back(v); 55 } 56 while(!s.empty()) s.pop(); 57 flag = index = 0; 58 tarjan(1); 59 flag?puts("Yes"):puts("No"); 60 } 61 return 0; 62 }
美观点
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define INF 0x3f3f3f3f 15 using namespace std; 16 const int maxv = 10010; 17 const int maxe = 100010; 18 int dfn[maxv],low[maxv],n,m,index,scc; 19 vector<int>g[maxv]; 20 bool instack[maxv]; 21 stack<int>s; 22 bool tarjan(int u) { 23 s.push(u); 24 instack[u] = true; 25 dfn[u] = low[u] = ++index; 26 for(int i = 0; i < g[u].size(); i++) { 27 int v = g[u][i]; 28 if(!dfn[v]) { 29 if(tarjan(v)) return true; 30 low[u] = min(low[u],low[v]); 31 } else if(instack[u] && low[u] > dfn[v]) low[u] = dfn[v]; 32 } 33 if(dfn[u] == low[u]) { 34 int v,o = 0; 35 do { 36 v = s.top(); 37 s.pop(); 38 o++; 39 } while(v != u); 40 if(o == n) return true; 41 } 42 return false; 43 } 44 int main() { 45 int i,j,u,v; 46 while(scanf("%d%d",&n,&m),n+m) { 47 for(i = 1; i <= n; i++) { 48 g[i].clear(); 49 dfn[i] = 0; 50 low[i] = 0; 51 instack[i] = false; 52 } 53 for(i = 0; i < m; i++) { 54 scanf("%d%d",&u,&v); 55 g[u].push_back(v); 56 } 57 while(!s.empty()) s.pop(); 58 index = 0; 59 tarjan(1)?puts("Yes"):puts("No"); 60 } 61 return 0; 62 }
原文:http://www.cnblogs.com/crackpotisback/p/3863795.html