Labeling Balls
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:

  1. No two balls share the same label.
  2. 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)的更多相关文章

  1. POJ3687 Labeling Balls(拓扑)

    题目链接. 题目大意: N个球,从1-N编号,质量不同,范围1-N,无重复.给出小球间的质量关系(<), 要求给每个球贴标签,标签表示每个球的质量.按编号输出每个球的标签.如果解不唯一,按编号小 ...

  2. POJ-3687 Labeling Balls(拓扑)

    不一样的拓扑排序 给定一些标记为1到n的数, 求出满足a < b 的序列, 如果有多个输出, 按先标签1往前的位置, 然后按标签2往前的位置, 对于每个标签, 位置都尽量往前. 因为位置要往前, ...

  3. POJ - 3687 Labeling Balls (拓扑)

    (点击此处查看原题) 题意 此处有n盏灯,编号为1~n,每盏灯的亮度都是唯一的,且在1~n范围之间,现已知m对灯之间的关系:a b ,说明灯a的亮度比灯b小,求出每盏灯的亮度,要求字典序最小(编号小的 ...

  4. POJ 3687 Labeling Balls (top 排序)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15792   Accepted: 4630 D ...

  5. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

  6. Labeling Balls(拓扑排序wa)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12466   Accepted: 3576 D ...

  7. POJ 3687:Labeling Balls(优先队列+拓扑排序)

    id=3687">Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10178 Acc ...

  8. POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)

    题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...

  9. poj 3687 Labeling Balls(拓补排序)

    Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...

随机推荐

  1. php操作mongodb手册地址

    php操作mongodb手册地址: http://php.net/manual/zh/class.mongocollection.php

  2. Qemu/kvm虚拟化源码解析学习视频资料

    地址链接:tao宝搜索:Linux云计算KVM Qemu虚拟化视频源码讲解+实践​https://item.taobao.com/item.htm?ft=t&id=646300730262 L ...

  3. k8s使用ceph的rbd作后端存储

    k8s使用rbd作后端存储 k8s里的存储方式主要有三种.分别是volume.persistent volumes和dynamic volume provisioning. volume: 就是直接挂 ...

  4. C逗号表达式

    c语言提供一种特殊的运算符,逗号运算符,优先级别最低,它将两个及其以上的式子联接起来,从左往右逐个计算表达式,整个表达式的值为最后一个表达式的值.如:(3+5,6+8)称为逗号表达式,其求解过程先表达 ...

  5. 【Android】修改快捷键,前一步默认是Ctrl + Z,修改后一步

    我已经忘了,我什么时候已经习惯前一步是Ctrl + Z,后一步是Ctrl + Y Android Studio默认前一步快捷键是相同的,但是后一步就不是了 Ctrl + Y变成删除一行代码,就是下图D ...

  6. 银联acp手机支付总结

    总结: 1.手机调用后台服务端接口,获取银联返回的流水号tn 银联支付是请求后台,后台向银联下单,返回交易流水号,然后返回给用户,用户通过这个交易流水号,向银联发送请求,获取订单信息,然后再填写银行卡 ...

  7. 【MySQL】统计累计求和

    https://geek-docs.com/sql/sql-examples/sql-cumulative-sum.html

  8. Spring Cloud Alibaba 整合 Nacos 实现服务配置中心

    在之前的文章 <Nacos 本地单机版部署步骤和使用> 中,大家应该了解了 Nacos 是什么?其中 Nacos 提供了动态配置服务功能 一.Nacos 动态配置服务是什么? 官方是这么说 ...

  9. kubeadm安装k8s集群

    安装kubeadm kubectl kubelet 对于Ubuntu/debian系统,添加阿里云k8s仓库key,非root用户需要加sudo apt-get update && a ...

  10. 可扩展标记语言XML(淅淅沥沥的小雨)

    XML简述 XML用于描述数据,是当前处理结构化文档信息的有力工具.与操作系统编程语言的开发平台无关,可以实现不同系统之间的数据交互. xml文件结构: 1 <?xml version=&quo ...