POJ 3687:Labeling Balls(优先队列+拓扑排序)
| 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:
- No two balls share the same label.
- 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, 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 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(优先队列+拓扑排序)的更多相关文章
- POJ 3687 Labeling Balls【拓扑排序 优先队列】
题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.cs ...
- 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 ...
- POJ 3687 Labeling Balls(拓扑排序)题解
Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...
- POJ 3687 Labeling Balls (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 D ...
- POJ - 3687 Labeling Balls (拓扑)
(点击此处查看原题) 题意 此处有n盏灯,编号为1~n,每盏灯的亮度都是唯一的,且在1~n范围之间,现已知m对灯之间的关系:a b ,说明灯a的亮度比灯b小,求出每盏灯的亮度,要求字典序最小(编号小的 ...
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
- poj 3687 Labeling Balls【反向拓扑】
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12246 Accepted: 3508 D ...
- poj 3687 Labeling Balls(拓补排序)
Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...
随机推荐
- 为何url地址不是直接发送到服务器,而是被编码后再发送
首先,先说一下,关于为何必须将url地址,去编码后,再发送,是因为相关的协议规范:RFC 1738,定义了url地址中不能包含除了0-9的数字,大小写字母(a-zA-Z),短横线’-‘ 之外的字母.换 ...
- python cProfile分析程序性能
转自:http://xianglong.me/article/analysis-python-application-performance-using-cProfile/?utm_source=tu ...
- JDK7集合框架源码阅读(三) HashMap
基于版本jdk1.7.0_80 java.util.HashMap 代码如下 /* * Copyright (c) 1997, 2010, Oracle and/or its affiliates. ...
- VS2017源代码版本管理
VS2017源代码版本管理有两种方式:Git(代码提交到服务器)和Team Foundation Server(代码提交到局域网) 一.Git版本管理(上传到码云服务器https://gitee.co ...
- 华农oj Problem L: CreatorX背英语【STL】
Problem L: CreatorX背英语 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 53 Solved: 36 [Submit][Status][ ...
- Python的网络编程[4] -> DHCP 协议[0] -> DHCP 的基本理论
DHCP协议 / DHCP Protocol 目录 DHCP 基本理论 DHCP 通信流程 DHCP 完整报文 DHCP 的 Optional 字段 DHCP 的报文类型 1 DHCP 基本理论 DH ...
- JAVA基础加强(张孝祥)_类加载器、分析代理类的作用与原理及AOP概念、分析JVM动态生成的类、实现类似Spring的可配置的AOP框架
1.类加载器 ·简要介绍什么是类加载器,和类加载器的作用 ·Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrap,ExtClassLoader ...
- CodeForces - 985F Isomorphic Strings
假如两个区间的26的字母出现的位置集合分别是 A1,B1,A2,B2,....., 我们再能找到一个排列p[] 使得 A[i] = B[p[i]] ,那么就可以成功映射了. 显然集合可以直接hash, ...
- POJ 3420 Quad Tiling (矩阵乘法)
[题目链接] http://poj.org/problem?id=3420 [题目大意] 给出一个4*n的矩阵,求用1*2的骨牌填满有多少方案数 [题解] 弄出不同情况的继承关系,用矩阵递推即可. [ ...
- hdu 4826 Labyrinth DP
题目链接:HDU - 4826 度度熊是一只喜欢探险的熊,一次偶然落进了一个m*n矩阵的迷宫,该迷宫只能从矩阵左上角第一个方格开始走,只有走到右上角的第一个格子才算走出迷宫,每一次只能走一格,且只能向 ...