Test instructions: 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. (The Chinese problem surface will not speak)
The main problem needs to be sorted by the minimum dictionary order, so the priority queue is used instead of the queue to complete the topology sort.
1#include <stdio.h>2#include <string.h>3#include <queue>4#include <algorithm>5 using namespacestd;6 Const intmaxm=505;7 8 intid[maxm],n,m;9 intMA[MAXM][MAXM];Ten One voidtopo () { Apriority_queue<int,vector<int>,greater<int> >Q; - for(intI=1; i<=n;i++){ - if(!Id[i]) Q.push (i); the } - intCnt=0; - while(!Q.empty ()) { - intu=q.top (); +printf"%d", u); - if(++cnt==n) printf ("\ n"); + Elseprintf" "); A Q.pop (); at for(intI=1; i<=n;++i) { - if(I==u)Continue; - if(Ma[u][i]) { -id[i]--; - if(!Id[i]) Q.push (i); - } in } - } to } + - intMain () { the while(SCANF ("%d%d", &n,&m)! =EOF) { *Memset (MA,0,sizeof(MA)); $ while(m--){Panax Notoginseng intb; -scanf"%d%d",&a,&b); the if(!Ma[a][b]) { +ma[a][b]=1; Aid[b]++; the } + } - topo (); $ } $ return 0; -}
View Code
hdu1285 topological sequence