Labeling Balls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9028   Accepted: 2444

Description

Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 toN 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 withb".

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 nextM line each contain two integersa and b indicating the ball labeled witha must be lighter than the one labeled withb. (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 labelN. 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<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
typedef struct v{ int vex;
v *next; }V;
V*p;
typedef struct h{ int indegree;
v *next ;
}H;
int n,result[300],tot,ans[300];
priority_queue<int ,vector<int>,less<int> > q;
H team[50000];
bool visit[305][305];
void topsort()
{
int i,ci,sum,a,b;
ci=1;
for(i=1;i<=n;i++)
{ if(team[i].indegree==0)
q.push(i); }
sum=n; while(!q.empty())
{
sum--;
a=q.top();
q.pop();
result[ci++]=a;
for(p=team[a].next;p!=0;p=p->next)
{ b=p->vex;
if(--team[b].indegree==0)
q.push(b);
} }
// printf("%d\n",sum);
if(sum>0)
{
printf("-1\n");
}
else
{
int nn=n;
for(i=1;i<=n;i++)
{ ans[result[i]]=nn--;//这是排序
}
for(i=1;i<n;i++) printf("%d ",ans[i]);//这是重量
printf("%d\n",ans[i]); } }
int main()
{ int i,m,a,b,T,j; scanf("%d",&T);
while(T--)
{
while(!q.empty())
{ q.pop();
} scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{ visit[i][j]=false;
} for(i=0;i<=n;i++)
{
team[i].indegree=0;
team[i].next=NULL;
result[i]=0;
}
while(m--)
{ scanf("%d%d",&b,&a);//反向建表
if(visit[a][b])
continue;
visit[a][b]=true;
team[b].indegree++;
p=new V;
p->vex=b;
p->next=team[a].next;
team[a].next=p; } topsort(); } return 0;
}
												

poj3687的更多相关文章

  1. [poj3687]Labeling Balls_拓扑排序

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

  2. 两个很经典的拓扑排序题目POJ3687+HDU1285

    一.题目链接 POJ:http://poj.org/problem?id=3687 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1285 二.思路 这两 ...

  3. 拓扑排序+不是字典序的优先级排列(POJ3687+HDU4857)

    一.前言 在过去的一周里结束了CCSP的比赛,其中有一道题卡了我9个小时,各种调错都没法完整的调处来这题,于是痛下决心开始补题,这个是计划的一部分.事实上,基于错误的理解我写了若干发拓扑排序+字典序的 ...

  4. ACM/ICPC 之 拓扑排序-反向(POJ3687)

    难点依旧是题意....需要反向构图+去重+看题 POJ3687-Labeling Balls 题意:1-N编号的球,输出满足给定约束的按原编号排列的重量序列,如果有多组答案,则输出编号最小的Ball重 ...

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

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

  6. poj3687 拓扑序

    题意:有编号 1-n 的球,每个球的质量不同,质量从 1 到 n 不等,给出一系列比较,分别是两个编号的球的大小关系,求一个序列满足上述关系,并且从编号 1 开始依次选择可选的最小质量,输出每个球的质 ...

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

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

  8. POJ3687 Labeling Balls(拓扑)

    题目链接. 题目大意: N个球,从1-N编号,质量不同,范围1-N,无重复.给出小球间的质量关系(<), 要求给每个球贴标签,标签表示每个球的质量.按编号输出每个球的标签.如果解不唯一,按编号小 ...

  9. POJ-3687 Labeling Balls(拓扑)

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

随机推荐

  1. POJ 2947 Widget Factory (高斯消元 判多解 无解 和解集 模7情况)

    题目链接 题意: 公司被吞并,老员工几乎全部被炒鱿鱼.一共有n种不同的工具,编号1-N(代码中是0—N-1), 每种工具的加工时间为3—9天 ,但是现在老员工不在我们不知道每种工具的加工时间,庆幸的是 ...

  2. UVa 10305 (拓扑排序) Ordering Tasks

    题意: 经典的拓扑排序.有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列. 分析: 拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系.我们将“小于”这种关系看做一条有向 ...

  3. EF4.0和EF5.0增删改查写法区别

    1 public T AddEntity(T entity) 2 { 3 //EF4.0的写法 4 添加实体 5 //db.CreateObjectSet<T>().AddObject(e ...

  4. 最受Web前端开发者欢迎的五大开发工具

    工其事,必利于器.好的开发工具毋容置疑会帮助Web前端开发者事半功倍,51CTO在上期主办的技术沙龙<大型网站PHP开发之道> 对现场的百余位Web开发者做了问卷调查,后经51CTO调研小 ...

  5. [转]ASP.NET 页生命周期概述

    原文链接:http://msdn.microsoft.com/zh-cn/library/ms178472(v=vs.110).aspx  对应版本:.NET 4.0 ASP.NET 页运行时,此页将 ...

  6. apache开源项目--dbutils

    原则上这不能说是一个持久层框架,它提供了一些Jdbc的操作封装来简化数据查询和记录读取操作.本站就是采用这个项目来读写数据库,代码非常简洁,如果你厌烦了Hibernate的庞大,不妨可以试试——DbU ...

  7. ORACLE 远程连接数据库

    通过运行->cmd->sqlplus/nolog 登录sqlplus时,突然间提示“sqlplus不是内部或外部命令,也不 是可运行的程序或批处理文件”,如下图: 分析后感觉不可能啊,因为 ...

  8. win下Velocity安装和试用

    1.eclipse等就不说了 2.velocity的eclipse插件: http://www.oschina.net/p/veloeclipse(介绍) 方法1(现在基本上非常慢)http://pr ...

  9. android直接读取数据库文件

    public class Dictionary extends Activity  implements OnClickListener, TextWatcher{     private final ...

  10. java web 学习九(通过servlet生成验证码图片)

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: