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

Posted leonard-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:

  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, b ≤ N) 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

题意:给定n个编号从1-n的球,m个关系:a b(代表标号为a的球比编号为b的球轻),最后要求输出每个编号的球对应的重量,并且前面编号的球的重量要尽可能小。

题解:逆向建图,把轻的球给予入度(这里可以认为是让重球先出来),然后从n-1标号给予重球,标记即可。

 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 const int N=2222;
 8 int n,m;
 9 int in[N],ans[N];
10 bool E[N][N];
11 queue <int> Q;
12 
13 bool toposort(){
14     while(!Q.empty()) Q.pop();
15     for(int i=n;i>=1;i--){//代表位置,倒序找最大的
16         int cnt=0,idx;
17         for(int j=1;j<=n;j++){//代表球的重量,找球的重量最大的
18             if(in[j]==0) cnt++,idx=j;
19         }
20         if(cnt==0) return false;
21         ans[idx]=i;
22         in[idx]--;
23         for(int k=1;k<=n;k++){
24             if(E[idx][k]) in[k]--;
25         }
26     }
27     return true;
28 }
29 
30 
31 int main(){
32 
33     int t;
34     scanf("%d",&t);
35     while(t--){
36         memset(E,0,sizeof(E));
37         memset(in,0,sizeof(in));
38         scanf("%d%d",&n,&m);
39         //反向建图
40         for(int i=1;i<=m;i++){
41             int a,b;
42             scanf("%d%d",&a,&b);
43             if(!E[b][a]){
44                 E[b][a]=1;
45                 in[a]++;
46             }
47         }
48         if(!toposort()) printf("-1\n");
49         else{
50             for(int i=1;i<=n;i++){
51                 if(i!=n) printf("%d ",ans[i]);
52                 else printf("%d\n",ans[i]);
53             }
54         }
55     }
56 
57     return 0;
58 }

 

以上是关于POJ 3687 Labeling Balls(特殊的拓扑排序)的主要内容,如果未能解决你的问题,请参考以下文章

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

POJ 3687 Labeling Balls

POJ-3687 Labeling Balls(拓扑)

POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

POJ - 3687 Labeling Balls (拓扑排序)

POJ 3783 Balls (DP)