POJ 3687 Labeling Balls 拓扑排序
2016-03-16 23:18
477 查看
曾经是2012年AC的题。。
在POJ排到Rank 55是不是已经很好了。。
Total Submissions: 13054 Accepted: 3771
No two balls share the same label.
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?
4 0
4 1
1 1
4 2
1 2
2 1
4 1
2 1
4 1
3 2
-1
-1
2 1 3 4
1 3 2 4
在POJ排到Rank 55是不是已经很好了。。
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define FOR(i,j,k) for(i=j;i<=k;++i) using namespace std; const int N = 205, M = 40001; int h , p[M], v[M], in , ans ; int main() { priority_queue<int> q; int i, j, n, m, t, kase, tag, tot; scanf("%d", &kase); while (kase--) { scanf("%d%d", &n, &m); memset(h, 0, sizeof h); memset(in, 0, sizeof in); t = 1; tot = 0; while (m--) { scanf("%d%d", &j, &i); if (i == j) t = 0; if (!t) continue; p[++tot] = h[i]; v[tot] = j; h[i] = tot; ++in[j]; } if (!t) { printf("-1\n"); continue; } t = n; FOR(i,1,n) if (!in[i]) q.push(i); while (!q.empty()) { i = q.top(); q.pop(); ans[i] = t--; for(j = h[i]; j; j = p[j]) if(!--in[v[j]]) q.push(v[j]); } if (t) printf("-1"); else FOR(i,1,n) printf("%d ", ans[i]); putchar('\n'); } return 0; }
Labeling Balls
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13054 Accepted: 3771
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 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
54 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
相关文章推荐
- codeforces-148E-Porcelain【DP】
- NYOJ 街区最短路径问题
- 关于MyEclipse2015无法导入servlet源码包的解决方法
- springmvc 基础
- Maven之 学习资料
- 读书笔记
- hashtable和hashmap的区别
- 关于try catch finally一点研究
- 清华EMBA课程系列思考之七 -- 财务会计控制
- 几个JAVA常见面试题
- 进程、应用程序域、对象上下文
- C++之:模板元编程(二) 模板形参
- hibernate的面试总结.
- 最近面试的一些感想
- 整型数与浮点数在内存中的表示
- 【凸包问题】HDU1392Surround the Trees
- 用邻接表表示图【java实现】
- CSS学习(十六)-HSLA颜色模式
- LeetCode #14 Longest Common Prefix 最长公共前缀 解题小节
- Yii2 利用controllerMap自定义控制器类