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 ...
随机推荐
- uniapp 标题后面紧跟一个标签的布局
使用uni-app的时候,因为想用flex布局,所以一开始就设置了全部view display为flex. 之后遇到了如下这种样式: 开始想了半天没想出来,后来想到div span有这个效果. 然后就 ...
- MFC新建工程中目录包含中文,资源文件打开失败
※尽量不适用中文,各种未知错误,嘿嘿 此方法临时解决问题,可以使程序运行,后续是否还有错误是未知数 需要修改3处位置: 1.资源文件中.rc 右键,点击“查看代码”,找到带中文的资源ID,把中文修改掉 ...
- eclipse-jee-kepler 如何设置编译compiler为1.8
最新下载了jdk1.8,想在eclipse里面用一下 jdk1.8的新特性 但是,貌似eclipse(eclipse-jee-kepler-SR2-win32-x86_64.zip)最高的编译级别为: ...
- centos7 安装mongodb
1. 创建mongodb数据,日志,配置文件存放目录# mkdir /data# tar xzf mongodb-linux-x86_64-rhel70-4.0.8.tgz# mv mongodb- ...
- JAVA8 Stream集合操作:中间方法和完结方法
StreamLambda为java8带了闭包,这一特性在集合操作中尤为重要:java8中支持对集合对象的stream进行函数式操作,此外,stream api也被集成进了collection api, ...
- Python学习—数据库篇之SQL补充
一.SQL注入问题 在使用pymysql进行信息查询时,推荐使用传参的方式,禁止使用字符串拼接方式,因为字符串拼接往往会带来sql注入的问题 # -*- coding:utf-8 -*- # auth ...
- jmeter-用户定义的变量
添加-配置元件-用户定义的变量 请求中出现变量值的位置,用${_tbip}替换 脚本执行完成,在查看结果树中debug sampler中可以看见变量名和变量值
- Failed to install gems via Bundler
问题:在执行git push heroku master时,程序报错. 解决办法: 1.bundle update 2.git add . 3.git commit -m "message& ...
- js及vue监听键盘回车事件
js document.onkeydown = (event) => { var e = event || window.event; if(e && e.keyCode==13 ...
- 【Android端】【日志收集上报SDK相关内容测试的方案梳理总结】
测试方案: 主要从几个方面关注,功能 性能 服务端策略(目前所有的这些上报收集等都会通过开关的精细化,通过接口方式将信息返回给APP端,APP端根据相关内容进行上报,因此基于此的上报机制及收集机制都需 ...