POJ 3687 Labeling Balls (拓扑排序)
2016-10-06 19:43
316 查看
Labeling Balls
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit Status
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 Mline 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
Sample Output
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit Status
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 Mline 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
先将编号经过拓扑排序找出,然后再赋予他们重量,最后按序号大小输出重量,注意防止重边现象
#include <cstdio> #include <cstring> #include <queue> #define maxn 100002 using namespace std; int head[maxn], indegree[maxn], ans[maxn],j; struct Node{ int to, next; } map[maxn]; void topoSort(int n) { j=n; priority_queue<int> Q; int i, u; for(i = 1; i <= n; ++i) if(!indegree[i]) Q.push(i); while(!Q.empty()) { u=Q.top(); Q.pop(); ans[u]=j; j--; for(i = head[u]; i != -1; i = map[i].next) if(!--indegree[map[i].to]) Q.push(map[i].to); } } int main() { int t, n, m, a, b, i; scanf("%d", &t); while(t--){ memset(indegree, 0, sizeof(indegree)); memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); for(i = 0; i < m; ++i){ scanf("%d%d", &a, &b); map[i].to = a; map[i].next = head[b]; head[b] = i; ++indegree[a]; } topoSort(n); if(j==0) { for(i = 1; i <= n; i++) if(i != n) printf("%d ", ans[i]); else printf("%d\n", ans[i]); } else printf("-1\n"); } return 0; }
相关文章推荐
- poj 3687 Labeling Balls 拓扑排序
- POJ 3687 Labeling Balls(拓扑排序)
- POJ 3687 Labeling Balls(拓扑排序)
- Poj 3687 Labeling Balls (拓扑排序)
- POJ 3687 Labeling Balls(拓扑排序+反向思考)
- POJ 3687 Labeling Balls (反向拓扑排序)
- POJ 3687 Labeling Balls (拓扑排序)
- POJ 3687 Labeling Balls(拓扑排序)
- poj 3687 Labeling Balls ( 拓扑排序 )
- POJ 3687 Labeling Balls 逆向拓扑排序,贪心思想
- POJ 3687 Labeling Balls【拓扑排序】
- poj3687——Labeling Balls(拓扑排序)
- POJ 3687 Labeling Balls (反向拓扑排序)
- poj 3687 Labeling Balls【拓扑排序 输出元素在拓扑序列中的位置】
- Labeling Balls(POJ 3687-逆序拓扑排序)
- POJ 3687 Labeling Balls 拓扑排序
- [ACM] POJ 3687 Labeling Balls (拓扑排序,逆向建边)
- POJ 3687 Labeling Balls (逆向拓扑排序)
- POJ训练计划3687_Labeling Balls(拓扑排序)
- [ACM] POJ 3687 Labeling Balls (拓扑排序,逆向建边)