标签:des style blog http io color ar os sp
3 3 1 3 2 3 3 1 2 1 1 2 0
1 3 2
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 pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 6000; 18 struct arc { 19 int to,next; 20 arc(int x = 0,int y = -1) { 21 to = x; 22 next = y; 23 } 24 }; 25 arc e[maxn*100]; 26 int head[maxn],dfn[maxn],low[maxn],belong[maxn],my[maxn]; 27 int tot,n,m,top,scc,idx,out[maxn],ans[maxn]; 28 bool instack[maxn]; 29 void init() { 30 for(int i = 0; i < maxn; ++i) { 31 dfn[i] = low[i] = belong[i] = 0; 32 instack[i] = false; 33 head[i] = -1; 34 out[i] = 0; 35 } 36 scc = tot = idx = top = 0; 37 } 38 void add(int u,int v){ 39 e[tot] = arc(v,head[u]); 40 head[u] = tot++; 41 } 42 void tarjan(int u) { 43 dfn[u] = low[u] = ++idx; 44 my[top++] = u; 45 instack[u] = true; 46 for(int i = head[u]; ~i; i = e[i].next) { 47 if(!dfn[e[i].to]) { 48 tarjan(e[i].to); 49 low[u] = min(low[u],low[e[i].to]); 50 } else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]); 51 } 52 if(dfn[u] == low[u]) { 53 scc++; 54 int v; 55 do { 56 v = my[--top]; 57 instack[v] = false; 58 belong[v] = scc; 59 } while(v != u); 60 } 61 } 62 int main() { 63 int u,v; 64 while(~scanf("%d",&n)&&n) { 65 scanf("%d",&m); 66 init(); 67 for(int i = 0; i < m; ++i) { 68 scanf("%d%d",&u,&v); 69 add(u,v); 70 } 71 for(int i = 1; i <= n; ++i) 72 if(!dfn[i]) tarjan(i); 73 for(int i = 1; i <= n; ++i) 74 for(int j = head[i]; ~j; j = e[j].next) 75 if(belong[i] != belong[e[j].to]) out[belong[i]]++; 76 int cnt = 0; 77 for(int i = 1; i <= n; ++i) 78 if(!out[belong[i]]) ans[cnt++] = i; 79 if(cnt){ 80 for(int i = 0; i < cnt; ++i) 81 printf("%d%c",ans[i],i + 1 == cnt?‘\n‘:‘ ‘); 82 }else puts(""); 83 } 84 return 0; 85 }
POJ 2553 The Bottom of a Graph
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/crackpotisback/p/4095991.html