标签:des style blog java color strong
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
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> #define maxn 30005 using namespace std; int n,m,ans; bool vis[maxn]; int in[maxn]; vector<int>edge[maxn]; int main() { int i,j,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(in,0,sizeof(in)); for(i=1;i<=n;i++) edge[i].clear(); int u,v; for(i=1;i<=m;i++) { scanf("%d%d",&u,&v); in[u]++; edge[v].push_back(u); } priority_queue<int> q; for(i=1;i<=n;i++) { if(in[i]==0) q.push(i); } memset(vis,0,sizeof(vis)); vector<int>res; ans=0; while(!q.empty()) { ans++; u=q.top(); res.push_back(u); q.pop(); vis[u]=1; for(i=0;i<edge[u].size();i++) { v=edge[u][i]; in[v]--; if(in[v]==0) q.push(v); } } for(i=res.size()-1;i>=0;i--) { if(i==res.size()-1) printf("%d",res[i]); else printf(" %d",res[i]); } puts(""); } return 0; } /* 20 4 3 4 1 4 2 3 2 */
hdu 4857 逃生 (拓扑排序+保证最小在前面),布布扣,bubuko.com
标签:des style blog java color strong
原文地址:http://blog.csdn.net/tobewhatyouwanttobe/article/details/37997339