(Hdu step 5.2.5) determine the tournament position (for topological sequence)

Source: Internet
Author: User

Topic:

Determine the position of the match
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 337 Accepted Submission (s): 180
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
Give 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)
Recommendlcy


Topic Analysis:

Topological sequences, simple questions.

There are also several topological sequence questions: http://blog.csdn.net/hjd_love_zzt/article/details/28700653


code using the adjacency matrix AC :

/* * e1.cpp * * Created on:2015 March 8 * author:administrator * * #include <iostream> #include <cstdio> #inc Lude <cstring>using namespace Std;const int maxn = 505;int map[maxn][maxn];//connection condition. For example, map[a][b]=1 means a and b connect int The indegree[maxn];//is used to store the degrees of each point int queue[maxn];//the last topological sequence int n;//points int m;//edge number int cnt;//topological sequence node number/** * Find topological sequence */void topo () { int I;int J;int k;for (i = 1; I <= n; ++i) {//Enumerate all beginnings for (j = 1; j <= N; ++j) {//Enumerate all endpoints if (indegree[j] = = 0) {//If a final The 0indegree[j]--;//of the point is removed from the point queue[cnt++] = j;//The point into the topological sequence//The entry of the end point of all edges with that point as a starting point -1for (k = 1; k <= N; ++k) {if (map[j][k]! = 0) {indegree[k]--;}} break;//this must have, otherwise will WA. Break only works on this layer loop. does not work with the If-esle statement}//existence Loop if (J > N) {cout << "presence Ring" << Endl;return;}}} int main () {while (scanf ("%d%d", &n, &m)! = EOF) {memset (indegree, 0, sizeof (indegree)); memset (map, 0, sizeof (map) CNT = 0;int i;for (i = 0; i < m; ++i) {int A, b;scanf ("%d%d", &a, &b), if (map[a][b] = = 0) {//deduplication. There may be duplicate data for this problem. For example 1 2 This set of data appears multiple times Map[a][b]= 1;indegree[b]++;}} Topo (); for (i = 0; i < cnt-1; ++i) {printf ("%d", Queue[i]);} printf ("%d\n", Queue[cnt-1]);} return 0;}


Use the chain forward star to implement the topology sort, but the order of the output does not conform to the test instructions of the problem, but you can take this to learn

The notation of the chain-forward star:

/* * e.cpp * * Created on:2015 March 8 * author:administrator * * #include <iostream> #include <cstdio> #incl Ude <cstring>using namespace Std;const int maxn = 501;const int MAXM = MAXN * maxn;struct Edge {int To;int weight;in T next;}  Edge[maxm];int head[maxn];int indegree[maxn];int N, m;void topo () {int Queue[maxn];int iq = 0;int i;for (i = 1; I <= N; ++i) {if (indegree[i] = = 0) {queue[iq++] = i;}} for (i = 0; i < IQ; ++i) {int k;for (k = Head[queue[i]]; K! =-1; k = edge[k].next) {indegree[edge[k].to]--;if (Indegre E[edge[k].to] = = 0) {queue[iq++] = edge[k].to;}}} for (i = 0; I <= iq-2; ++i) {printf ("%d", Queue[i]);} printf ("%d\n", Queue[iq-1]);//if (IQ < N) {//If the final IQ value is less than N. Indicates that the topology sequence does not exist ...//printf ("t_t\n");//printf ("%d\n", N-iq);/} else {//printf ("O (∩_∩) o\n");//}}int Main () {while (scanf ("%d%d", &n, &m)! = EOF) {memset (head,-1, sizeof (head)); memset (indegree, 0, sizeof (indegree)), int cnt = 0;int i;for (i = 1; I <= m; ++i) {int a;int b;scanf ("%d%d", &a, &b); indegree[b]++;edge[cnt].to = B;edge[cnt].next = Head[a];head[a] = cnt++;} Topo ();} return 0;}









(Hdu step 5.2.5) determine the tournament position (for topological sequence)

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.