HDU1285 -- determine the competition rankings

Source: Internet
Author: User

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) Analysis: Topology Sorting ~

# Include <cstdio> # include <cstring> # include <algorithm> # define maxn 510 using namespace std; int map [maxn] [maxn]; // path int in_degree [maxn]; // inbound int ans [maxn]; int n, m, x, y; void topo () {for (int I = 1; I <= n; I ++) for (int j = 1; j <= n; j ++) if (map [I] [j]) in_degree [j] ++; // records each inbound for (int I = 1; I <= n; I ++) {int k = 1; while (in_degree [k]! = 0) k ++; ans [I] = k; in_degree [k] =-1;/* updated to-1, it is not affected during subsequent detection. It is equivalent to deleting a node */for (int j = 1; j <= n; j ++) if (map [k] [j]) in_degree [j] --; // associated inbound subtraction 1} int main () {while (scanf ("% d", & n, & m )! = EOF) {memset (in_degree, 0, sizeof (in_degree); memset (ans, 0, sizeof (ans); memset (map, 0, sizeof (map )); for (int I = 0; I <m; I ++) {scanf ("% d", & x, & y ); map [x] [y] = 1;} topo (); for (int I = 1; I <n; I ++) printf ("% d ", ans [I]); printf ("% d \ n", ans [n]);} 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.