POJ——T 3687 Labeling Balls
http://poj.org/problem?id=3687
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 14842 | Accepted: 4349 |
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
Source
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; const int M();
const int N();
int head[N],sumedge;
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[M];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} priority_queue<int>que;
int rd[N],ans[N],cnt;
inline void init()
{
sumedge=cnt=;
memset(rd,,sizeof(rd));
memset(ans,,sizeof(ans));
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
} int AC()
{
int t; scanf("%d",&t);
for(int n,m,if_;t--;init())
{
scanf("%d%d",&n,&m);
for(int u,v;m--;ins(v,u))
scanf("%d%d",&u,&v),rd[u]++;
for(int i=;i<=n;i++)
if(!rd[i]) que.push(i);
for(int u,v,weight=n;!que.empty();)
{
u=que.top(); que.pop();
ans[u]=weight--;cnt++;
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(--rd[v]==) que.push(v);
}
}
if(cnt!=n) puts("-1");
else
{
for(int i=;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[cnt]);
}
}
return ;
} int I_want_AC=AC();
int main(){;}
POJ——T 3687 Labeling Balls的更多相关文章
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- 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(反向拓扑+贪心思想!!!非常棒的一道题)
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: 9641 Accepted: 2636 Descri ...
- poj 3687 Labeling Balls【反向拓扑】
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12246 Accepted: 3508 D ...
- 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: 14835 Accepted: 4346 D ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
随机推荐
- ansible yum 模块 安装 vsftp
[root@ftp:/root] > ansible ansible01 -u root -k -m yum -a 'name=vsftpd state=latest' SSH password ...
- 修改Myeclies作者用户名
首先点击 windos 点击 preferences 依次点击左侧 Java -> Code Style -> Code Templates 击右侧Comments,将其中的Types项, ...
- S5PV210 三个Camera Interface/CAMIF/FIMC的区别
S5PV210有三个CAMIF单元,分别为CAMIF0 CAMIF1和CAMIF2.对应着驱动中的fimc0, fimc1, fimc2.在三星datasheet和驱动代码中CAMIF和FIMC(Fu ...
- 2019-03-15 使用Request POST获取CNABS网站上JSON格式的表格数据,并解析出来用xlwt写到Excel中
import requests import xlwt url = 'https://v1.cn-abs.com/ajax/ChartMarketHandler.ashx' headers={ 'Us ...
- JavaScript 常见创建对象的方式
JavaScript 有哪几种创建对象的方式? javascript创建对象简单的说,无非就是使用内置对象或各种自定义对象,当然还可以用JSON:但写法有很多种,也能混合使用. (1)对象字面量的方式 ...
- 基本配置及安全级别security-level
interface GigabitEthernet0/0 nameif outside //指定接口名称 security-level 0 //安全级别设置 ip address 1.1.1.2 ...
- 【【henuacm2016级暑期训练】动态规划专题 E】Destroying Roads
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 首先. 这张图是无向无权图. 因此任意两点之间的最短路可以通过N^2的bfs轻易算出来. 即得到d[N+10][N+10] 考虑s[ ...
- MyBatis学习总结(1)——MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- oracle用户锁定
https://www.cnblogs.com/lostyue/archive/2011/12/06/2278063.html
- 【原创】PHP扩展开发进阶
PHP扩展开发进阶 作者:wf (360电商技术) 在第一期PHP扩展开发入门中,简单的介绍了PHP的总体架构和执行机制,并具体说明了怎样开发和编译一个主要的PHP扩展,最后在PHP 5.3的环境下 ...