标签:graph pos tchar sub rar 记录 dir ret mda
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 11044 | Accepted: 4542 |
Description
Input
Output
Sample Input
3 3
1 3 2 3 3 1
2 1
1 2
0
Sample Output
1 3
2
Source
定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径;若v不可以到达u,则u到v的路径可有可无。
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define N 10000 using namespace std; bool vis[N]; int n,m,x,y,sum,tim,tot,top; int out[N],dfn[N],low[N],ans[N],head[N],stack[N],belong[N],point[N]; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘) {if(ch==‘-‘) f=-1;ch=getchar();} while(ch<=‘9‘&&ch>=‘0‘) {x=x*10+ch-‘0‘;ch=getchar();} return f*x; } struct Edge { int from,to,next; }edge[500010]; void add(int x,int y) { tot++; edge[tot].to=y; edge[tot].next=head[x]; head[x]=tot; } void begin() { tot=0;top=0;sum=0,tim=0; memset(edge,0,sizeof(edge)); memset(stack,0,sizeof(stack)); memset(head,0,sizeof(head)); memset(out,0,sizeof(out)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(belong,0,sizeof(belong)); memset(ans,0,sizeof(ans)); } int tarjan(int now) { dfn[now]=low[now]=++tim; stack[++top]=now;vis[now]=true; for(int i=head[now];i;i=edge[i].next) { int t=edge[i].to; if(vis[t]) low[now]=min(low[now],dfn[t]); else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]); } if(dfn[now]==low[now]) { sum++;belong[now]=sum; for(;stack[top]!=now;top--) { vis[stack[top]]=false; belong[stack[top]]=sum; } vis[now]=false;top--; } } void shrink_point() { for(int i=1;i<=n;i++) for(int j=head[i];j;j=edge[j].next) if(belong[i]!=belong[edge[j].to]) out[belong[i]]++; } int main() { while(~scanf("%d",&n)&&n) { m=read();begin(); for(int i=1;i<=m;i++) x=read(),y=read(),add(x,y); for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); shrink_point(); x=0; for(int i=1;i<=n;i++) if(!out[belong[i]]) ans[++x]=i; sort(ans+1,ans+1+x); if(x) { for(int i=1;i<x;i++) printf("%d ",ans[i]); printf("%d\n",ans[x]); } else printf("\n"); } return 0; }
注意:注意数组的大小!!
标签:graph pos tchar sub rar 记录 dir ret mda
原文地址:http://www.cnblogs.com/z360/p/7045334.html