HDU-1285 to determine the ranking

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1285

Multiple methods for Topology Sorting

Determine competition rankings

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/others) total submission (s): 12796 accepted submission (s): 5139

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 input4 31 22 34 3 sample output1 2 4 3 authorsmallbeer (CRF) Source hangdian ACM training team training session (VII) 2. Topology Sorting + priority queue: Train of Thought: add the outgoing degree 0 to the priority queue. Use array d [] to indicate the degree. Map [x] [Y] = 1 indicates that an edge exists and the duplicate edge is considered. Priority queue: http://www.cppblog.com/shyli/archive/2007/04/06/21366.html
#include<iostream>#include<cstring>#include<cstdio>#include<queue>using namespace std;int d[505],map[505][505];priority_queue<int,vector<int>,greater<int> > q;void toposort(int n){  int v,res[505],r,i;     r=0;  for(i=1;i<=n;i++)  {      if(d[i]==0)          q.push(i);  }  while(!q.empty())  {      v=q.top();     // printf("top=%d\n",top);       q.pop();       res[r++]=v;        for(i=1;i<=n;i++)       {           if(!map[v][i])              continue;            d[i]--;          if(d[i]==0)             q.push(i);       }  }  printf("%d",res[0]); for(i=1;i<r;i++)     printf(" %d",res[i]);   printf("\n");}int main(){    int m,x,y,n,i;    while(~scanf("%d%d",&n,&m))     {      memset(d,0,sizeof(d));     memset(map,0,sizeof(map));      for(i=0;i<m;i++)      {         scanf("%d%d",&x,&y);         if(map[x][y])            continue;         map[x][y]=1;         d[y]=d[y]++;     }      toposort(n);     }}

 

HDU-1285 to determine the ranking

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.