Labeling Balls--poj3687

Labeling Balls

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12273   Accepted: 3516

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

这是一个拓扑题,还不是普通的拓扑,这必须要反向建图+逆向输出,并且注意,这个题让输出的是各个人的位置!!!
 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4
 5 using namespace std;
 6 int map[250][250];
 7 int degree[250];
 8 void topo(int n)
 9 {
10     int i,j,mark,que[250];
11     for(i=n;i>=0;i--)
12     {
13         int x=0;//标记看是否满足要求
14         for(j=n;j>=0;j--)
15         {
16             if(degree[j]==0)
17             {
18                 x=1;
19                 mark=j;
20                 break;
21             }
22         }
23         if(x==0)
24             break;
25         que[mark]=i;
26         degree[mark]=-1;
27         for(j=1;j<=n;j++)
28         {
29             if(map[mark][j])
30                 degree[j]--;
31         }
32     }
33     if(i!=-1)
34         printf("-1\n");
35     else
36     {
37         printf("%d",que[1]);
38         for(i=2;i<=n;i++)
39         printf(" %d",que[i]);
40         printf("\n");
41
42     }
43 }
44
45 int main()
46 {
47     int N,i,m,n,a,b;
48     scanf("%d",&N);
49     while(N--)
50     {
51         memset(map,0,sizeof(map));
52         memset(degree,0,sizeof(degree));
53         scanf("%d%d",&n,&m);
54         for(i=0;i<m;i++)
55         {
56             scanf("%d%d",&a,&b);
57             if(!map[b][a])//避免重复录入
58             {
59                 map[b][a]=1;
60                 degree[a]++;//反向建图
61             }
62         }
63         topo(n);
64     }
65     return 0;
66 }
时间: 2024-12-30 19:15:03

Labeling Balls--poj3687的相关文章

Labeling Balls POJ3687 【拓扑排序反向建边】【邻接表】

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 like "The

poj3687(Labeling Balls)

题目大意: 给你N个球的重量比较,输出1->N位置球的重量(记住是球的重量,不是按照球重量大小输出序号,球的重量大小也是1->n).如果无法判断输出-1. 解题思路: 拓扑排序,记录较小的编号球的入度,依次n--赋值入度为零的编号球. 代码: 1 #include <algorithm> 2 #include <iostream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <c

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(优先队列+拓扑排序)

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

[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

[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

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: No two balls share the same label. The labeling satisfies several constrains like "The ball labeled wi

POJ3687 Labeling Balls

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13645   Accepted: 3955 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

POJ-3687 Labeling Balls(拓扑)

不一样的拓扑排序 给定一些标记为1到n的数, 求出满足a < b 的序列, 如果有多个输出, 按先标签1往前的位置, 然后按标签2往前的位置, 对于每个标签, 位置都尽量往前. 因为位置要往前,就不能正向建图, 因为正向的拓扑每次在最前的都是最小的点, 并不能保证标签1也在最前面, 比如 1 5 3 4 2 和 1 4 5 3 2 如果按拓扑排序, 答案一定是1 4 5 3 2, 因为4比5小, 但是题目想要各个标签的位置往前, 这样1, 2标签位置一样, 对于3标签, 第一个在3处, 第二个在