HDU 3687 Labeling Balls(逆向拓扑)

Labeling Balls

Description

Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that:

  1. No two balls share the same label.
  2. The labeling satisfies several constrains like "The ball labeled with a is lighter than the one labeled with b".

Can you help windy to find a solution?

Input

The first line of input is the number of test case. The first line of each test case contains two integers, N (1 ≤ N ≤ 200) and M (0 ≤ M ≤ 40,000). The next M line each contain two integers a and b indicating the ball labeled with a must be lighter than the one labeled with b. (1 ≤ a, bN) There is a blank line before each test case.

Output

For each test case output on a single line 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

5

4 0

4 1
1 1

4 2
1 2
2 1

4 1
2 1

4 1
3 2

Sample Output

1 2 3 4
-1
-1
2 1 3 4
1 3 2 4
 1 #include<cstdio>
 2 #include<vector>
 3 #include<cstring>
 4 #include<queue>
 5 #include<algorithm>
 6 using namespace std;
 7
 8 int indegree[205];
 9 int topo[205];
10 int G[205][205];
11 int m,n;
12
13 void init()
14 {
15     memset(indegree,0,sizeof(indegree));
16     memset(G,0,sizeof(G));
17     //memset(topo,0,sizeof(topo));
18 }
19
20 bool toposort()
21 {
22     int t=n;
23     priority_queue<int,vector<int>,less<int> >q;
24     for(int i=n;i>=1;i--)
25         if(indegree[i]==0)
26         q.push(i);
27     while(!q.empty())
28     {
29         int u=q.top();
30         q.pop();
31         topo[u]=t--;
32         for(int v=1;v<=n;v++)
33         {
34             if(G[u][v])
35             {
36                 indegree[v]--;
37                 if(indegree[v]==0)
38                 q.push(v);
39             }
40         }
41     }
42     if(t==0)
43     return true;
44     return false;
45 }
46
47 int main()
48 {
49     int i,a,b,t;
50     scanf("%d",&t);
51     while(t--)
52     {
53         init();
54         scanf("%d%d",&n,&m);
55         for(i=0;i<m;i++)
56         {
57             scanf("%d%d",&a,&b);
58             if(!G[b][a])
59             indegree[a]++;
60             G[b][a]=1;
61         }
62         bool flag=toposort();
63         if(flag)
64         {
65             for(i=1;i<=n;i++)
66             {
67                 if(i!=n)
68                 printf("%d ",topo[i]);
69                 else
70                 printf("%d\n",topo[i]);
71             }
72         }
73         else
74             printf("-1\n");
75     }
76     return 0;
77 }
时间: 2024-07-31 06:42:43

HDU 3687 Labeling Balls(逆向拓扑)的相关文章

POJ 3687 Labeling Balls 逆向拓扑排序

链接: poj3687 题意: 有N个标号为1~N的小球,重量(不包括断言)依次增加 ,现给出M句断言 ,断言格式为a b 表示小球a轻于小球b     要求根据重量大小依次输出1~N号小球应在的的位置(重量递增)不满足断言则输出-1 题解: 因为重量是依次增加的  不能按常规的构造edge[a][b]=1生成拓扑排序 既然关系格式和一般拓扑排序是相反的  我们可以尝试着构造反向边edge[b][a]=1: indegree[a]++;   再根据小球标号从后往前进行拓扑排序,即可得到一幅完整的

POJ 3687 Labeling Balls【拓扑排序 优先队列】

题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.csdn.net/scf0920/article/details/28108243 然后用优先队列来实现的参看的这一篇 http://ycool.com/post/u9ahrwg#algo3 1 #include<iostream> 2 #include<cstdio> 3 #includ

poj 3687 Labeling Balls 【拓扑排序】

题目链接:http://poj.org/problem?id=3687 注意重边 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functional> #include <iterator> #include

[ACM] POJ 3687 Labeling Balls (拓扑排序,逆向建边)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

[ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 toN in such a way that: No two balls share

POJ3687 Labeling Balls (拓扑排序)经典

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11469   Accepted: 3295 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

Labeling Balls(拓扑排序wa)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12466   Accepted: 3576 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

POJ 3687 Labeling Balls(特殊的拓扑排序)

题目链接:http://poj.org/problem?id=3687 题目: Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share the same label. The labeling satisfies several constrains

POJ - 3687 Labeling Balls (拓扑排序)

题意:N个点,给出M条两个点u.v,满足u比值小.给这N个点编号,要求排在前的比排在后的质量小,且编号不重复.求每点能得到最小编号的编号方法. 分析:用拓扑排序求解. 用优先队列来存待标记的点,编号大的点优先出队列,然后从大到小依次标记(编号小的优先肯定是错的,当时wa死了). 若求不出拓扑排序则答案无解. #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #i