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

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, 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

Source

输出满足所给限制条件的小球编号,要求靠前的球编号尽可能小。

转换思路,将靠后的球标记得尽可能大。根据题目关系反向建边,做类似拓扑排序的操作即可。(不能像一般的拓扑排序一样,将入度为0的点压进栈挨个处理,而应每次找入度为0的最靠后的点来处理,因为每个点处理完,都可能解锁新的较靠后的点)。

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
struct edge{
int v,next;
}e[mxn];
int hd[mxn],cnt;
int in[mxn];
int ans[mxn];
int n,m;
void add_edge(int u,int v){
e[++cnt].v=v;e[cnt].next=hd[u];hd[u]=cnt;
}
int st[mxn],top;
int vis[mxn];
void tp(){
top=;
int tot=;
int i,j;
while(){
if(tot>=n)break;
for(i=n;i>=;i--){//每次找最靠后的可行点
if(!in[i] && !vis[i]){st[++top]=i,vis[i]=;break;}
}
if(!top){
printf("-1\n");
return;
}
while(top){
int u=st[top--];
for(i=hd[u];i;i=e[i].next){
in[e[i].v]--;
}
ans[u]=++tot;
}
}
for(i=;i<=n;i++){
printf("%d ",n+-ans[i]);
}
printf("\n");
return;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(e,,sizeof e);
memset(hd,,sizeof hd);
memset(in,,sizeof in);
memset(vis,,sizeof vis);
cnt=;
scanf("%d%d",&n,&m);
int i,j;int a,b;
for(i=;i<=m;i++){
scanf("%d%d",&a,&b);
add_edge(b,a);
in[a]++;
}
tp();
}
return ;
}

POJ3687 Labeling Balls的更多相关文章

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

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

  2. POJ3687.Labeling Balls 拓扑排序

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13201 Accepted: 3811 Descr ...

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

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

  4. POJ3687 Labeling Balls(拓扑)

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

  5. POJ-3687 Labeling Balls(拓扑)

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

  6. Labeling Balls(poj3687)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13109   Accepted: 3782 D ...

  7. [poj3687]Labeling Balls_拓扑排序

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

  8. POJ 3687 Labeling Balls()

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...

  9. Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...

随机推荐

  1. Spring Cloud学习介绍

    最近在学spring cloud, 整理了下 简单知识要求: 1.要了解springboot 2.了解分布式架构 3.了解微服务 4.了解springcloud是做什么的 带着这些,初学者 就至少有个 ...

  2. [Python]有关pygame库中的flip和update的区别

    pygame.display.flip()和pygame.display.update()的用法上的区别: 资料一.   资料二. (资料最后更新时间:2019年1月9日)

  3. (转)curl常用命令

    本文转自 http://www.cnblogs.com/gbyukg/p/3326825.html 下载单个文件,默认将输出打印到标准输出中(STDOUT)中 curl http://www.cent ...

  4. [jzoj5233]概率博弈(树形DP)

    Description 小A和小B在玩游戏.这个游戏是这样的: 有一棵

  5. (转) Redis哨兵的详解

    1 哨兵的作用 哨兵是redis集群架构中非常重要的一个组件,主要功能如下: 1. 集群监控:负责监控redis master和slave进程是否正常工作 2. 消息通知:如果某个redis实例有故障 ...

  6. P1616 疯狂的采药

    P1616 疯狂的采药 题目背景 此题为NOIP2005普及组第三题的疯狂版. 此题为纪念LiYuxiang而生. 题目描述 LiYuxiang是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为 ...

  7. 测试环境docker化(一)—基于ndp部署模式的docker基础镜像制作

    本文来自网易云社区 作者:孙婷婷 背景 我所在测试项目组目前的测试环境只有一套,在项目版本迭代过程中,开发或产品偶尔会在测试环境进行数据校验,QA人数在不断增加,各个人员在负责不同模块工作时也会产生脏 ...

  8. Shell脚本直接执行sql语句和不显示列名

    在shell脚本编程的时候,可以通过在mysql连接命令添加-N和-e参数实现查询结果不显示列名和直接执行sql语句操作 demo $(mysql -h ${HOST} -u ${USER} -p${ ...

  9. PHP 自定义二维码生成

    环境:PHP 7.*.* ,Composer 包管理工具.QrCode 效果如下: 使用 Composer 安装 QrCode QrCode 类库基于 php 的 GD 库,用于生成任意尺寸的二维码, ...

  10. dijkstra 堆优化

    #include <iostream> #include <vector> #include <cstring> #include <queue> us ...