HDU1285 determine the competition ranking (topological sorting)

Source: Internet
Author: User

HDU1285 determine the competition ranking (topological sorting)
Determine competition rankingsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 14558 Accepted Submission (s): 5814



Problem Description has N teams (1 <= N <= 500), numbered 1, 2, 3 ,...., n. After the competition is over, the referee committee will rank all participating teams from the past to the next. However, the referee Committee cannot directly obtain the results of each team, but only knows the results of each competition, that is, P1 wins P2, which is represented by P1 and P2, and is ranked before P2. Now, compile the program to determine the 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 Input

4 31 22 34 3

Sample Output
1 2 4 3

Author SmallBeer (CRF)
Source hangdian ACM training team training competition (VII)
#include
 
  #include
  
   const int N =505;int mapt[N][N],n,in[N];void tope(){    int k=n;    while(k--)    {        int i;        for( i= 1;i<=n;i++)            if(in[i]==0)            break;        printf("%d",i);        if(k)            printf(" ");        for(int j=1;j<=n;j++)            in[j]-=mapt[i][j];        in[i]=-1;    }    printf("\n");}int main(){    int m;    while(scanf("%d%d",&n,&m)>0)    {        memset(mapt,0,sizeof(mapt));        memset(in,0,sizeof(in));        while(m--)        {            int a,b;            scanf("%d%d",&a,&b);            mapt[a][b]++; in[b]++;        }        tope();    }}
  
 

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.