poj 3687 Labeling Balls(拓补排序)
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
题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小。(先保证1号球最轻,其次2号……)
这道题每次输入a,b的时候表示的是编号为a的球比编号为b的球轻,最后输出的是从编号 1到编号 n每个小球的重量,如果存在多组解,输出使最小重量尽量
排在前边的那组解,亦即 所有解中 1到 n号球的重量的字典序最小。。。。所以说最后重量 1 到 n 的球的标号的字典序最小的做法是不对的。
分析:拓扑排序,注意根据题的要求,要先保证1号球最轻,如果我们由轻的向重的连边,然后我们依次有小到大每次把重量分给一个入度为0的点,那么在拓扑时我们面对多个入度为0的点,我们不知道该把最轻的分给谁才能以最快的速度找到1号(使1号入度为0),并把当前最轻的分给1号。所以我们要由重的向轻的连边,然后从大到小每次把一个重量分给一个入度为0的点。这样我们就不用急于探求最小号。我们只需要一直给最大号附最大值,尽量不给小号赋值,这样自然而然就会把轻的重量留给小号。
注意重边。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxm=;
int rd[maxm];
bool g[maxm][maxm],vis[maxm];
int answer[maxm];
int t,n,m;
void tupu()
{
if(m==)
{
for(int i=; i<n; i++)
cout<<i<<' ';
cout<<n<<endl;
return ;
}
memset(vis,false,sizeof(vis));
memset(answer,,sizeof(answer));
int k;
for(int i=n; i>; i--)
{
k=-;
for(int j=n; j>; j--)
{
if(!vis[j]&&rd[j]==) //如果没有点为被访问过且入度为0的点
{
k=j; //记录下标
break;
}
}
if(k==-) //若没有入度为0的点
{
cout<<"-1"<<endl;
return ;
}
vis[k]=true;
answer[k]=i;
for(int j=; j<=n; j++)
{
if(g[k][j])
rd[j]--;
}
}
for(int i=; i<n; i++)
cout<<answer[i]<<" ";
cout<<answer[n]<<endl;
return ;
}
int main()
{
int a,b;
cin>>t;
while(t--)
{
memset(g,false,sizeof(g));
memset(rd,,sizeof(rd));
cin>>n>>m;
for(int i=; i<=m; i++)
{
cin>>a>>b;
if(!g[b][a])
rd[a]++; //重量轻的入度加一
g[b][a]=true;
}
tupu();
}
return ;
}
poj 3687 Labeling Balls(拓补排序)的更多相关文章
- 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(拓扑排序)题解
Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...
- POJ 3687 Labeling Balls【拓扑排序 优先队列】
题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.cs ...
- [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: 12246 Accepted: 3508 D ...
- poj——3687 Labeling Balls
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14835 Accepted: 4346 D ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
随机推荐
- 使用phxpaxos开发过程中遇到的坑
1. 开启BatchPropose后,状态机使用ExecuteForCheckpoint生成快照要注意: ExecuteForCheckpoint中的InstanceID不能立即持久化. 例如: 当i ...
- 左手是“Python”的身体,右手是“R”的灵魂,你爱哪个?
来源商业新知网,原标题:你爱 “Python”的身体,还是“R”的灵魂? 数据科学界有三大宝: Python.SAS和R,不过像SAS这种高端物种,不是我们这些平民能供养得起的啊. 根据 IEEE S ...
- 关于SQL 导出脚本失败 及SQL 的重装
说点题外话 最近跳到一家新公司 薪资比较客观 但是技术可能不太尽如人意 而且对.Net方向的开发好像不是很友好 自己也不知道该怎么办 一个人大老远跑这边来 附近也没有什么.net的公司和职位 房子租 ...
- 常用Macro
[enable_if] SFINAE是英文Substitution failure is not an error的缩写. 这句话什么意思呢?当调用模板函数时编译器会根据传入参数推导最合适的模板函数, ...
- Python学习—基础篇之基本数据类型(二)
Python中重要的数据结构 1.列表 2.元组 3.字典 4.集合 列表 1.创建列表 # 方式一 name = [] print(type(name)) # 执行结果 >>> & ...
- Django之CBV\FBV
FBV(function base views) 就是在视图里使用函数处理请求. 写一个FBV的实例:在views中代码 :就是写了个函数 def book(request): ''' :param ...
- 2. 2A03简介
2A03简介 1.CPU 1.1 内部寄存器 1.累加寄存器A(Accumulator):8位寄存器,用于同算术逻辑单元(ALU)共同完成各种算术逻辑运算,它既为ALU提供原始操作数又担任存放ALU运 ...
- ROS零门槛学渣教程系列(一)——ubuntu安装
本教程使用虚拟机安装ubuntu 实验前准备:下载ubuntu系统镜像 本教程使用的是ubuntu14.04lts版本,有能力的读者可自行下载安装. 推荐使用本人制作的镜像,该镜像已安装好ROS.和配 ...
- python简单实现tftp客户端(基于udp)
TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂.开销不大的文件传输服务 ...
- itchat 报错 OSError: [WinError -2147221003] 找不到应用程序: 'QR.png'
OSError: [WinError -2147221003] 找不到应用程序: 'QR.png' 原因: 缺少在windows 下相关处理方法 解决方法:找到你运行环境C:\Python36\L ...