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 ...
随机推荐
- onenet简介
物联科技|物联网开放平台探秘之移动OneNET平台(上) 物联科技 百家号17-07-1008:57 当前,国内外物联网行业发展如火如荼,众多传统行业也在谋求向物联网转型.许多电子工程师或出于个人意愿 ...
- ES6 Promise 用法转载
Promise是一个构造函数,自己身上有all.reject.resolve这几个眼熟的方法,原型上有then.catch等同样很眼熟的方法. 那就new一个 var p = new Promise( ...
- ASP.NET Easyui datagrid增删改+sqlhelper
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjAAAAFxCAYAAABtHZI7AAAgAElEQVR4nOy9DXwU1b3//wmCYHjUPl
- python--第十八天总结(Django进阶)
一.路由系统 1.每个路由规则对应一个view中的函数 1 2 3 url(r'^index/(\d*)', views.index), url(r'^manage/(?P<name>\w ...
- Vue数据交互
注意:本地json文件必须放在static目录下面,读取或交互数据前,请先安装vue-resource.点击前往 -->(vue-resource中文文档) 一.Vue读取本地JSON数据 c ...
- 26. pt-summary
pt-summary # Percona Toolkit System Summary Report ###################### Date | 2018-11-23 10:48:51 ...
- 使用Druid作为SpringBoot项目数据源(添加监控)
Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JDBC兼容数据库,包括了Oracle.MySQL.PostgreSQL.SQL Server.H2等.Druid在监 ...
- windows7 python3.4 安装scrapy
首先升级pip 虽然提示说已经升级,但是查看版本还是发现不对,随即进入C:\Python34\Lib\site-packages 查看已经存在9.0.1的版本,怎么办?将其移除,重新升级 安装lxml ...
- shapefile添加字段 设置文件名为字段内容
转眼间,这一年又结束了,再记录一点知识吧 同事说他有好多shapefile,想给每个shapefile添加一字段,并设置该字段的内容为shapefile文件名,想着用arcpy实现,于是有了下面的代码 ...
- laravel 填充器Seed
我们在进行多人开发时,发现运行php artisan db:seed --class=填充器名称 发现有报错 Exception trace: 1 ReflectionClass::__constru ...