( ̄▽ ̄) "
//This problem is required for the serial number output, the smaller ordinal priority output, so use the priority queue//Priority queue is the highest priority pop-up value, so the last to reverse the output, is the correct outputs#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<cstdlib>#include<vector>#include<queue>using namespaceStd;typedefLong Longll;Const intmaxn=100005; Vector<int>G[MAXN];intdegree[maxn],l[maxn],n,m;voidToposort () {inttot=0; Priority_queue<int> que;//the implementation of Toposort is similar to BFS for(intI=1; i<=n;i++) if(!Degree[i]) Que.push (i); while(!Que.empty ()) { intx=que.top (); Que.pop (); L[tot++]=x; for(intj=0; J<g[x].size (); j + +) { intt=G[x][j]; Degree[t]--; if(!degree[t]) Que.push (t); } } //if (tot==n) return true; //return false;}intMain () {intt,a,b; scanf ("%d",&t); while(t--) {scanf ("%d%d",&n,&m); memset (degree,0,sizeof(degree)); memset (L,0,sizeof(L)); Memset (g,0,sizeof(g)); for(intI=0; i<m;i++) {scanf ("%d%d",&a,&b); G[b].push_back (a); Degree[a]++; } toposort (); for(inti=n-1; i>=0; i--) {printf ("%d", L[i]); if(i!=0) printf (" "); Elseprintf ("\ n"); } }}
HDU 4857 Escape (Reverse topology sort + priority queue)