POJ3687Labeling Balls
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14278 | Accepted: 4162 |
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 题意:
这道题每次输入a,b的时候表示的是编号为a的球比编号为b的球轻,最后输出的是从编号 1
到编号 n每个小球的重量,如果存在多组解,输出使最小重量尽量排在前边的那组解,亦即 所有解中 1到 n号球的重量的字典序最小
/*
解题思路:
将输入的n行建立有向图,如果该图产生回路,那么输出-1,如果不产生回路,将入度为0的点放进优先队列中,
优先队列从大到小排序,每个队列按顺序出队,当出队的数与其他点有边存在,就在相应该点的入度减一,
然后在判断是否入度为0,如果为0再次入队。本题一个比较容易出错的就是判重,如果输入两个相同的a和b,
那么如果没有判重将会输出0,判重就在输入边时判断该边是否已经存在,如果存在该边的入度就不在自加。
两组比较好的测试案例:
第二个测试案例有5个,但是有2个一样的,所以按 4 个算
2 5 4
5 1
4 2
1 3
2 3 10 5
4 1
8 1
7 8
4 1
2 8 ans:
2 4 5 3 1 逆向建图
5 1 6 2 7 8 3 4 9 10 没有判重边的话就输出 -1
典型的拓扑排序满足不了题目小号排前的要求,可以采用反向拓扑排序,加优先队列完成。
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<queue> using namespace std; int g[][];
int degree[];//入度
int value[];
priority_queue<int> q;//定义优先队列 //得到入度为0的点
int toposort(int n)
{
int j=n;
for(int i=;i<=n;i++)
{
if(degree[i]==)
{
q.push(i);
}
}
if(q.empty())
return ;
while(!q.empty())
{
int t = q.top();
q.pop();
value[t]=j;
j--;
for(int i=;i<=n;i++)
{
if(g[i][t]!=)
{
g[i][t]=;
degree[i]--;
if(degree[i]==)
q.push(i);
}
}
}
if(j!=)
return ;
return ;
} int main()
{
int T;
int n,m;
int a,b;
scanf("%d",&T);
while(T--)
{
memset(g,,sizeof(g));
memset(degree,,sizeof(degree));
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&a,&b);
if(g[a][b]>) //判重,如果输入一样的那么只算一个
degree[a]--;
g[a][b]=; //a到b的边,起点a,终点b的边
degree[a]++; //反向建图
}
int x=toposort(n);
if(x==)
printf("-1\n");
else
{
for(int i=;i<n;i++)
printf("%d ",value[i]);
printf("%d\n",value[n]);
}
}
return ;
}
心若向阳,无言悲伤
POJ3687Labeling Balls的更多相关文章
- ACM/ICPC 之 拓扑排序-反向(POJ3687)
难点依旧是题意....需要反向构图+去重+看题 POJ3687-Labeling Balls 题意:1-N编号的球,输出满足给定约束的按原编号排列的重量序列,如果有多组答案,则输出编号最小的Ball重 ...
- Codeforces554 C Kyoya and Colored Balls
C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- 13 Balls Problem
今天讨论的是称球问题. No.3 13 balls problem You are given 13 balls. The odd ball may be either heavier or ligh ...
- Open judge C16H:Magical Balls 快速幂+逆元
C16H:Magical Balls 总时间限制: 1000ms 内存限制: 262144kB 描述 Wenwen has a magical ball. When put on an infin ...
- hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...
- hdu 3635 Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...
- HDU 5810 Balls and Boxes(盒子与球)
Balls and Boxes(盒子与球) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
随机推荐
- jQuery图片延迟加载插件jQuery.lazyload 的使用
使用方法 引用jquery和jquery.lazyload.js到你的页面 1 2 <script src="jquery-1.11.0.min.js"></sc ...
- Gym - 101670F Shooting Gallery(CTU Open Contest 2017 区间dp)
题目&题意:(有点难读...) 给出一个数字序列,找出一个区间,当删除这个区间中的两个相同的数字后,只保留这两个数字之间的序列,然后继续删除相同的数字,问最多可以实行多少次删除操作. 例如: ...
- CCF201503-2 数字排序 java(100分)
试题编号: 201503-2 试题名称: 数字排序 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出. 输 ...
- Python supprocess模块
当我们需要调用系统的命令的时候,最先考虑的os模块.用os.system()和os.popen()来进行操作.但是这两个命令过于简单,不能完成一些复杂的操作,如给运行的命令提供输入或者读取命令的输出, ...
- Spring 获取当前activeProfile
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext()) ...
- 使用pycharm学习django纪实
之前已经下了python3.7 首先官网下载pycharm专业版,然后利用学生邮箱激活 新建django项目,使用虚拟环境就好了,方便之后的服务器部署(大概 找到博客开始学习:https://blog ...
- 常州模拟赛d8t1 友好数对
分析:其实就是问你有多少对a,b有且仅有两位不相同.我们可以先枚举这两位,对应ai枚举一位,对应bi枚举一位,如果ai^(1<<x) == bi^(1<<y),证明恰好有两位不 ...
- JS中showModalDialog 详细使用方法
基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.showModalDialog() 方法用来创建一个 ...
- BZOJ(4) 1050: [HAOI2006]旅行comf
1050: [HAOI2006]旅行comf Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3984 Solved: 2236[Submit][St ...
- 9、Java并发性和多线程-线程安全与共享资源
以下内容转自http://ifeve.com/thread-safety/: 允许被多个线程同时执行的代码称作线程安全的代码.线程安全的代码不包含竞态条件.当多个线程同时更新共享资源时会引发竞态条件. ...