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. Docker的基本使用及DockerFile的编写

    前言: 最近在准备面试,在复习到Docker相关内容时,想写一些东西分享给大家然后加深一下自己的印象,有了这篇随笔. Docker的简介: docker从文件系统.网络互连到进程隔离等等,极大的简化了 ...

  2. CSS区分Chrome和Firefox

    CSS区分Chrome和FireFox 描述:由于Chrome和Firefox浏览器内核不同,对CSS解析有差别,因此常会有在两个浏览器中显示效果不同的问题出现,解决办法如下: /*Chrome*/ ...

  3. flink-----实时项目---day07-----1.Flink的checkpoint原理分析 2. 自定义两阶段提交sink(MySQL) 3 将数据写入Hbase(使用幂等性结合at least Once实现精确一次性语义) 4 ProtoBuf

    1.Flink中exactly once实现原理分析 生产者从kafka拉取数据以及消费者往kafka写数据都需要保证exactly once.目前flink中支持exactly once的sourc ...

  4. linux 常用清空文件方法

    1.vim 编辑器 vim /tmp/file :1,$d  或 :%d 2.cat 命令 cat /dev/null > /tmp/file

  5. OS开发之Objective-C与JavaScript的交互

    UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS ...

  6. oracle体系结构(图)

  7. 渐进式web应用 (PWA)

    PWA(渐进式 Web 应用)运用现代的 Web API 以及传统的渐进式增强策略来创建跨平台 Web 应用程序. PWA的特点: Discoverable, 内容可以通过搜索引擎发现. Instal ...

  8. 【Linux】【Basis】【RHEL】KickStart for RHEL6.8

    1. 概念: 自动安装的脚本,这篇文章以RHEL6.8为例 kickstart for RHEL6.8官方教程:https://access.redhat.com/documentation/en-U ...

  9. redis的总结笔记

    # Redis    1. 概念: redis是一款高性能的NOSQL系列的非关系型数据库        1.1.什么是NOSQL            NoSQL(NoSQL = Not Only ...

  10. Django auth

    auth是django一个自带的用户验证系统,使用它可以减少我们的开发流程. 基本使用 大体流程: 自定义类 from django.contrib.auth.models import Abstra ...