The first is the greedy thought ... We grew up to the top of each place.
Build the inverse diagram, and then sort the topology, each time to find the $deg=0$ point of the largest, so you can ensure that the number of small as far as possible in the back
Finally the order upside down the output is good ...
1 /**************************************************************2 problem:40103 User:rausen4 language:c++5 result:accepted6 time:900 Ms7 memory:3936 KB8 ****************************************************************/9 Ten#include <cstdio> One#include <cstring> A#include <queue> - - using namespacestd; the Const intN = 1e5 +5; - - structEdge { - intnext, to; +Edgeint_n =0,int_t =0): Next (_n), to (_t) {} - } E[n]; + A intN, M; at intfirst[n], tot; - intDeg[n], q[n]; - intT[n], now, v[n]; - intAns[n]; -Priority_queue <int>h; - inInlineintread (); - toInlinevoidAdd_edge (intXinty) { +E[++tot] = Edge (first[x], y), first[x] =tot; -++Deg[y]; the } * $ #defineP Q[l]Panax Notoginseng #defineY e[x].to - BOOLcheck_circle () { the Static intI, L, R, X; + for(R =0, i =1; I <= N; ++i)if(Deg[i] = =0) Q[++r] =i; A for(L =1; L <= R; ++l) the for(x = first[p]; x; x =e[x].next) + if(--deg[y] = =0) Q[++r] =y; - returnr! =N; $ } $ -InlineintBFsints) { - Static intL, R, X; the++Now ; -Q[l = r =1] =s;Wuyi for(; l <= R; + +)l) the for(x = first[p]; x; x = e[x].next)if(!V[y]) { -++Deg[y]; Wu if(T[y]! =Now ) -Q[++r] = y, t[y] =Now ; About } $ } - #undefP - -Inlinevoid Get(ints) { A Static intp, x; + while(!h.empty ()) H.pop (); theH.push (s), v[s] =1; - while(!H.empty ()) { $p =h.top (), H.pop (); theans[++ans[0]] =p; the for(x = first[p]; x; x = e[x].next)if(!V[y]) the if(--deg[y] = =0) H.push (y), v[y] =1; the } - } in #undefY the the intMain () { About intT, I, X, y; theT =read (); the while(t--) { then = Read (), M = Read (), tot = now =0; +memset (First,0,sizeof(first)); -memset (V,0,sizeof(v)); thememset (deg,0,sizeof(deg));Bayimemset (T,0,sizeof(t)); the for(i =1; I <= m; ++i) { thex = Read (), y =read (); - Add_edge (y, x); - } the if(Check_circle ()) { thePuts"impossible!"); the Continue; the } - for(i =1; I <= N; ++i)if(!V[i]) { the BFS (i); the Get(i); the for(; ans[0]; --ans[0]) printf ("%d", ans[ans[0]]);94 } thePuts""); the } the return 0;98 } About -InlineintRead () {101 Static intx;102 Static Charch;103x =0, ch =GetChar ();104 while(Ch <'0'||'9'<ch) theCH =GetChar ();106 while('0'<= CH && Ch <='9') {107x = x *Ten+ CH-'0';108CH =GetChar ();109 } the returnx;111}
View Code
BZOJ4010 [HNOI2015] dish making