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. 关于web.xml配置中的<url-pattern>

    标签<url-pattern> <url-pattern>是我们用Servlet做Web项目时需要经常配置的标签,例: <servlet><servlet-n ...

  2. Python装饰器使用规范案例详解

    由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25') ... >> ...

  3. 使用 CAST

    使用 CAST: CAST ( expression AS data_type ) 使用 CONVERT: CONVERT (data_type[(length)], expression [, st ...

  4. BFS:HDU-1072-Nightmare

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  5. HBase(0.94.5)的Compact和Split源码分析

    经过对比,0.94.5以后版本主要过程基本类似(有些新功能和细节增加) 一.       Compact 2.1.   Compact主要来源 来自四个方面:1.Memstoreflush时:2.HR ...

  6. Android广告页循环播放

    摘要:项目要求做一个广告页,实现几秒更换一次广告页,下方还有指示第几张广告页,同样也支持手动左滑或右滑. 1.准备好粘贴5个有关广告页的类. ①BaseViewPager==>自定义高度的Vie ...

  7. 【小程序入门集锦】19,微信小程序个人帐号申请

    个人账号与企业帐号相比,缺少支付等功能,与个人订阅号类似.   小程序开放个人开发者申请注册,个人用户可访问微信公众平台,扫码验证个人身份后即可完成小程序帐号申请并进行代码开发.   下面我们就来说说 ...

  8. poj2955:Brackets

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8716   Accepted: 4660 Descript ...

  9. mongoTemplate聚合操作Demo

    package com.tangzhe.mongodb.mongotemplate; import com.mongodb.BasicDBObject; import com.mongodb.DBOb ...

  10. 谋哥:《App自推广》连载2直立人行走迁徙

    [谋哥每天一干货,第六十九篇] 前篇说到声音在远古时代,是一个神奇的东西,它能够很快地把信息传播到其他地方,突破了短距离.然而能人的后代直立人学会了直立行走,他们开始走出非洲,到达遥远的中东.中国,还 ...