poj 3687--Labeling Balls
2013-04-03 12:45
351 查看
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
View Code
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 只能说这题的样例数据太强了- -就算你用正向拓扑答案也和样例一样。。就这么被光荣的误导了。。这题还有一个很大的bug就是 最后输出的是每个编号球的重量而不是编号的序号- -。。。虽然我没犯这个错误。。但我也没想到要用逆向拓扑、还是看了别人的 blog才知道的。唉。长见识了。。
View Code
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; struct edge { int v; int next; }e[210*210]; int cnt; int map[210][210],first[210]; int in[210],n,m,rank[210]; void add(int u,int v) { e[cnt].v=v; e[cnt].next=first[u]; first[u]=cnt++; } int Topo() { int i,j; for(i=n;i>=1;i--) { for(j=n;j>=1;j--) { if(in[j]==0) { in[j]=-1; rank[j]=i; break; } } if(j==0) return 0; for(j=first[j];j!=-1;j=e[j].next) in[e[j].v]--; } return 1; } int main() { int i,j,t,u,v; scanf("%d",&t); while(t--) { cnt=1; scanf("%d%d",&n,&m); memset(in,0,sizeof(in)); memset(map,0,sizeof(map)); memset(first,-1,sizeof(first)); while(m--) { scanf("%d%d",&u,&v); if(!map[v][u]) { add(v,u); map[v][u]=1; in[u]++; } } if(Topo()) { printf("%d",rank[1]); for(i=2;i<=n;i++) printf(" %d",rank[i]); printf("\n"); } else printf("-1\n"); } return 0; }
相关文章推荐
- poj3687——Labeling Balls(拓扑排序)
- poj 3687 Labeling Balls
- POJ 3687 Labeling Balls (拓扑排序)
- POJ 3687 Labeling Balls
- Labeling Balls(poj 3687)
- 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
- poj 3687 Labeling Balls【反向拓扑】
- [ACM] POJ 3687 Labeling Balls (拓扑排序,逆向建边)
- poj 3687 Labeling Balls(拓扑排序)
- POJ 3687 Labeling Balls 拓扑排序