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. cf550D. Regular Bridge(构造)

    题意 给出一个$k$,构造一个无向图,使得每个点的度数为$k$,且存在一个桥 Sol 神仙题 一篇写的非常好的博客:http://www.cnblogs.com/mangoyang/p/9302269 ...

  2. MSBuild常用方法

    打包后把nuget包复制到指定的目录 <Target Name="CopyPackage" AfterTargets="Pack"> <Cop ...

  3. linux常用指令学习记录

    前言 本文主要为学习贴,用来记录一些 linux上的常用指令 以供参考. 文件内容查看 cat 从上往下阅读文件内容 cat [-AbEnTv] ${FILE_NAME) cat -n /etc/is ...

  4. node操作mogondb数据库的封装

    注:摘自网络 上面的注释都挺详细的,我使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关于mongoose的安装就是 npm install -g mo ...

  5. 【Linux】安装mysql之设置远程访问权限

    最近重装了云主机,又要安装各种东西,其中一个就要设置mysql权限 出于学习方便,我在自己的云主机上安装的是phpstudy集成环境,所以要进入mysql控制台不能直接用“mysql -u root ...

  6. 在windows server 2008 64位服务器上配置php环境

    1.安装windows2008 R2 46位 安装2008 R2 关键步骤,网上有很多诸如此类的安装介绍.在些南昌网站建设公司百恒网络工程师就不作详细介绍.关键是要选择适合实际应用的部署.    2. ...

  7. 基于django的个人博客网站建立(一)

    基于django的个人博客网站建立(一) 前言 网站效果可点击这里访问 之前基于hexo和github page搭建过一个博客网页,后来由于换了个系统,感觉弄的有点麻烦也就没有再去管它了,最近偶然从网 ...

  8. Elizabeth Taylor【伊丽莎白·泰勒】

    Elizabeth Taylor People fell in love with Elizabeth Taylor in 1944, when she acted in the movie Nati ...

  9. Codeforces Round #271 (Div. 2) D Flowers【计数dp】

    D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input ...

  10. CentOS6.5生产环境系统安装

    CentOS 6.5系统安装 1-1 将预先准备的CentOS 6.5安装光盘插入光驱中,开机/重启系统时,系统会进行自检,自检完毕就会出现安装系统时的引导界面,如图1-1所示.1-2 使用键盘方向键 ...