POJ 3687:Labeling Balls(优先队列+拓扑排序)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10178 | Accepted: 2815 |
Description
Windy has N balls of distinct weights from 1 unit toN units. Now he tries to label them with 1 toN in such a way that:
- No two balls share the same label.
- The labeling satisfies several constrains like "The ball labeled witha is lighter than the one labeled withb".
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 nextM line each contain two integersa and
b indicating the ball labeled witha must be lighter than the one labeled withb. (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 labelN. 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
在主要的拓扑排序的基础上又添加了一个要求:编号最小的节点要尽量排在前面;在满足上一个条件的基础上,编号第二小的节点要尽量排在前面;
在满足前两个条件的基础上。编号第三小的节点要尽量排在前面……依此类推。点击打开链接又是看结题报告。。。
。哎。。
。。
太弱了
第一百篇。。留念。。。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector> using namespace std; const int M = 250 ;
int t, n, m;
int outint[M];
int out[M];
int in[M];
int cut;
vector<int>amap[M];
int flag; bool toposort()
{
cut = 0;
priority_queue<int>que;
for(int i=1; i<=n; i++)
if( !in[i] )
que.push(i);
while( !que.empty() )
{
int u = que.top();
que.pop();
outint[ cut++] = u;
for( int i=0; i<amap[u].size(); i++ )
{
int v = amap[u][i];
if( --in[v]==0 )
que.push(v);
}
}
if( cut<n )
return false;
else
return true;
} int main()
{
scanf( "%d", &t );
while( t-- )
{
memset( in, 0, sizeof(in) );
scanf( "%d%d", &n, &m );
for( int i=1; i<=n; i++ )
amap[i].clear();
for( int i=1; i<=m; i++ )
{
int a, b;
scanf( "%d%d", &a, &b );
amap[b].push_back(a);
in[a]++;
}
if( !toposort() )
printf("-1\n");
else
{
for( int i=0; i<n; i++ )
out[ outint[i] ] = n-i;
for(int i=1; i<=n; i++)
{
if(i<n)
printf( "%d ", out[i] );
else
printf( "%d\n", out[i] );
}
}
} return 0;
}
POJ 3687:Labeling Balls(优先队列+拓扑排序)的更多相关文章
- POJ 3687 Labeling Balls【拓扑排序 优先队列】
题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.cs ...
- 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 (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 D ...
- POJ - 3687 Labeling Balls (拓扑)
(点击此处查看原题) 题意 此处有n盏灯,编号为1~n,每盏灯的亮度都是唯一的,且在1~n范围之间,现已知m对灯之间的关系:a b ,说明灯a的亮度比灯b小,求出每盏灯的亮度,要求字典序最小(编号小的 ...
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- 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(拓补排序)
Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...
随机推荐
- centos 资源链接
不时更新.. 安装.启动相关 自动安装的精简的ISO 教你制作属于自己的CentOS 6.4一键自动化安装ISO镜像光盘 initrd介绍 理解 vmlinuz, initrd 和 System.ma ...
- 【计算机网络】HTTP协议详解
详见:http://blog.csdn.net/gueter/article/details/1524447 不让转载,但写得很好
- AC日记——[Wc2008]游览计划 bzoj 2595
2595 思路: 状压DP+spfa转移+dfs输出路径: 或者,斯坦纳树算法模板: 来,上代码: #include <queue> #include <cstdio> #in ...
- easyui常用
清空列表选中项 //easyui datagrid 清空勾选 $('#List').datagrid('clearSelections'); 扩展修改查询方法 function QueryList() ...
- Flask插件系列之flask_celery
现在继续学习在集成的框架中如何使用celery. 在Flask中使用celery 在Flask中集成celery需要做到两点: 创建celery的实例对象的名字必须是flask应用程序app的名字,否 ...
- react 如何处理页面加载时无法将获取缓存信息存入全局变量中
最近在做一个权限功能时,发现在读取用户公司ID进行列表查询 时,无法钭读取到缓存中的数据存入页面全局变量中进行加载查询 将问题代码整理出来 将信息存入缓存: let menuList = Helper ...
- 牛客练习赛16 B 漂亮的树【哈希hash/思维】
链接:https://www.nowcoder.com/acm/contest/84/B 来源:牛客网 题目描述 街上有n棵树,标号为1...n,第i棵树的高度为ai. 定义这n棵树是漂亮的,当且仅当 ...
- SSO [ OAuth2.0 ]
1) SSO英文全称Single Sign On,单点登录. SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 它包括可以将这次主要的登录映射到其他应用中用于同一个用户的 ...
- 深入理解MySQL中的Redo、Undo、MVCC
http://edu.csdn.net/course/detail/3495 http://edu.csdn.net/courses/o317_a3/云计算大数据
- 前端福利:一套UI框架
阅读目录 背景 预览效果 表单组件 普通文本框 多行文本框 复选框 切换滑块 单选框 下拉选择框 数字输入框 时间选择 文件选择 显示文本 按钮 开源地址 背景 目前市场上有很多表单美化的UI,做的都 ...