Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28765 Accepted Submission(s): 11463
php
#include<queue> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define N 10000 using namespace std; priority_queue<int,vector<int>,greater<int> >q; int n,m,x,y,s,tot,in[N],head[N],ans[N]; int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f; } struct Edge { int from,to,next; }edge[N]; int add(int x,int y) { tot++; edge[tot].to=y; edge[tot].next=head[x]; head[x]=tot; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { s=0,tot=0; memset(in,0,sizeof(in)); memset(ans,0,sizeof(ans)); memset(head,0,sizeof(head)); memset(edge,0,sizeof(edge)); for(int i=1;i<=m;i++) { x=read(),y=read(); in[y]++;add(x,y); } for(int i=1;i<=n;i++) if(in[i]==0) q.push(i); while(!q.empty()) { x=q.top(),q.pop(); ans[++s]=x; for(int i=head[x];i;i=edge[i].next) { int t=edge[i].to; in[t]--; if(in[t]==0) q.push(t); } } for(int i=1;i<s;i++) printf("%d ",ans[i]); printf("%d\n",ans[s]); } return 0; }