Hdu4857 escape
The topic is to find the topology order, but not to make the smallest output according to the Lexicographic Order, but to put a small number at the top.
The first error idea is to give each vertex a priority (the smallest vertex that can be reached by this vertex), and then use Topology Sorting + to prioritize forward processing of the column and forward output. This is incorrect, as shown in the following example:
1
5 4
5 2
4 3
2 1
3 1
The correct solution is to reverse edge creation, with a high priority for vertices. Use Topology Sorting + priority queue to reverse output the sequence.
According to each pair of restrictions, you can determine the topological sequence, but there may be multiple topological sequences at this time (the order of vertices without any relationship is not fixed ). If this question requires a small point to be placed in the front, the sequence can be determined.
(1) If a and B have a direct and simplified topological relationship, the order of A and B can be determined by topological order.
(2) If there is no direct topological relationship between vertices A and B, the order of A and B is determined by the points that A and B can reach.
For example:
1
3 2
3 1
3 1
The output result is 3 1 2.
There is no direct topological relationship between point 3 and point 2, but 3 reaches the minimum point of 1, 2 and 2.
Combining (1) and (2) this question requires reverse processing.
PS: path output of the Euler loop is also reverse output.
# Include <bits \ stdc ++. h> using namespace STD; typedef long ll; const int INF = 1000000007; const double EPS = 1e-10; const int maxn = 30010; int d [maxn]; vector <int> V [maxn]; priority_queue <int, vector <int>, less <int> q; int n, m; int main () {int T; cin> T; while (t --) {scanf ("% d", & N, & M); memset (D, 0, sizeof (d )); for (INT I = 0; I <= N; I ++) V [I]. clear (); For (INT I = 0; I <m; I ++) {int X, Y; Scanf ("% d", & X, & Y); V [Y]. push_back (x); D [x] ++;} For (INT I = 1; I <= N; I ++) if (! D [I]) Q. Push (I); stack <int> SK; while (! Q. empty () {int x = Q. top (); q. pop (); SK. push (x); For (INT I = 0; I <V [X]. size (); I ++) {int y = V [x] [I]; d [y] --; If (! D [y]) {q. Push (y) ;}} int FIR = 1; while (! SK. Empty () {If (! FIR) printf (""); FIR = 0; printf ("% d", SK. top (); SK. pop () ;}puts ("") ;}return 0 ;}