Click here ~ ~
Determine the tournament position timeLimit: -/ +MS (java/others) MemoryLimit:65536/32768 K(java/others) TotalSubmission (s):17531Accepted Submission (s):7005Problem description hasNA team of players (1<=N<= -), numbered sequentially1,2,3,。。。。 ,NAfter the competition, the Referee Committee will rank all the teams from the point of arrival, 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.InputInput has several groups, the first behavior in each group is two numbersN(1<=N<= -), M;NIndicates the number of teams, and M indicates the input data for M rows. 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. SampleInput4 31 22 34 3Sample Output1 2 4 3
Solving ideas: Topological sorting + priority queuing
Code:
#include <iostream>#include <vector>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace STD;Const intMAXN =1505;//From small to largestructcmp{BOOL operator() (intXintY) {returnx > y; }};intDEGREE[MAXN];intN, M, SEG[MAXN]; vector<int>VEC[MAXN];//topology sequencingintTopo () {priority_queue<int, vector<int>,cmp>q;intIN[MAXN]; for(intI=1; i<=n; i++) {In[i] = Degree[i];if(!in[i]) Q.push (i); }intK =0;BOOLRET =false; while(!q.empty ()) {if(Q.size ()! =1) ret =true;intU = q.top (); Q.pop (); seg[k++] = u; for(intI=0; I<vec[u].size (); i++) {intv = vec[u][i]; in[v]--;if(!in[v]) Q.push (v); } }if(k < N)return-1;if(ret)return 0;return 1;}intMain () { while(Cin>>N>>M) { for(intI=0; i<=n; i++) {vec[i].clear (); Degree[i] =0; } for(intI=0; i<m; i++) {intU, v;Cin>>u>>v; Vec[u].push_back (v); degree[v]++; } for(intI=1; i<=n; i++) sort (Vec[i].begin (), Vec[i].end ()); Topo (); for(intI=0; i<n; i++) {if(i = = n1)printf("%d\n", Seg[i]);Else printf("%d", Seg[i]); } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1285 determine the tournament position