problem DescriptionThere 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.
Inputthe 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.
Outputgive 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 3My topology sort is written in the queue, as there are many possible times here, according to the number of small to large output,therefore, the priority queue Priority_queue is changed. The priority queue defaults to a small output, so the operator < is overloaded, making it small to large Priority queue fetch element is not. Front (), but. Top ().the idea of topological sequencing:1. Store the degree of each point with an Inde array, adjacent table save Edge2. Sweep over and add a 0 point into the queue3. Each time an element is taken out of the queue and the element is removed, then the entry of the element to the Edge V-1,if the degree of V becomes 0, the V is added to the queue. re-numbering points can be queued at the point of the time, but also at the point of the time when the team. because every point has to be numbered, so every point has to be in the team once .set a counter, each time out of the team record, if the last counter > number of nodes, the description is not topological order.
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <queue>5#include <vector>6 7 using namespacestd;8 9 Const intmaxn=505;Ten One structNode A { - intv; - BOOL operator< (ConstNode &a)Const the { - returna.v<v; - } - }; + -vector<int>NODE[MAXN]; + intF[MAXN]; A intINDE[MAXN]; at - inttot; - - voidTopointN) - { -tot=1; in -Priority_queue<node>que; to while(!que.empty ()) + Que.pop (); - the for(intI=1; i<=n;i++) * { $ if(inde[i]==0)Panax Notoginseng { - Node temp; thetemp.v=i; + Que.push (temp); A } the } + - while(!que.empty ()) $ { $Node u=que.top (); - Que.pop (); -f[tot++]=u.v; the - for(intI=0; I!=node[u.v].size (); i++)Wuyi { the intw=Node[u.v][i]; -inde[w]--; Wu if(inde[w]==0) - { About Node temp; $temp.v=W; - Que.push (temp); - } - } A } + } the - intMain () $ { the intN; the the while(SCANF ("%d", &n)! =EOF) the { - intm; inscanf"%d",&m); the thememset (Inde,0,sizeof(Inde)); About for(intI=1; i<=n;i++) the node[i].clear (); the the intu,v; + - for(intI=1; i<=m;i++) the {Bayiscanf"%d%d",&u,&v); the Node[u].push_back (v); theinde[v]++; - } - the topo (n); the the for(intI=1; i<tot-1; i++) theprintf"%d", F[i]); -printf"%d\n", f[tot-1]); the } the the return 0;94}
View Code
HDU 1285 determine the ranking of the match topological ranking water problem