Title Link: Http://codeforces.com/problemset/problem/825/E
Test instructions: Give a direction graph, the arrangement is the ordinal of each point, so that the order of the points corresponding to the sequence of topological order and the Order of the dictionary is minimal.
It is not possible to run the smallest topological order of the dictionary order directly, because that is to make sure that the dictionary order of the points is not the order of the dictionary, such as this data:
10 1
5 2
In turn, a point with a large entry level of 0 must be in the back, and this position is determined. However, a small number of degrees to 0 is not necessarily in the front, because this point may have a degree of not 0, but the point unrelated to this point in front, press test instructions this point is smaller than the current point ordinal.
Reverse build, run dictionary order the largest topological sort on the line.
1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intMAXN =100100;5 intN, M, CNT;6vector<int>G[MAXN];7 intRET[MAXN],inch[MAXN];8priority_queue<int>PQ;9 Ten voidinit () { OneCNT =N; AMemsetinch,0,sizeof(inch)); - while(!pq.empty ()) Pq.pop (); - for(inti =0; i < MAXN; i++) g[i].clear (); the } - - signed Main () { - //freopen ("in", "R", stdin); + intu, v; - while(~SCANF ("%d%d",&n,&m)) { + init (); A for(inti =0; I < m; i++) { atscanf"%d%d",&u,&v); - g[v].push_back (u); - inch[u]++; - } - for(inti =1; I <= N; i++) { - if(!inch[i]) pq.push (i); in } - while(!Pq.empty ()) { to intU =pq.top (); Pq.pop (); +Ret[u] = cnt--; - for(Auto V:g[u]) { the inch[v]--; * if(inch[v] = =0) Pq.push (v); $ }Panax Notoginseng } - for(inti =1; I <= N; i++) { theprintf"%d%c", Ret[i], i==n?'\ n':' '); + } A } the return 0; +}
[cf825e] Minimal Labels (reverse build, topological sort)