HDU 3687 Labeling Balls (reverse topology)

Source: Internet
Author: User

Labeling Balls

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

1#include <cstdio>2#include <vector>3#include <cstring>4#include <queue>5#include <algorithm>6 using namespacestd;7 8 intindegree[205];9 inttopo[205];Ten intg[205][205]; One intM,n; A  - voidInit () - { thememset (Indegree,0,sizeof(Indegree)); -memset (G,0,sizeof(G)); -     //memset (topo,0,sizeof (topo)); - } +  - BOOLToposort () + { A     intt=N; atpriority_queue<int,vector<int>,less<int> >Q; -      for(inti=n;i>=1; i--) -         if(indegree[i]==0) - Q.push (i); -      while(!q.empty ()) -     { in         intu=q.top (); - Q.pop (); totopo[u]=t--; +          for(intv=1; v<=n;v++) -         { the             if(G[u][v]) *             { $indegree[v]--;Panax Notoginseng                 if(indegree[v]==0) - Q.push (v); the             } +         } A     } the     if(t==0) +     return true; -     return false; $ } $  - intMain () - { the     inti,a,b,t; -scanf"%d",&t);Wuyi      while(t--) the     { - init (); Wuscanf"%d%d",&n,&m); -          for(i=0; i<m;i++) About         { $scanf"%d%d",&a,&b); -             if(!G[b][a]) -indegree[a]++; -g[b][a]=1; A         } +         BOOLflag=Toposort (); the         if(flag) -         { $              for(i=1; i<=n;i++) the             { the                 if(i!=N) theprintf"%d", Topo[i]); the                 Else -printf"%d\n", Topo[i]); in             } the         } the         Else Aboutprintf"-1\n"); the     } the     return 0; the}

HDU 3687 Labeling Balls (reverse topology)

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.