*hdu1285 topology Ordering

Source: Internet
Author: User

Determine the position of the match

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 23336 Accepted Submission (s): 9441


Problem description has N teams (1<=n<=500), numbered three-in-one, .... , N to play, after the game, the Referee Committee will be all the teams from the arrival of the ranking, but now the referee committee can not directly get each team's performance, only 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 n (1<=n<=500), M, where n represents the number of troops, and m represents the input data for the M row. 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 the output is required to be numbered in front of the team; the input data is guaranteed to be correct, i.e. the input data ensures that there must be a qualifying ranking.

Sample Input4 31 22 34 3

Sample OUTPUT1 2 4 3

Authorsmallbeer (CML)

SOURCE Hangzhou Electric ACM Training Team Training Tournament (VII) code:
1 //topology ordering. A sort like this is called topological ordering. The subject of the dictionary order small in front so with priority queue to save. 2 //save their relationship with a vector array, in which the in numbers record the degrees of each element, in the top of the zero, each time a layer element with zero degrees is swept through3 //Find out if there is only one son (from his side), if there is a son to join the queue, not to subtract one-time into the degree. Until finally, if there is an in-queue, there is a ring. 4#include <iostream>5#include <cstdio>6#include <cstring>7#include <vector>8#include <queue>9#include <functional>Ten using namespacestd; One intans[505]; Avector<int>g[505]; - int inch[505]; - intn,m,cnt; the voidtopo () - { -priority_queue<int,vector<int>,greater<int> >Q; -Cnt=0; +      for(intI=1; i<=n;i++) -     if(inch[i]==0) + Q.push (i); A      while(!q.empty ()) at     { -         intA=q.top (); - Q.pop (); -ans[++cnt]=A; -          for(intI=0; I<g[a].size (); i++) -         { in             intb=G[a][i]; -             if(--inch[b]==0) to Q.push (b); +         } -     } the } * intMain () $ {Panax Notoginseng     intb; -      while(SCANF ("%d%d", &n,&m)! =EOF) the     { +          for(intI=0; i<=n;i++) A         { the g[i].clear (); +             inch[i]=0; -         } $          for(intI=0; i<m;i++) $         { -scanf"%d%d",&a,&b); - G[a].push_back (b); the             inch[b]++; -         }Wuyi topo (); the        //if (cnt<n) printf ("No topological sort with loops"); -          for(intI=1; i<cnt;i++) Wuprintf"%d", Ans[i]); -printf"%d\n", ans[cnt]); About     } $     return 0; -}

*hdu1285 topology Ordering

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.