HDU 1285 determining the tournament position "minimum topological sort of dictionary order + priority queue"

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): 33964 Accepted Submission (s): 13321

Problem Description
There are 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
The input has several groups, the first behavior in each group is two number n (1<=n<=500), M, where n represents the number of troops, and m indicates 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 3
0 S
2 3
4 3

Sample Output
1 2 4 3

Author
Smallbeer (CML)

Source
Hangzhou Electric ACM Training Team Training Tournament (VII)

#include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream > #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector > #include <map> #include <cctype> #include <stack> #include <sstream> #include <list># include<assert.h> #include <bitset> #include <numeric> #define DEBUG () puts ("++++") #define GCD (A, B) _ _GCD (A, b) #define Lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define fi first#define se second#define pb push_ Back#define Sqr (x) ((x) * (x)) #define MS (A, b) memset (A,b,sizeof (a)) #define SZ size () #define BE begin () #define PU push_up# Define PD Push_down#define CL Clear () #define LOWBIT (x)-x&x#define all 1,n,1#define Rep (i,n,x) for (int i= (x); i< (n) ; i++) #define in Freopen ("In.in", "R", stdin) #define OUT freopen ("Out.out", "w", stdout) using namespace Std;typedef long Long ll;typedef unsigned long long ull;typedef pair<int,int> p;const int INF = 0x3f3f3f3f;const LL LNF = 1e18;const int MAXN = 1e5 + 20;const int maxm = 1e6 + 10;const Double PI = ACOs ( -1.0); Const d  ouble EPS = 1e-8;const int dx[] = { -1,1,0,0,1,1,-1,-1};const int dy[] = {0,0,1,-1,1,-1,1,-1};const int mon[] = {0, 31, 28, m,ok,u,v;vector<int> g[maxn];int Ans[maxn];char s[10];int indeg[maxn],x;int A,c;char b;priority_queue<int,    vector<int>,greater<int> > Q;void topsort () {int num=0;    while (!q.empty ()) Q.pop ();    for (int i=1;i<=n;i++) if (!indeg[i]) Q.push (i);        while (!q.empty ()) {Int. now = Q.top ();        Q.pop ();        Ans[++num]=now;            for (int i=0; i<g[now].size (); i++) {int NXT = g[now][i];        if (--indeg[nxt] = = 0) q.push (NXT);        }}}int Main () {while (~scanf ("%d%d", &n,&m)) {MS (indeg,0);        MS (ans,0); for (int i=1;i<=n;i++) G[i].clear ();            for (int i=0;i<m;i++) {scanf ("%d%d", &u,&v);            G[u].push_back (v);        indeg[v]++;        } topsort ();        for (int i=1;i<n;i++) printf ("%d", ans[i]);    printf ("%d\n", Ans[n]); } return 0;}

HDU 1285 determines the tournament position "minimum topological sort of dictionary order + priority queue"

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.