Labeling Balls (Topology sort WA)

Source: Internet
Author: User

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:

    1. No. Balls share the same label.
    2. 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, bN) 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.