poj——3687 Labeling Balls
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 14835 | Accepted: 4346 |
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 题意:有T组测试数据,每组测试数据第一行有两个数n,m。代表有n个球,接下来m行,每行有两个数a,b,代表a球比b球轻,让你从小到大输出球的重量,若是相同重量,按照字典序输出。思路:这道题可以用拓扑排序来做、、拓扑排序是一定的,但是我们的拓扑排序里面是要用到大根堆,而且我们是逆向来存的层数、、、首先显而易见是拓扑排序.但是考虑到要让前面的数尽量小。我们就不可以采用正向建图了、、 举个例子: 1 我们得到了下面这样一个图、、 5 4 正向建图得到的点的次序是1 4 5 3 2
1 4 1到n的质量为1 5 4 3 2 4 2 正确答案为1 5 3 4 2 5 3 3 2 为什么?!
我们正向建图时,1出去以后的入度为0的点只有4 5我们并没有考虑到5还指向3于是我们an小根堆先选择了4 这样就会造成错误、、、 所以我们这个地方就要反向建图,当然这也就决定了我们要用大根堆、、、、这样我们每次把标号最大的放在后面就可以很好地解决字典序这个问题
注意:我们这个地方输出的是他们的质量、、、、、
代码:
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 41000
using namespace std;
priority_queue<int>q;
int n,m,x,y,s,t,tot,sum,in[N],ans[N],head[N];
int read()
{
,f=;char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
struct Edge
{
int from,to,next;
}edge[N];
void add(int x,int y)
{
tot++;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
void begin()
{
sum=,tot=;
memset(,sizeof(in));
memset(ans,,sizeof(ans));
memset(edge,,sizeof(edge));
memset(head,,sizeof(head));
}
int main()
{
t=read();
while(t--)
{
begin();
n=read(),m=read();
;i<=m;i++)
{
x=read(),y=read();
in[x]++,add(y,x);
}
;i<=n;i++)
) q.push(i);
tot=n;
while(!q.empty())
{
x=q.top(),q.pop();ans[x]=tot;
tot--;sum++;
for(int i=head[x];i;i=edge[i].next)
{
int t=edge[i].to;
in[t]--;
) q.push(t);
}
}
if(sum!=n) printf("-1\n");
else
{
;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
}
;
}
poj——3687 Labeling Balls的更多相关文章
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- poj 3687 Labeling Balls【反向拓扑】
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12246 Accepted: 3508 D ...
- POJ 3687 Labeling Balls (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 D ...
- poj 3687 Labeling Balls - 贪心 - 拓扑排序
Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- poj 3687 Labeling Balls(拓补排序)
Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...
随机推荐
- Spring-----注解开发和Spring测试单元
一.注解开发 导入jar包;spring-aop-xxx.jar 导入约束:(在官方文档xsd-configuration.html可找) <beans xmlns="http://w ...
- Visual SVN自动给文件加锁
在使用SVN作为版本控制器的时候,在VS里安装VISUALSVN插件,当修改文件公共文件的时候需要先Get Lock,如果对于多次操作这个鼠标操作显得是一些复杂,自动给文件加锁的操作实际是给文件加一个 ...
- H.264学习笔记4——变换量化
A.变换量化过程总体介绍 经过帧内(16x16和4x4亮度.8x8色度)和帧间(4x4~16x16亮度.4x4~8x8色度)像素块预测之后,得到预测块的残差,为了压缩残差信息的统计冗余,需要对残差数据 ...
- FlowNet: Learning Optical Flow with Convolutional Networks
作者:嫩芽33出处:http://www.cnblogs.com/nenya33/p/7122701.html 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须 ...
- 【Gambit】Gambit使用教程
第一章 Gambit使用 Gambit介绍 网格的划分使用Gambit软件,首先要启动Gambit,在Dos下输入Gambit <filemane>,文件名如果已经存在,要加上参数-old ...
- leetcode_486. Predict the Winner
https://leetcode.com/problems/predict-the-winner/ 题目描述:给定一个非负的积分数组,玩家1可以从数组两端任取一个积分,接着玩家2执行同样的操作,直至积 ...
- spring的设计思想
在学习Spring框架的时候, 第一件事情就是分析Spring的设计思想 在学习Spring的时候, 需要先了解耦合和解耦的概念 耦合: 简单来说, 在软件工程当中, 耦合是指对象之间的相互依赖 耦合 ...
- Git中文书籍
Git中文书籍: http://git-scm.com/book/zh/v1
- JAVA基础——设计模式之装饰者模式
装饰模式 : 对新房进行装修并没有改变房屋的本质,但它可以让房子变得更漂亮.更温馨.更实用. 在软件设计中,对已有对象(新房)的功能进行扩展(装修). 把通用功能封装在装饰器中,用到的地方 ...
- 关于动态添加的html元素绑定的事件不生效的解决办法
1.可以通过行内添加事件的方法,比如onclick="fn()"; 在js中写好方法名对应的方法就可以了,如果绑定方法的元素太多 2.jquery的on事件绑定 //on事件可以给 ...