HDU-1285 determining competition ranking topological sorting

Source: Internet
Author: User
Determine competition rankings


Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3141 accepted submission (s): 1128

Problem description has
N teams (1 <= n <= 500), numbered 1, 2, 3 ,...., n. After the competition is over, the referee committee will sort all participating teams from the back to the back.
Name, but now the referee committee can not directly obtain the results of each team, only know the results of each game, that is, P1 wins P2, expressed in P1, P2, ranking at P1 before P2. Compile Program OK
Ranking.

The input has several groups. The first behavior in each group is n (1 <= n <= 500) and M. N indicates the number of groups, M indicates that there are m rows of input data. In the next row of M data, each row also has two integers P1. P2 indicates that the P1 team won the P2 team.

Output provides a qualified ranking. There is a space between the output team numbers, and there is no space behind the last one.

Other note: the qualified ranking may not be unique. In this case, the team with a small number must be in front of the output. The input data must be correct, that is, input data to ensure a qualified ranking.

Sample input4 3
1 2
2 3
4 3

Sample output1 2 4 3 simple Topology Sorting problem, the problem is that the point with zero inbound is selected.CodeAs follows:

#include 
  
    # define Max 505 # include 
   
     int map [Max] [Max], n, m, hash [Max]; void Update (int x) {for (INT I = 1; I <= N; ++ I) {map [x] [I] = 0 ;}} void Tsort () {for (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= N; ++ J) {If (hash [J]) {continue;} int Sign = 1; for (int K = 1; k <= N & sign; ++ K) {If (Map [k] [J]) {Sign = 0; break ;}} if (sign) {printf (I = 1? "% D": "% d", J); hash [J] = 1; Update (j); break ;}} puts ("");} int main () {While (scanf ("% d", & N, & M )! = EOF) {memset (MAP, 0, sizeof (MAP); memset (hash, 0, sizeof (hash); For (INT I = 1; I <= m; ++ I) {int X, Y; scanf ("% d", & X, & Y); map [x] [Y] = 1 ;} tsort () ;}return 0 ;}
   
  

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.