zoukankan      html  css  js  c++  java
  • [题解] 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;
    }
    
  • 相关阅读:
    【PQ】学会逆透视、透视,专治表格多行并一行,一行拆多行【分分合合几时休,学会了马上休】
    【Pandas】concat拼接,plan shapes are not aligned列标号不一致问题
    【MySQL】Pivot功能 一行拆多行等
    【PowerQuery】制作年底倒计时提醒
    数据分析师8大能力
    【爬虫基础】如何查看网页编码
    Mysql 插入中文错误:Incorrect string value: 'xE7xA8x8BxE5xBAx8F...' for column 'course' at row 1
    【MySQL】日期函数、时间函数总结
    mysql相关问题总结
    2020年 10月17 日 我遇见了一个很好的,善解人意的女孩
  • 原文地址:https://www.cnblogs.com/Anding-16/p/7402534.html
Copyright © 2011-2022 走看看