Poj3687 Topology Sorting is not clear yet. Let's look back.

Source: Internet
Author: User

[Question]: N weights are 1 ~ Given the weight comparison relationship between some balls (for example, 2 1 indicates that the second ball is lighter than the first ball), calculate the possible weight of each ball, ans [I] indicates the weight of the I-th ball, which requires that the output be the case with the smallest ans Lexicographic Order.

[Idea]: for the given side of a B, select a value with the smallest sequence number (from N to the largest to the smallest value) for each vertex with an outbound degree of 0 ).

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <string> 5 # include <algorithm> 6 # include <map> 7 # include <queue> 8 # include <stack> 9 # include <set> 10 # include <cmath> 11 # include <vector> 12 # define INF 0x3f3f3f13 # define INF 0x3fffffffffffffffll14 # define EPS 1e-915 # define PI ACOs (-1.0) 16 using namespace STD; 17 18 int in [202], ANS [202]; 19 bool G [202] [202], vis [202]; 20 priority_qu EUE <int> q; 21 22 int main () 23 {24 int I, T, J, n, m, a, B; 25 scanf ("% d ", & T); 26 while (t --) 27 {28 // getchar (); 29 scanf ("% d", & N, & M ); 30 memset (in, 0, sizeof (in); 31 memset (G, false, sizeof (g); 32 memset (VIS, false, sizeof (VIS )); 33 while (M --) 34 {35 scanf ("% d", & A, & B); 36 IF (G [B] [a]) 37 continue; 38g [B] [a] = true; 39 in [a] ++; 40} 41 for (I = 1; I <= N; I ++) 42 if (in [I] = 0) 43 {44 Q. push (I); 45 vis [I] = true; 46} 47 int num = N; 48 (I = 0; I <n; I ++) // each time a number with an outbound degree of 0 is the smallest 49 {50 int temp; 51 if (Q. empty () 52 break; 53 temp = Q. top (); q. pop (); // printf ("I % d \ n", temp); 54 ans [temp] = num --; 55 for (Int J = 1; j <= N; j ++) 56 If (J! = Temp & G [temp] [J]) 57 {58 in [J] --; 59 If (in [J] = 0 &&! Vis [J]) 60 {61 Q. push (j); 62 vis [J] = true; 63} 64} 66} 67 // printf ("Ans"); 68 if (I <n) 69 printf ("-1 \ n"); 70 else71 {72 printf ("% d", ANS [1]); 73 for (I = 2; I <= N; I ++) 74 printf ("% d", ANS [I]); 75 printf ("\ n"); 76} 77} 78 79}

 

Consider this set of data:

25 41 44 25 33 25 31 44 23 5 output should be: 1 5 3 4 21 3 4 2 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.