• [题解] poj 3687 Labeling Balls (拓扑排序)


    - 传送门 -

     http://poj.org/problem?id=3687

    #Labeling Balls

    | Time Limit: 1000MS |   | Memory Limit: 65536K |
    | Total Submissions: 14827 |   | Accepted: 4344 |

    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

    Source

    POJ Founder Monthly Contest – 2008.08.31, windy7926778

    - 题意 -

     有 n 个重量分别为 1 到 n 的球.
     给出 m 组比较, a b 表示 a 的重量小于 b.
     求 1 到 n 的重量.
     存在多组解时, 保证 1 重量最小, 其次保证 2, 然后 3....
     

    - 思路 -

     首先显而易见是拓扑排序.
     但是考虑要让前面的尽量小.
     正向见图就不行了.
     举个例子:
     1
     5 4
     1 4
     4 2
     5 3
     3 2
     
     
     得到了这样的图.
     正向见图找到点的顺序为: 1 4 5 3 2
     1 到 n 的质量为: 1 5 4 2 3
     正确答案为: 1 5 3 4 2 (使 3 尽量小)
     正向见图时, 1 出去后入度为 0 的点就只有 4, 5, 我们无法考虑到 5 指向的 3, 于是便先选择了 4.
     所以用反向建图, 入度为 0 的点从大往小先取大值, 小的节点就能尽量取小一点的值了.
     
     细节见代码.
     

    - 代码 -

    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    const int N = 200 + 5;
    const int M = 4e4 + 5;
    
    int IN[N], ANS[N];
    int MAP[N][N];
    int n, m, a, b, cas, sz;
    
    void topo() {
    	int i, j;
    	for (i = n; i >= 1; --i) {
    		for (j = n; j >= 1; --j) {
    			if (IN[j] == 0) {
    				for (int k = 1; k <= n; ++k) {
    					if (MAP[j][k])
    						IN[k] --;
    				}
    				sz ++;
    				IN[j] --;
    				ANS[j] = i;
    				break;
    			}
    		}
    		if (j == 0) break;
    	}
    	if (sz != n)
    		printf("-1
    ");
    	else {
    		for (int i = 1; i < n; ++i)
    			printf("%d ", ANS[i]);
    		printf("%d
    ", ANS[n]);
    	}
    }
    
    int main () {
    	scanf("%d", &cas);
    	for (int I = 1; I <= cas; ++I) {
    		memset(IN, 0, sizeof (IN));
    		memset(MAP, 0, sizeof (MAP));
    		sz = 0;
    		scanf("%d%d", &n, &m);
    		for (int i = 1; i <= m; ++i) {
    			scanf("%d%d", &a, &b);
    			if (!MAP[b][a]) IN[a] ++;
    			MAP[b][a] = 1;
    		}
    		topo();
    	}
    	return 0;
    }
    
  • 相关阅读:
    Vue源码解析
    开发调试的几个小技巧
    C#课后小作业
    C#随堂
    C#是数据类型
    插眼
    SQL基本的45题
    SQL创建数据库、建表、填入内容
    T-SQL语句基础
    SQL基本数据类型等
  • 原文地址:https://www.cnblogs.com/Anding-16/p/7402534.html
Copyright © 2020-2023  润新知