Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 10178
Accepted: 2815

Description

Windy has N balls of distinct weights from 1 unit toN units. Now he tries to label them with 1 toN in such a way that:

  1. No two balls share the same label.
  2. The labeling satisfies several constrains like "The ball labeled witha is lighter than the one labeled withb".

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 nextM line each contain two integersa and
b indicating the ball labeled witha must be lighter than the one labeled withb. (1 ≤
a, bN) 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 labelN. 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

在主要的拓扑排序的基础上又添加了一个要求:编号最小的节点要尽量排在前面;在满足上一个条件的基础上,编号第二小的节点要尽量排在前面;

在满足前两个条件的基础上。编号第三小的节点要尽量排在前面……依此类推。点击打开链接又是看结题报告。。。

。哎。。

。。

太弱了

第一百篇。。留念。。。


#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector> using namespace std; const int M = 250 ;
int t, n, m;
int outint[M];
int out[M];
int in[M];
int cut;
vector<int>amap[M];
int flag; bool toposort()
{
cut = 0;
priority_queue<int>que;
for(int i=1; i<=n; i++)
if( !in[i] )
que.push(i);
while( !que.empty() )
{
int u = que.top();
que.pop();
outint[ cut++] = u;
for( int i=0; i<amap[u].size(); i++ )
{
int v = amap[u][i];
if( --in[v]==0 )
que.push(v);
}
}
if( cut<n )
return false;
else
return true;
} int main()
{
scanf( "%d", &t );
while( t-- )
{
memset( in, 0, sizeof(in) );
scanf( "%d%d", &n, &m );
for( int i=1; i<=n; i++ )
amap[i].clear();
for( int i=1; i<=m; i++ )
{
int a, b;
scanf( "%d%d", &a, &b );
amap[b].push_back(a);
in[a]++;
}
if( !toposort() )
printf("-1\n");
else
{
for( int i=0; i<n; i++ )
out[ outint[i] ] = n-i;
for(int i=1; i<=n; i++)
{
if(i<n)
printf( "%d ", out[i] );
else
printf( "%d\n", out[i] );
}
}
} return 0;
}

POJ 3687:Labeling Balls(优先队列+拓扑排序)的更多相关文章

  1. POJ 3687 Labeling Balls【拓扑排序 优先队列】

    题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.cs ...

  2. poj 3687 Labeling Balls - 贪心 - 拓扑排序

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

  3. POJ 3687 Labeling Balls(拓扑排序)题解

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

  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 (拓扑)

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

  6. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  7. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

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

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

  9. poj 3687 Labeling Balls【反向拓扑】

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12246   Accepted: 3508 D ...

  10. 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. zero(NOIP模拟赛 Round 4)

    题目描述 假设x=N!,那么x的末尾有多少个零呢? 输入 一行,一个整数N. 输出 输出只有一个整数,表示x末尾零的个数. 这道题目,我们看一看数据范围, 10^1000肯定是高精啦! 然后我们再想一 ...

  2. 免格式化制作老毛桃PE工具

    由于移动硬盘数据很多,格式化制作太麻烦 先去老毛桃官网下载PE,生成ISO文件 将移动硬盘单独划分一个2G的空间用于装老毛桃,并格式化为FAT32格式,这样就避免全盘格式化了,只需要格式化这个分区   ...

  3. 11.OpenStack 安装监控和业务流程服务

    安装业务流程模块 安装和配置业务流程 创建数据库 mysql -uroot -ptoyo123 CREATE DATABASE heat; GRANT ALL PRIVILEGES ON heat.* ...

  4. js5秒后自动关闭本页面及5秒钟后自动跳转指定页面的方法

    5秒钟后自动关闭 <!DOCTYPE HTML> <html> <head> <title>倒计时自动关闭/跳转页面</title> < ...

  5. Servlet 2.4 规范之第七篇:过滤器

    过滤器是一套java组件,用于在请求—>资源—>应答的这一过程中即时转换处理负载和头信息. 本章讲述了Servlet 2.4 API中一些类和方法,这些类和方法提供了一套轻量级框架用于过滤 ...

  6. 2018 ICPC 徐州邀请赛 总结

    Day 0 上午在高铁里面,很困但是睡不着…… 中午到矿大报道. 食堂饭菜不错,有西瓜,就是有点辣. 下午热身赛,D题队友想了个假算法……GG. 评测机摸底考试正常进行. 热身赛之后精疲力尽,赶到宾馆 ...

  7. APP专项测试 | 内存及cpu

    命令: adb shell dumpsys meminfo  packagename 关注点: 1.Native/Dalvik 的 Heap 信息 具体在上面的第一行和第二行,它分别给出的是JNI层和 ...

  8. Linux Whois命令安装与使用

    大家都知道查看域名的详细信息,都是跑去whois服务器去查询,如 http://whois.chinaz.com 其实在Linux下直接有一个whois的命令,不过需要安装jwhois才可以,以Cen ...

  9. 每天一个linux命令1之scp

    不同的Linux之间copy文件常用有3种方法: 第一种就是ftp,也就是其中一台Linux安装ftpServer,这样可以另外一台使用ftp的client程序来进行文件的copy. 第二种方法就是采 ...

  10. OC语言基础之利用property优化封装

    1.property功能用法 1: // @property:可以自动生成某个成员变量的setter和getter声明 2: @property int age;//可以直接免去变量的声明 3: // ...