标签:dfs 0ms script style memset i++ ora for false
http://poj.org/problem?id=2553
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 10987 | Accepted: 4516 |
Description
Input
Output
Sample Input
3 3 1 3 2 3 3 1 2 1 1 2 0
Sample Output
1 3 2
Source
1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 const int MAXN(500010); 8 const int N(5010); 9 int n,m,u,v; 10 int sumedge,head[N]; 11 struct Edge 12 { 13 int to,next; 14 Edge(int to=0,int next=0) : 15 to(to),next(next) {} 16 }edge[MAXN]; 17 18 void ins(int from,int to) 19 { 20 edge[++sumedge]=Edge(to,head[from]); 21 head[from]=sumedge; 22 } 23 24 int low[N],dfn[N],tim; 25 int Stack[N],instack[N],top; 26 int sumcol,col[N],point[N]; 27 28 void DFS(int now) 29 { 30 low[now]=dfn[now]=++tim; 31 Stack[++top]=now; instack[now]=true; 32 for(int i=head[now];i;i=edge[i].next) 33 { 34 int go=edge[i].to; 35 if(!dfn[go]) 36 DFS(go),low[now]=min(low[now],low[go]); 37 else if(instack[go]) low[now]=min(low[now],dfn[go]); 38 } 39 if(low[now]==dfn[now]) 40 { 41 col[now]=++sumcol; 42 for(;Stack[top]!=now;top--) 43 { 44 col[Stack[top]]=sumcol; 45 instack[Stack[top]]=false; 46 } 47 instack[now]=false; top--; 48 } 49 } 50 51 int cnt,ans[N],chudu[N]; 52 53 void init() 54 { 55 tim=top=cnt=0; 56 sumcol=sumedge=0; 57 memset(ans,0,sizeof(ans)); 58 memset(low,0,sizeof(low)); 59 memset(dfn,0,sizeof(dfn)); 60 memset(col,0,sizeof(col)); 61 memset(head,0,sizeof(head)); 62 memset(chudu,0,sizeof(chudu)); 63 memset(Stack,0,sizeof(Stack)); 64 memset(instack,0,sizeof(instack)); 65 } 66 67 int main() 68 { 69 /*freopen("made.txt","r",stdin); 70 freopen("myout.txt","w",stdout);*/ 71 72 while(~scanf("%d",&n)&&n) 73 { 74 scanf("%d",&m); init(); 75 for(;m;m--) 76 scanf("%d%d",&u,&v),ins(u,v); 77 for(int i=1;i<=n;i++) 78 if(!dfn[i]) DFS(i); 79 for(u=1;u<=n;u++) 80 for(v=head[u];v;v=edge[v].next) 81 if(col[u]!=col[edge[v].to]) chudu[col[u]]++; 82 /*for(int sc=1;sc<=sumcol;sc++) 83 if(!chudu[sc]) 84 for(int i=1;i<=n;i++) 85 if(sc==col[i]) ans[++cnt]=i;*/ 86 for(int i=1;i<=n;i++) 87 if(!chudu[col[i]]) ans[++cnt]=i; 88 sort(ans+1,ans+cnt+1); 89 if(cnt) 90 { 91 for(int i=1;i<cnt;i++) printf("%d ",ans[i]); 92 printf("%d\n",ans[cnt]); 93 } 94 else printf("\n"); 95 } 96 return 0; 97 }
POJ——T2553 The Bottom of a Graph
标签:dfs 0ms script style memset i++ ora for false
原文地址:http://www.cnblogs.com/Shy-key/p/6881408.html