Labeling Balls(poj3687)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13109 | Accepted: 3782 |
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
思路:拓扑排序,优先队列。
开始时我是正向建边,然后拓扑排序,贪心去当前度为0的最小的点WA
后来想想,因为要保证1,2,。。。要最优,也就是要先找到1,那么在1前面度为0的点肯定在1之前。那么用上面这种贪心,我们会先选1,然后
4然后5,然后才是3,由于我们要先贪心找到2,所以这种策略就不对了,那么我们还是从正向模拟一下,1,5,3,4,2,这时正向走就是按每次最小所以我们的先找到2然后这时
这时2前面的数已经取出,但我们不知道在2出来前咋对前面的数进行选取。我们思考这么一种策略,我们建反向边,我们可以知道如果这时有入度为0的点,那么我们选取最大的
入度为0的点,这时这个点肯定是最后一个,那些在它下一层的那些点肯定排在它的前面,同时比它小和他同一层的点也要排在它的前面,所以每次都这样,选最大的可以保证最优。
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<vector>
7 using namespace std;
8 typedef struct pp {
9 int x;
10 bool operator<(const pp &n)const {
11 return n.x>x;
12 }
13 } ss;
14 vector<int>vec[300];
15 priority_queue<ss>que;
16 int ans[300];
17 int bb[300];
18 int aa[300];
19 int ma[300][300];
20 int main(void) {
21 int i,j,k;
22 int p,q;
23 scanf("%d",&k);
24 while(k--) {
25 for(i=0; i<300; i++) {
26 vec[i].clear();
27 }
28 while(!que.empty())que.pop();
29 memset(ma,0,sizeof(ma));
30 memset(ans,0,sizeof(ans));
31 scanf("%d %d",&p,&q);
32 int ff=0;
33 while(q--) {
34 int x,y;
35 scanf("%d %d",&x,&y);
36 if(x==y)ff=1;
37 {
38 vec[y].push_back(x);
39 ans[x]++;
40 ma[y][x]=1;
41 }
42 }
43 if(ff)printf("-1\n");
44 else {
45 int flag=0;
46 for(i=1; i<=p; i++) {
47 if(ans[i]==0) {
48 ss g;
49 g.x=i;
50 que.push(g);
51 }
52 }
53 while(!que.empty()) {
54 ss fg=que.top();
55 int c=fg.x;
56 bb[flag++]=c;
57 que.pop();
58 for(i=0; i<vec[c].size(); i++) {
59 int uu=vec[c][i];
60 ans[uu]--;
61 fg.x=uu;
62 if(ans[uu]==0) {
63 que.push(fg);
64 }
65 }
66 }
67 if(flag==p) {
68 for(i=0; i<p; i++) {
69 aa[bb[i]]=p-i;
70 }
71 printf("%d",aa[1]);
72 for(i=2; i<=p; i++) {
73 printf(" %d",aa[i]);
74 }
75 printf("\n");
76 } else printf("-1\n");
77 }
78 }
79 return 0;
80 }
Labeling Balls(poj3687)的更多相关文章
- POJ3687 Labeling Balls(拓扑)
题目链接. 题目大意: N个球,从1-N编号,质量不同,范围1-N,无重复.给出小球间的质量关系(<), 要求给每个球贴标签,标签表示每个球的质量.按编号输出每个球的标签.如果解不唯一,按编号小 ...
- POJ-3687 Labeling Balls(拓扑)
不一样的拓扑排序 给定一些标记为1到n的数, 求出满足a < b 的序列, 如果有多个输出, 按先标签1往前的位置, 然后按标签2往前的位置, 对于每个标签, 位置都尽量往前. 因为位置要往前, ...
- POJ - 3687 Labeling Balls (拓扑)
(点击此处查看原题) 题意 此处有n盏灯,编号为1~n,每盏灯的亮度都是唯一的,且在1~n范围之间,现已知m对灯之间的关系:a b ,说明灯a的亮度比灯b小,求出每盏灯的亮度,要求字典序最小(编号小的 ...
- POJ 3687 Labeling Balls (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 D ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
- Labeling Balls(拓扑排序wa)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12466 Accepted: 3576 D ...
- POJ 3687:Labeling Balls(优先队列+拓扑排序)
id=3687">Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10178 Acc ...
- POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)
题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到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 ...
随机推荐
- 【模板】单源最短路径(Dijkstra)/洛谷P4779
题目链接 https://www.luogu.com.cn/problem/P4779 题目大意 给定一个 \(n\) 个点 \(m\) 条边有向图,每个点有一个非负权值,求从 \(s\) 点出发,到 ...
- Elasticsearch中关于transform的一个问题?
背景:现在有一个业务,派件业务,业务员今天去派件(扫描产生一条派件记录),派件可能会有重复派件的情况,第二天再派送(记录被更新,以最新的派件操作为准).现在需要分业务员按天统计每天的派件数量.es版本 ...
- day08 索引的创建与慢查询优化
day08 索引的创建与慢查询优化 昨日内容回顾 视图 视图:将SQL语句查询结果实体化保存起来,方便下次查询使用. 视图里面的数据来源于原表,视图只有表结构 # 创建视图 create view 视 ...
- 数仓day03-----日志预处理
1. 为什么要构建一个地理位置维表(字典) 在埋点日志中,有用户的地理位置信息,但是原始数据形式是GPS坐标,而GPS坐标在后续(地理位置维度分析)的分析中不好使用.gps坐标的匹配,不应该做这种精确 ...
- 转 GSON
转 https://www.jianshu.com/p/75a50aa0cad1 GSON弥补了JSON的许多不足的地方,在实际应用中更加适用于Java开发.在这里,我们主要讲解的是利用GSON来操作 ...
- Linux基础命令---apachectl
apachectl apachectl指令是apache http服务器的前端控制程序,可以协助控制apache服务的守护进程httpd. 此命令的适用范围:RedHat.RHEL.Ubuntu.Ce ...
- soapui pro 5.1.2 的破解方法
Protection-4.6,和scz.key这两个文件能破解5.1.2的SoapUI 的Pro版本,mac 和 windows均可.1.拷贝Protection-4.6.jar到soapui安装的l ...
- easyhadoop 安装
ldconfig deferred processing now taking place正在处理用于 libapache2-mod-php5 的触发器... * Reloading web serv ...
- JS 的三种定义变量 var let const
Let 只在 let 命令所在的代码块内有效,在外就会报错 Let 是块级作用域,函数内部使用let定义后,对函数外部无影响 Let/const 不存在变量提升,使用前一定要声明后,在使用,否则会报错 ...
- Consumer方法结合Lambda表达式的应用
package com.itheima.demo05.Consumer;import java.util.function.Consumer;/** * @author newcityman * @d ...