POJ3687 Labeling Balls
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:
- No two balls share the same label.
- 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的更多相关文章
- POJ3687——Labeling Balls(反向建图+拓扑排序)
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
- POJ3687.Labeling Balls 拓扑排序
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13201 Accepted: 3811 Descr ...
- POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)
题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...
- POJ3687 Labeling Balls(拓扑)
题目链接. 题目大意: N个球,从1-N编号,质量不同,范围1-N,无重复.给出小球间的质量关系(<), 要求给每个球贴标签,标签表示每个球的质量.按编号输出每个球的标签.如果解不唯一,按编号小 ...
- POJ-3687 Labeling Balls(拓扑)
不一样的拓扑排序 给定一些标记为1到n的数, 求出满足a < b 的序列, 如果有多个输出, 按先标签1往前的位置, 然后按标签2往前的位置, 对于每个标签, 位置都尽量往前. 因为位置要往前, ...
- Labeling Balls(poj3687)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13109 Accepted: 3782 D ...
- [poj3687]Labeling Balls_拓扑排序
Labeling Balls poj-3687 题目大意:给出一些球之间的大小关系,求在满足这样的关系下,编号小的尽量比编号大的球的方案. 注释:1<=N(球的个数)<=200,1< ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...
随机推荐
- 5- vue django restful framework 打造生鲜超市 -完成商品列表页(上)
使用Python3.6与Django2.0.2(Django-rest-framework)以及前端vue开发的前后端分离的商城网站 项目支持支付宝支付(暂不支持微信支付),支持手机短信验证码注册, ...
- 第五篇:selenium调用IE问题(Protected Mode settings are not the same for all zones)
代码信息: driver = webdriver.Ie()driver.get('http://www.baidu.com') 问题描述: raise exception_class(message, ...
- php与js的crc32(支持中文)
代码: <?php function myCrc32($string, $crc = 0) { $table = "00000000 77073096 EE0E612C 990951B ...
- win7旗舰版64位java的jdk环境变量的配置(2012-12-26-bd 写的日志迁移
首先到oracle的官方网站http://www.oracle.com/technetwork/cn/java/javase/downloads/index.html下个JDK比如下图: 必须是win ...
- Air Pollution【空气污染】
Air Pollution Since the 1940s, southern California has had a reputation for smog. 自20世纪40年代以来,南加利福尼亚 ...
- I Like for You to Be Still【我会一直喜欢你】
I Like for You to Be Still I like for you to be still 我会一直喜欢这你 It is as though you are absent 就算你并不在 ...
- NAND Flash和NOR Flash的比较
目前Flash主要有两种NOR Flash和NADN Flash.NOR Flash的读取和我们常见的SDRAM的读取是一样,用户可以直接运行装载在NOR FLASH里面的代码,这样可以减少SRAM的 ...
- 裸奔着造房子——对政府禁止采购Win8系统的一些看法
前段时间有消息称政府招标的项目将禁止使用Win8系统,原因是Win8系统的安全架构将有利于暴露敏感信息给微软,而微软的老子是美利坚,老子想要知道什么,儿子当然不敢不从.因此Win8也被打入冷宫,微软多 ...
- CQRS之旅——旅程5(准备发布V1版本)
旅程5:准备发布V1版本 添加功能和重构,为V1版本发布做准备. "大多数人在完成一件事之后,就像留声机的唱片一样,一遍又一遍地使用它,直到它破碎,忘记了过去是用来创造更多未来的东西.&qu ...
- HDU 5657 CA Loves Math 状压DP + 枚举
题意: 给出\(A(2 \leq A \leq 11), n(0 \leq n \leq 10^9), k(1 \leq k \leq 10^9)\). 求区间\([1, A^n]\)中各个数字互不相 ...