1 5 10 3 5 1 4 2 5 1 2 3 4 1 4 2 3 1 5 3 5 1 2
1 2 3 4 5
//453MS 2288K #include<stdio.h> #include<string.h> #include<vector> #include<queue> #define M 30007 using namespace std; vector<int> v[M]; int in[M],ans[M],num,n; void topsort() { num=0; priority_queue<int>q; for(int i=1;i<=n;i++) if(!in[i]){q.push(i);} while(!q.empty()) { int u=q.top(); q.pop(); for(int i=0;i<v[u].size();i++) { in[v[u][i]]--; if(!in[v[u][i]])q.push(v[u][i]); } ans[num++]=u; } } int main() { int t; scanf("%d",&t); while(t--) { int m; scanf("%d%d",&n,&m); memset(in,0,sizeof(in)); for(int i=1;i<=n;i++) v[i].clear(); int a,b; while(m--) { scanf("%d%d",&a,&b); v[b].push_back(a); in[a]++; } topsort(); for(int i=num-1;i>0;i--) printf("%d ",ans[i]); printf("%d\n",ans[0]); } return 0; }
1 3 2 1 2 1 3 6 0 1 15 0 3 4 1 1 1 3 0 2 33 1 2
4 15 15
//453MS 2288K #include<stdio.h> #include<string.h> #include<vector> #include<queue> #define M 30007 using namespace std; vector<int> v[M]; int in[M],ans[M],num,n; void topsort() { num=0; priority_queue<int>q; for(int i=1;i<=n;i++) if(!in[i]){q.push(i);} while(!q.empty()) { int u=q.top(); q.pop(); for(int i=0;i<v[u].size();i++) { in[v[u][i]]--; if(!in[v[u][i]])q.push(v[u][i]); } ans[num++]=u; } } int main() { int t; scanf("%d",&t); while(t--) { int m; scanf("%d%d",&n,&m); memset(in,0,sizeof(in)); for(int i=1;i<=n;i++) v[i].clear(); int a,b; while(m--) { scanf("%d%d",&a,&b); v[b].push_back(a); in[a]++; } topsort(); for(int i=num-1;i>0;i--) printf("%d ",ans[i]); printf("%d\n",ans[0]); } return 0; }
BestCoder Round #1,布布扣,bubuko.com
原文:http://blog.csdn.net/crescent__moon/article/details/38032591