HDU 1285 determining the tournament position (topological sorting)

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): 14854 Accepted Submission (s): 5929


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 Input
4 31 22) 34 3

Sample Output
1 2 4 3

Authorsmallbeer (CML)
SOURCE Hangzhou Electric ACM Training Team Training Tournament (VII)
Because the title requirement is when there are multiple cases, according to the small to sort, so it is necessary to enter the degree of 0 points one by one to the queue operation, and constantly update the point of the degree. The point of the problem is to consider the heavy side. A typical topological sorting problem, my first topological sort problem, feel the code to improve, otherwise too troublesome.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace Std;
int main ()
{
int n,i,j,k,m,p1,p2,indegree[1000],v[1000],num;
int map[505][505];
queue<int>qu;
while (scanf ("%d%d", &n,&m)!=eof)
{

memset (map,0,sizeof (map));
memset (indegree,0,sizeof (Indegree));
for (i=1;i<=m;i++)
{
scanf ("%d%d", &AMP;P1,&AMP;P2);
if (!MAP[P1][P2])//Here to judge the heavy side, if not on WA
indegree[p2]++;
Map[p1][p2]=1;
}
for (i=1;i<=n;i++)
if (!indegree[i]) {Qu.push (i); indegree[i]--; break;} Find the first point in the 0, and break off once you find it.
num=0;
while (!qu.empty ())
{
V[num]=qu.front (); Qu.pop (); num++;

for (i=1;i<=n;i++)
{
if (Map[v[num-1]][i])
indegree[i]--;

}
for (i=1;i<=n;i++)
if (!indegree[i]) {
Qu.push (i); indegree[i]--;break; Ditto, once found, immediately jump out
}
}

for (i=0;i<num-1;i++)
printf ("%d", v[i]);
printf ("%d\n", v[num-1]);
}

return 0;
}

HDU 1285 determining the tournament position (topological sorting)

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.