Labeling Balls
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 12466 |
|
Accepted: 3576 |
Description
Windy have n balls of distinct weights from 1 unit to N units. Now the he tries to the label them with 1 to N in such a by that:
- No. Balls share the same label.
- The labeling satisfies several constrains like "the ball labeled with a are lighter than the one labeled with b ".
Can windy to find a solution?
Input
The first line of input was the number of the test case. The first line of all test case contains-integers, n (1≤ n ≤200) and m (0≤ m ≤ 40,000). The next M line each contain, integers a and B indicating the ball labeled with a must is lighter than the one labeled with b. (1≤ A, b ≤ N) There is a blank line before each test case.
Output
For each test, the output on a, the balls ' weights from label 1 to label N. If Several solutions exist, you should output the one with the smallest weight for label 1, then with the smallest weight For label 2, then with the smallest weight for label 3 and so on ... If no solution exists, output-1 instead.
Sample Input
54 04 11 14 21 22 14 12 14 13 2
Sample Output
1 2 3 4-1-12 1 3 41 3 2 4
The inverse of the map, but WA ... Just leave it.
WA Code:
1#include <stdio.h>2#include <string.h>3#include <queue>4 #defineMem (x, y) memset (x,y,sizeof (x))5 using namespacestd;6 Const intmaxn= About;7 Const intmaxm=40010<<1;8 intHEAD[MAXM];9 intQUE[MAXN],ANS[MAXN];Ten structedge{ One int from, To,next; A }; - Edge EDG[MAXM]; - intEdgnum; the intN; - voidAddintUintv) { -Edge e={U,v,head[u]}; -edg[edgnum]=E; +head[u]=edgnum++; - } + voidTopu () { Apriority_queue<int>DL; at inttop=1; - for(intI=1; i<=n;i++) - if(!Que[i]) Dl.push (i); - while(!Dl.empty ()) { - intk=dl.top (); - Dl.pop (); inans[top++]=K; -que[k]=-1; to for(inti=head[k];i!=-1; i=Edg[i].next) { + intv=edg[i].to; -que[v]--; the if(!Que[v]) Dl.push (v); * } $ }Panax Notoginseng if(top!=n+1) puts ("-1"); - Else{ the for(inti=n;i>=1; i--){ + if(i!=n) printf (" "); Aprintf"%d", Ans[i]); the } +Puts""); - } $ } $ intMain () { - intt,m,a,b; -scanf"%d",&T); the while(t--){ -scanf"%d%d",&n,&M);Wuyi for(intI=1; i<=n;i++) ans[i]=i; theMem (que,0); -MEM (head,-1); Wuedgnum=0; - while(m--){ Aboutscanf"%d%d",&a,&b); $Add (B,a);// -que[a]++;//Reverse Build Diagram - } - Topu (); A } + return 0; the}
Labeling Balls (Topology sort WA)