poj 3687 Labeling Balls 【拓扑排序 输出元素在拓扑序列中的位置】
来源:互联网 发布:手机淘宝5.6.0版本 编辑:程序博客网 时间:2024/06/06 22:46
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 04 11 14 21 22 14 12 14 13 2
Sample Output
1 2 3 4-1-12 1 3 41 3 2 4
大致题意:给你N个点和M个约束条件。问你拓扑序列是否存在,若存在则输出每个元素在 字典序最小的 拓扑序列中的位置,否则输出-1.
具体思路不说了。注意要反向拓扑 + 优先队列 才能找到字典序最小的解。
AC代码:
#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <algorithm>#define MAXN 200+10#define MAXM 40000+10using namespace std;struct Edge{ int from, to, next;};Edge edge[MAXM];int head[MAXN], edgenum;int in[MAXN];int N, M;void init(){ edgenum = 0; memset(head, -1, sizeof(head)); memset(in, 0, sizeof(in));}void addEdge(int u, int v){ int i; for(i = head[u]; i != -1; i = edge[i].next) { if(edge[i].to == v) break; } if(i == -1) { in[v]++; Edge E = {u, v, head[u]}; edge[edgenum] = E; head[u] = edgenum++; }}void getMap(){ int a, b; while(M--) { scanf("%d%d", &a, &b); addEdge(b, a); }}int pos[MAXN];//序列中位置int num;void solve(){ priority_queue<int> Q; stack<int> S; for(int i = 1; i <= N; i++) if(in[i] == 0) Q.push(i); while(!Q.empty()) { int u = Q.top(); Q.pop(); S.push(u); for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(--in[v] == 0) Q.push(v); } } if(S.size() == N) { num = 0; while(!S.empty()) { pos[S.top()] = ++num;//存储元素 在拓扑序列中的位置 S.pop(); } for(int i = 1; i <= N; i++) { if(i > 1) printf(" "); printf("%d", pos[i]); } printf("\n"); } else printf("-1\n"); while(!S.empty()) S.pop();}int main(){ int t; scanf("%d", &t); while(t--) { //printf("\n"); scanf("%d%d", &N, &M); init(); getMap(); solve(); } 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 Labeling Balls 3687 拓扑排序!!!!
- poj 3687 Labeling Balls 拓扑排序
- POJ 3687Labeling 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 拓扑排序
- 通过KVM_SET_USER_MEMORY_REGION操作虚拟机内存(Kernel 3.10.0 & qemu 2.0.0)
- java基础--网络编程
- PHP中 HTTP_HOST 和 SERVER_NAME 的区别
- ttl485
- Spring 事务管理
- poj 3687 Labeling Balls 【拓扑排序 输出元素在拓扑序列中的位置】
- Java日期操作
- 安装PS 无法选择路径
- 关于软件开发,你老板不知道的7件事
- Linux进程的实际用户ID和有效用户ID
- strip_tags()函数使用注意
- ZOJ 1654 Place the Robots(放置机器人)------最大独立集
- 仿ios右滑界面返回功能
- Python字典