Topological ordering, to make the dictionary order smallest, so the stack changed piles.
1#include <cstdio>2#include <queue>3#include <algorithm>4 using namespacestd;5 #defineN 1000016priority_queue<int,vector<int>,greater<int> >Q;7 intn,m,x,y;8 intV[n],first[n],next[n],en,chu[n],ru[n],tot,ans[n];9 voidAddedge (Const int&u,Const int&V)Ten { Onev[++en]=V; Anext[en]=First[u]; -first[u]=en; - } the intMain () - { -scanf"%d%d",&n,&m); - for(intI=1; i<=m;i++) + { -scanf"%d%d",&x,&y); +chu[x]++; ru[y]++; A Addedge (x, y); at } - for(intI=1; i<=n;i++)if(!Ru[i]) Q.push (i); - while(!q.empty ()) - { - intlast=tot; -ans[++tot]=Q.top (); Q.pop (); in for(intI=first[ans[tot]];i;i=Next[i]) - { toru[v[i]]--; + if(!Ru[v[i]]) Q.push (V[i]); - } the } * if(Tot!=n) puts ("OMG."); $ ElsePanax Notoginseng { - for(intI=1; i<n;i++) theprintf"%d", Ans[i]); +printf"%d\n", Ans[n]); A } the return 0; +}
"Topology Sort" "Heap" CH Round #57-story of the OI Class error