HDU 1285--to determine the rank of the contest "topological sorting && adjacency Table Implementation "

Source: Internet
Author: User

Determine the position of the matchTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 17134 Accepted Submission (s): 6789


Problem description has N race teams (1<=n<=500), numbered 1, 2. 3, .....

。 n Play the game. At the end of the game, the Referee Committee will rank all the participating teams from the point of travel, but now the referee Committee cannot get the results of each team directly. Just know the results of each game, that is P1 win P2, with P1,p2 said, ranked P1 before P2.

Now ask you to compile the program to determine the rankings.

Input inputs have several groups, the first behavior in each group is two number N (1<=n<=500), M. n indicates the number of teams. m indicates that the input data of the M row is followed. In the next M-row data, there are also two integers per line p1,p2 means that the P1 team won the P2 team.



Output gives a ranking that meets the requirements. There is a space between the queue numbers at the time of the output, and no space after the last.

Other Notes: Qualifying rankings may not be unique, at which point a small number of teams are required to output, and the input data is guaranteed to be correct. That is, the input data ensures that there must be a ranking that meets the requirements.

Sample Input

4 31 22) 34 3

Sample Output
1 2 4 3

Note two points:

(1): There may be a heavy edge in the input. Do not consider the adjacency table processing.

(2): The topic requires the output of small number in front, so with priority queue to store


#include <cstdio> #include <cstring> #include <algorithm> #include <queue>using namespace std; struct node {int u, V, next;};    int N, m;node edge[550 * 550];int in[550];int head[550], cnt;int sum[550];void init () {cnt = 0;    Memset (Head,-1, sizeof (head));    memset (in, 0, sizeof (in)); memset (sum, 0, sizeof (sum));}    void Add (int u, int v) {edge[cnt] = {u, V, Head[u]}; Head[u] = cnt++;}    void Topsort () {int ans = 0;    Priority_queue<int,vector<int>,greater<int> >q;    for (int i = 1; I <= n; ++i) {if (in[i] = = 0) Q.push (i);        } while (!q.empty ()) {int u = q.top ();        sum[ans++] = u;        Q.pop ();            for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V;            in[v]--;        if (in[v] = = 0) q.push (v);        }} for (int i = 0; i < ans; ++i) {if (!i) printf ("%d", sum[i]);    else printf ("%d", sum[i]);   } printf ("\ n");}        int main () {while (scanf ("%d%d", &n, &m)! = EOF) {init ();            while (m--) {int A, B;                scanf ("%d%d", &a, &b);                Add (A, b);        in[b]++;    } topsort (); } return 0;}


HDU 1285--to determine the rank of the contest "topological sorting &amp;&amp; adjacency Table Implementation "

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.