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 ...
随机推荐
- 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树
题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...
- tab菜单的点击的动态效果和内容页面的关联显示jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Http请求 GET和POST,405错误
我就简单说吧,在用SringMVC时,我们通常会用到 @RequestMapping(value="/test",method=RequestMethod.GET) public ...
- 【Ecshop】将内置的 FCkeditor 更换为 UEditor
1.下载UE,解压到includes/,更名目录名为ueditor 注意更改配置后端文件上传路径,参考文档 2.修改admin/includes/lib_main.php,添加 /** * 生成编辑器 ...
- Benelux Algorithm Programming Contest 2014 Final
// Button Bashing (bfs) 1 #include <iostream> #include <cstdio> #include <cstring> ...
- 【Todo】 cygwin下emacs中M-x shell 中出现乱码
- 自己用C语言写RH850 F1KM serial bootloader
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 手上有块Renesas ...
- TCP/IP网络编程之优于select的epoll(二)
基于epoll的回声服务端 在TCP/IP网络编程之优于select的epoll(一)这一章中,我们介绍了epoll的相关函数,接下来给出基于epoll的回声服务端示例. echo_epollserv ...
- IT帮2019年2月线下活动【定义工作,解读自我】之站桩练习
2019年2月IT帮线下活动[定义工作,解读自我] 昨天的活动收获很大,全面的总结周老师会另写一篇来帮助大家回顾.我想说一下其中最打动我的一句话:“只有你能决定你有多优秀!” “工作中把自己当成企业家 ...
- MySQL基础7-分页查询
1.分页查询(MySQL特有的,oracle中没有) 栗子1: 每页最多3条记录:pageSize=3:第一页:SELECT * FROM product LIMIT 0,3第二页:SELECT * ...