码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-02-21 10:49:10      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:并且   desc   names   href   建图   ...   first   rip   single   

题目链接: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(特殊的拓扑排序)

标签:并且   desc   names   href   建图   ...   first   rip   single   

原文地址:https://www.cnblogs.com/Leonard-/p/8456186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!