poj 3687 Labeling Balls【拓扑排序 输出元素在拓扑序列中的位置】
来源:互联网 发布:淘宝刷单清洗之后降权 编辑:程序博客网 时间:2024/06/14 00:27
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 bindicating 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
#include<cstdio>#include<cstring>#include<cmath>#include<stack>#include<queue>#define cle(a, b) memset(a, (b), sizeof(a))#define Wi(a) while(a--)#define Si(a) scanf("%d", &a)#define Pi(a) printf("%d\n", (a))#define INF 0x3f3f3f3f#include<algorithm>using namespace std;const int M = 40000+10;const int N = 200+10;int n, m;struct node{int from, to, next;};node p[M];int head[N], edgenum;int in[N];void init(){cle(head, -1);cle(in, 0);edgenum = 0;}void addedge(int u, int v){int i;for(i = head[u]; i != -1; i = p[i].next){if(p[i].to == v) break;}if(i == -1){in[v]++;node E = {u, v, head[u]};p[edgenum] = E;head[u] = edgenum++;}}void getmap(){int a, b;Wi(m){scanf("%d%d", &a,&b);addedge(b ,a);}}int pos[N];int num;void solve(){priority_queue<int> q;stack<int> s;for(int i = 1; i <= n; i++){if(!in[i]) q.push(i);}while(!q.empty()){int u = q.top(); q.pop();s.push(u);for(int i = head[u]; i != -1; i = p[i].next){int v = p[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++){printf(i>1?" %d":"%d",pos[i]);}printf("\n");}elseprintf("-1\n");while(!s.empty()) s.pop();}int main(){int c; Si(c);Wi(c){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 拓扑排序
- ubuntu apache 绑定域名
- uva 10905 Children's Game
- android四大组件之一Serveric
- mysql忽略主键冲突,避免重复插入的几种方式
- 【EJB学习笔记】——EJB开发环境搭建(Eclipse集成JBoss)
- poj 3687 Labeling Balls【拓扑排序 输出元素在拓扑序列中的位置】
- 目标
- 【转载】Java中Properties类的操作
- greenDAO讲义(一):使用篇
- 【android】小练手app及源码
- Elecator
- TQ2440 mifi驱动及开机自动连接wifi全过程
- java 多线程锁synchronized
- c++实现webservice