BestHack.....真乱.....
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 <algorithm> #include <vector> #include <queue> using namespace std; const int maxn=63000; int n,m; struct Edge { int to,next; }edge[maxn*3]; int Adj[maxn],Size=0,du[maxn]; bool used[maxn]; void Add_Edge(int u,int v) { edge[Size].to=v; edge[Size].next=Adj[u]; du[v]++; Adj[u]=Size++; } void init() { memset(Adj,-1,sizeof(Adj));Size=0; memset(du,0,sizeof(du)); memset(used,false,sizeof(used)); } priority_queue<int,vector<int>,less<int> > q; vector<int> vc; int main() { int T_T; scanf("%d",&T_T); while(T_T--) { init(); vc.clear(); scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); Add_Edge(b,a); } for(int i=1;i<=n;i++) if(du[i]==0) q.push(i); while(!q.empty()) { int u=q.top(); q.pop(); vc.push_back(u); for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; du[v]--; if(du[v]==0) q.push(v); } } int sz=vc.size(); for(int i=sz-1;i>=0;i--) { if(i!=sz-1) putchar(32); printf("%d",vc[i]); } putchar(10); } return 0; }
原文:http://blog.csdn.net/ck_boss/article/details/37997675