POJ3687——Labeling Balls(反向建图+拓扑排序)
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和M,N表示有N个小球,编号分别为1—N。M表示下面用M组数据(x,y)。
数据(x,y)表示小球x比小球y轻。每个小球的重量都不同,且范围是[1,n]。
输出为 编号1的小球的重量,编号2小球的重量。。。编号N小球的重量(且使编号小的小球尽量轻)。条件矛盾的话输出-1。
解题思路:
错误思路:正向建图+拓扑排序+贪心查找最小的编号 在根据编号在拓扑数组中的位置输出。(正向的贪心不能完全保证序号小的节点尽量排在前面。??)
eg:6→1→3←5←4←2
1)查找到6和2 将2加入topo数组
2)查找到6和4 将4加入topo数组
3)查找到6和5 将5加入topo数组
4)将6加入数组 将1加入数组 将3加入数组
topo数组:2 4 5 6 1 3
输出为:5 1 6 2 3 4
正确思路:反向建图+拓扑排序(尽量保证编号大的小球先确定大的重量)
eg:6→1→3←5←4←2 反向之后:6←1←3→5→4→2
1)查找到3 将3加入topo数组
2)查找到5 将5加入topo数组
3)查找到4 将4加入topo数组
4)将2加入数组 将1加入数组 将6加入数组
topo数组:6 1 2 4 5 3
输出为: 2 3 6 4 5 1
Code:
- #include<stdio.h>
- #include<string>
- #include<iostream>
- #include<cstring>
- #define MAXN 300
- using namespace std;
- int topo[MAXN+],map[MAXN+][MAXN+],dis[MAXN+];
- bool vis[MAXN+];
- int main()
- {
- int T,N,M;
- cin>>T;
- while (T--)
- {
- memset(vis,,sizeof(vis));
- memset(dis,,sizeof(dis));
- memset(topo,,sizeof(topo));
- memset(map,,sizeof(map));
- bool ok=;
- cin>>N>>M;
- int i,j;
- for (i=; i<=M; i++)
- {
- int t1,t2;
- cin>>t1>>t2;
- if (!map[t2][t1]) dis[t1]++;
- map[t2][t1]=;
- }
- for (i=N; i>=; i--)
- {
- for (j=N; j>=; j--)
- if (!dis[j]&&!vis[j])
- {
- topo[i]=j;
- vis[j]=;
- break;
- }
- if (j==) ok=;
- for (int k=; k<=N;k++)
- if (map[j][k])
- dis[k]--;
- }
- if (ok)
- {
- for (i=; i<=N; i++)
- {
- //printf("%d",topo[i]);
- for (int j=; j<=N; j++)
- {
- if (topo[j]==i)
- printf("%d",j);
- }
- if (i==N) printf("\n");
- else printf(" ");
- }
- }
- else printf("-1\n");
- }
- return ;
- }
POJ3687——Labeling Balls(反向建图+拓扑排序)的更多相关文章
- HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)
逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...
- HUD2647 Reward_反向建图拓扑排序
HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...
- 逃生 HDU 4857(反向建图 + 拓扑排序)
逃生 链接 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必 ...
- BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序
BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序 Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息 ...
- hdu 4857 逆向建图+拓扑排序 ***
题意:糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行.现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会是不平等的,这些人有的穷有 ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- poj 3683 2-sat建图+拓扑排序输出结果
发现建图的方法各有不同,前面一题连边和这一题连边建图的点就不同,感觉这题的建图方案更好. 题意:给出每个婚礼的2个主持时间,每个婚礼的可能能会冲突,输出方案. 思路:n个婚礼,2*n个点,每组点是对称 ...
- 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)
题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...
- [POI2015][bzoj4383] Pustynia [线段树优化建图+拓扑排序]
题面 bzoj权限题传送门 luogu传送门 思路 首先,这个题目显然可以从所有小的点往大的连边,然后如果没环就一定可行,从起点(入读为0)开始构造就好了 但是问题来了,如果每个都连的话,本题中边数是 ...
随机推荐
- const define 定义常量的区别
1.用const定义常量在编译的时候,提供了类型安全检查,而define 只是简单地进行字符串的替换 2.const定义的常量,会分配相应的内存空间.而define没有分配空间,只是在程序中与处理的时 ...
- 十天学会零基础入门学习Photoshop课程(在线观看)
适合人群:在校学生 在职工作者 淘宝运营者等一系列会操作电脑的人群 课程目录 试学课 课时11前言 8分钟1秒 课时22工作界面 试学课 课时33文件的新建 试学课 课时44文档保存 11分钟24秒 ...
- Linq to sql 接收存储过程返回的多个结果集
故事前提.......... 一.返回顺序结果集 存储过程实例 CREATE PROCEDURE MultipleResultTypesSequentially AS select * from pr ...
- Object-C - 类的定义
http://www.cnblogs.com/zhangweia/archive/2011/11/01/2231549.html 1. 文件分为.h:定义接口,及其属性,方法说明. .m :是实现类. ...
- WCF、Web API、WCF REST、Web Service之区别
http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-and-WCF-R ...
- AngularJS快速开始
Hello World! 开始学习AngularJS的一个好方法是创建经典应用程序“Hello World!”: 使用您喜爱的文本编辑器,创建一个HTML文件,例如:helloworld.html. ...
- jQuery实现页内查找相关内容
当需要在页面中查找某个关键字时,一是可以通过浏览器的查找功能实现,二是可以通过前端脚本准确查找定位,本文介绍通过jQuery实现的页面内容查找定位的功能,并可扩展显示查找后的相关信息. 本文以查找车站 ...
- Linux学习系列之Linux入门(一)linux安装与入门
第一篇:安装并配置Linux开发环境 一.安装linux: 主要安装Linux的发行版,到目前为之,主要的发行版有: 比较常用的是Ubuntu.redhat和centOS,主要的安装方法详细: Ubu ...
- fineui框架
http://fineui.com/demo/#/demo/layout/fit.aspx 虽然比较丑陋,但功能实用 此框架比较简单, 框架的作用你懂的,重点是要有帮助文档, 进阶型的容易上手的帮助文 ...
- Spring execution 表达式
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) th ...