Test instructions: Give n individuals, M weight relationship, to meet the severity of the given and meet the number of small as far as possible in the preceding sequence
Because the input is a heavier than B, but we're looking for a lighter one, so we need to build it backwards.
This article http://blog.csdn.net/scf0920/article/details/28108243 the reverse Build
And then use the priority queue to implement the see this article
Http://ycool.com/post/u9ahrwg#algo3
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <queue>9#include <algorithm>Ten #definemod=1e9+7; One using namespacestd; A -typedefLong LongLL; - Const intmaxn=1005; the intG[MAXN][MAXN], out[MAXN],ANS[MAXN]; - - intMain () { - intt,n,m,u,v,tmp,i,j; +scanf"%d",&t); - while(t--){ +Memset (G,0,sizeof(g)); AMemset out,0,sizeof( out)); atCin>>n>>m; - for(i=0; i<m;i++){ -Cin>>u>>v; - if(!G[u][v]) { -g[u][v]=1; - out[u]++; in } - } topriority_queue<int>Q; + for(i=1; i<=n;i++){ - if( out[i]==0) Q.push (i); the } * $ for(i=n;i>=1; i--){Panax Notoginseng if(Q.empty ()) Break; -U=q.top (); Q.pop ();//Take out the largest number in the queue theAns[u]=i;//make the large number as far back as possible + for(j=1; j<=n;j++){ A if(G[j][u]) { theg[j][u]=0; + out[j]--; - if( out[j]==0) Q.push (j); $ } $ - } - } the - if(i) printf ("-1\n");Wuyi Else{ theprintf"%d", ans[1]); - for(i=2; i<=n;i++) printf ("%d", Ans[i]); Wuprintf"\ n"); - } About } $ return 0; -}
View Code
Go---Go----the topological sort of winter vacation learn to forget 7788 said = = This problem of the inverse of the map to understand
POJ 3687 Labeling Balls "topological sort priority queue"