[ACM] Hdu 1285 determine the tournament position (topological sort)

Source: Internet
Author: User
Tags in degrees

Determine the position of the match

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 10358 Accepted Submission (s): 4046


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 Input4 31 22 34 3

Sample OUTPUT1 2 4 3

Authorsmallbeer (CML)

SOURCE Hangzhou Electric ACM Training Team Training Tournament (VII)

Problem Solving Ideas:

Principle: Topological ordering is applied to a directed no-loop graph (DAG), and after a directed non-loop topology is sorted, all vertices form a sequence, to all edges (u,v), to meet U at the front of v. The sequence describes the overall order in which the events or states that the vertices represent occur. The classic is in the engineering activities, some projects are completed, some other projects can continue, at this time can be the project as the vertex, the dependencies between the projects for the edge of the building diagram, with the topology of the order to obtain the reasonable execution of all projects.

There are two methods for topological sequencing a DAG, breadth-first search and depth-first search.

This introduction to breadth-first search, the topology of the order, each can be taken out of the vertex must be a point of 0, that is not pointed to the point, because such a point indicates that the event is not dependent on a point of 0 points of the event is executed, it points to the vertex depends on the point of less one, So we can first add all the points in the 0 to a queue, then the point they point to the degree minus 1, and then the point of 0 into the queue, so that the final can be a topological ordered sequence.

In this case, the qualifying ranking may not be unique, at this time the demand for the output of the small number of teams in front, need to use the priority queue, each time from the queue is the smallest element.

Code:

 

#include <iostream> #include <stdio.h> #include <string.h> #include <queue>using namespace std;    const int Maxn=510;int graph[maxn][maxn];//save diagram int degree[maxn];//save in degrees int main () {int n,m;        while (scanf ("%d%d", &n,&m)!=eof) {memset (graph,0,sizeof (graph));        memset (degree,0,sizeof (degree));            for (int i=0;i<m;i++) {int u,v;            scanf ("%d%d", &u,&v);                if (!graph[u][v]) {graph[u][v]=1;        Degree[v]++;//v in + +}} priority_queue<int,vector<int>,greater<int> >q;        for (int i=1;i<=n;i++) if (degree[i]==0) Q.push (i);        BOOL First=1;            while (!q.empty ()) {int cur=q.top ();            Q.pop ();                if (first) {cout<<cur;            first=0;            } else cout<< "" <<cur; for (iNT i=1;i<=n;i++) {if (Graph[cur][i]) {degree[i]--;//The connected points into the                Degree minus 1 if (degree[i]==0)//If the penetration is 0, join the queue q.push (i);    }}} printf ("\ n"); } return 0;}

[ACM] Hdu 1285 determine the tournament position (topological sort)

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.