Labeling Balls

Time Limit: 1000MS
Memory Limit: 65536K

Total Submissions: 13201
Accepted: 3811

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:

  1. No two balls share the same label.
  2. 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, bN) 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
 

题目链接:

http://poj.org/problem?id=3687
 

题意:,有N个质量不同的球,分别为1~N.每个测试数据a,b表示两个球的质量a<b.输出编号1~N的球的质量.如果存在多个方案,则输出编号为1的球质量最轻的方案,如果还是有多个方案,则输出编号为2得球质量最轻的方案,以此类推.

 

思路:输出的是质量,而不是编号.所以,输出的是编号所在的位置.这道题目用的是逆拓扑排序.先确定哪些球的入出为0,说明他们的质量是最大的一批,选取其中编号最大的球最为质量最大的球,重复此步骤.最后输出1~N号球的质量.

例:

不考虑输出要求。进行拓扑排序的结果(序号)

  • 1   4   5   3   2
  • 1   5   3   4   2
  • 1   5   4   3   2
  • 5   1   3   4   2
  • 5   1   4   3   2
  • 5   3   1   4   2

这所有的情况,第2种符合题目要求。那么输出为1 5 3 4 2(这里并不是序号,而是质量,即每个序号所在的位置)。


 代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,num;
int sign[];
int in[],out[];
int indegree[][],outdegree[][];
struct node
{
int p,m;
} ans[];
int cmp(node a,node b)
{
return a.p<b.p;
}
int Topsort()
{
int i,j,t;
num=;
for(t=; t<n; t++)
{
for(i=n; i>=; i--)
{
if(sign[i]==&&out[i]==)
break;
}
if(i<=) return -;
else
{
sign[i]=;
ans[num].p=i;
ans[num].m=n-(num++);
for(j=; j<in[i]; j++)
out[indegree[i][j]]--;
}
}
return ;
}
int main()
{
int i,T,u,v;
cin>>T;
while(T--)
{
cin>>n>>m;
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(indegree,,sizeof(indegree));
memset(outdegree,,sizeof(outdegree));
for(i=; i<m; i++)
{
scanf("%d%d",&u,&v);
if(outdegree[u][out[u]]==)outdegree[u][out[u]++]=v;
if(indegree[v][in[v]]==)indegree[v][in[v]++]=u;
}
for(i=; i<=n; i++) sign[i]=;
if(Topsort()<) cout<<"-1"<<endl;
else
{
sort(ans,ans+n,cmp);
for(i=; i<n-; i++)
cout<<ans[i].m<<" ";
cout<<ans[i].m<<endl;
}
}
return ;
}
/*
2 5 4
1 4
4 2
5 3
3 2 5 3
1 4
4 2
3 5 ans=
1 5 3 4 2
1 3 4 2 5
*/

POJ3687.Labeling Balls 拓扑排序的更多相关文章

  1. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  2. [poj3687]Labeling Balls_拓扑排序

    Labeling Balls poj-3687 题目大意:给出一些球之间的大小关系,求在满足这样的关系下,编号小的尽量比编号大的球的方案. 注释:1<=N(球的个数)<=200,1< ...

  3. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

  4. PKU 3687 Labeling Balls(拓扑排序)

    题目大意:原题链接 给出N个未编号的质量各不相同的球,以及它们质量轻重的大小关系,给它们从1-N贴标签编号,无重复.问是否存在可行的编号方法,不存在输出-1, 如果存在则输出唯一一种方案,此方案是使得 ...

  5. Java实现Labeling Balls(拓扑排序的应用)

    1 问题描述 给出一些球,从1N编号,他们的重量都不相同,也用1N标记加以区分(这里真心恶毒啊,估计很多WA都是因为这里),然后给出一些约束条件,< a , b >要求编号为 a 的球必须 ...

  6. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  7. POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)

    题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...

  8. POJ-3687 Labeling Balls(拓扑)

    不一样的拓扑排序 给定一些标记为1到n的数, 求出满足a < b 的序列, 如果有多个输出, 按先标签1往前的位置, 然后按标签2往前的位置, 对于每个标签, 位置都尽量往前. 因为位置要往前, ...

  9. POJ3687 Labeling Balls

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13645   Accepted: 3955 Description Wind ...

随机推荐

  1. 并发基础(三) java线程优先级

      在不同的JVM中(JVM也算是一个操作系统),有着不同的CPU调度算法,对于大部分的JVM来说,优先级也是调度算法中的一个参数.所以,线程优先级在一定程度上,对线程的调度执行顺序有所影响,但不能用 ...

  2. Tensorflow线程和队列

    读取数据 小数量数据读取 这仅用于可以完全加载到存储器中的小的数据集有两种方法: 存储在常数中. 存储在变量中,初始化后,永远不要改变它的值. 使用常数更简单一些,但是会使用更多的内存,因为常数会内联 ...

  3. linux获取日志指定行数范围内的内容

    假如我要获取“浅浅岁月拂满爱人袖”到“落入凡尘伤情着我”之间的内容. 1.首先得到他们所在的行号: -n选项显示行号 但是有时候grep后显示的是“匹配到二进制文件(标准输入)”,说明搜索的字符串在某 ...

  4. shiro 与spring的集成

    1.导入spring与shiro的jar包 2.在web.xml 文件中配置shiro的shiroFilter <filter> <filter-name>shiroFilte ...

  5. bat 笔记 二

    @echo off echo 等待 ping -n 5 127.1 >null cls echo 我爱嘉怡 pause >null 第一条利用echo关键字关闭路径面板 第二条利用echo ...

  6. 15 MySQL--索引

    索引: http://www.cnblogs.com/linhaifeng/articles/7356064.html http://www.cnblogs.com/linhaifeng/articl ...

  7. iphone splash screen

    https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articl ...

  8. ubuntu ftp 建立匿名用户 [转]

    转自:http://www.cnblogs.com/cocoajin/p/3761414.html ubuntu server vsftpd 匿名用户上传下载及目录设置 1:vsftpd服务器安装: ...

  9. 吴裕雄 实战PYTHON编程(4)

    import hashlib md5 = hashlib.md5()md5.update(b'Test String')print(md5.hexdigest()) import hashlib md ...

  10. Linux就业技术指导(三):IDC机房解密

    1.1 IDC机房 1.1.1 带宽计算 带宽流量计算公式: 1 Byte=8bit,1KB=1024B,1MB=1024KB,1GB=1024MB B表示Byte,工业标准是1000. 一般我们家装 ...