Topology sort + heap.
Turn from POPOQQQ god Ben.
Reverse build to run the topology sort and then output in reverse order.
Why not come, because do not know to choose the current dish to make which vegetables first.
In reverse order to run the topology, you can also ensure that the limit is met the number of small in front.
Off-topic: I have finished the third example of the output is not correct, a look at the problem directly abandoned therapy. It turns out that a few letters are changed ....
Toposort I always like to call "soil dial" sort, probably because my spoken English is not good ...
#include <cstdio>#include<algorithm>#include<cstring>#include<queue>using namespacestd;Const intMAXN =100000+Ten;Const intMAXM =200000+Ten;p Riority_queue<int>Q;intres[maxn],cnt;intG[MAXN],V[MAXM],NEXT[MAXM],inch[Maxn],eid;intT,n,m,u;voidAddedge (intAintb) {V[eid]=b; Next[eid]=g[a]; g[a]=eid++;inch[b]++;}intMain () {scanf ("%d",&T); while(t--) {memset (g,-1,sizeof(g)); Eid=0; scanf ("%d%d",&n,&m); CNT=0; for(intI=1, a,b;i<=m;i++) {scanf ("%d%d",&a,&b); Addedge (B,a); } for(intI=1; i<=n;i++)if(!inch[i]) q.push (i); while(!Q.empty ()) {u=q.top (); Q.pop (); for(intI=g[u];~i;i=Next[i]) { inch[v[i]]--; if(!inch[V[i]]) Q.push (V[i]); } res[++cnt]=u; } if(cnt<n) printf ("impossible!\n"); Else { for(inti=n;i>=1; i--) printf ("%d", Res[i]); printf ("\ n"); } } return 0; }
4010: [HNOI2015] food production