POJ-3687 Labeling Balls(拓扑)
不一样的拓扑排序
给定一些标记为1到n的数, 求出满足a < b 的序列, 如果有多个输出, 按先标签1往前的位置, 然后按标签2往前的位置, 对于每个标签, 位置都尽量往前。
因为位置要往前,就不能正向建图, 因为正向的拓扑每次在最前的都是最小的点, 并不能保证标签1也在最前面, 比如
1 5 3 4 2
和
1 4 5 3 2
如果按拓扑排序, 答案一定是1 4 5 3 2, 因为4比5小, 但是题目想要各个标签的位置往前, 这样1, 2标签位置一样, 对于3标签, 第一个在3处, 第二个在4处, 所以答案应该是上面那个。
所以正向建图是标签尽量往前,所以就反向建图得到
2 4 3 5 1
和
2 3 5 4 1
用less 的优先队列, 这样每次都把最大的放在后面, 就把小的留在前面了, 对于每一个标签, 都尽可能往后扔,最后在倒叙输出, 就可以得到答案。
题目还有一个点, 要求输出不是排序以后的各个标签的顺序, 而是在从1-n位置上的标签。
所以在倒叙的时候需要 ans[t] = num–;
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f typedef long long int ll;
using namespace std; const int maxn = ; int n, m;
int ans[maxn];
int ind[maxn];
bool maps[maxn][maxn]; void init() {
memset(ans, , sizeof ans);
memset(ind, , sizeof ind);
memset(maps, , sizeof maps);
} bool topu() {
int num = n;
priority_queue<int, vector<int>, less<int> > pq;
for(int i=; i<=n; i++) {
if(ind[i] == ) {
pq.push(i);
}
}
while(!pq.empty()) {
int t = pq.top();
pq.pop();
ans[t] = num--;
for(int i=; i<=n; i++) {
if(maps[t][i] == true) {
ind[i]--;
if(ind[i] == )
pq.push(i);
}
}
}
if(num == )
return true;
else
return false;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
init();
scanf("%d%d", &n, &m);
for(int i=; i<m; i++) {
int u, v;
scanf("%d%d", &u, &v);
if(!maps[v][u]) {
maps[v][u] = true;
ind[u] ++;
}
}
bool bo = topu();
if(bo) {
for(int i=; i<=n; i++) {
printf("%d%c", ans[i], i==n ? '\n' : ' ');
}
} else {
printf("-1\n");
}
}
return ;
}
POJ-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(拓扑排序)
题目: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
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14835 Accepted: 4346 D ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- POJ 3687 Labeling Balls (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 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 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- POJ 3687 Labeling Balls(拓扑排序)题解
Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...
随机推荐
- Python中Celery 的基本用法以及Django 结合 Celery 的使用和实时监控进程
celery是什么 1 celery是一个简单,灵活且可靠的,处理大量消息的分布式系统 2 专注于实时处理的异步任务队列 3 同时也支持任务调度 执行流程 Celery 基本使用 tasks.py i ...
- Druid Monitor开启登录界面
<!-- druid --> <filter> <filter-name>druidWebStatFilter</filter-name> <fi ...
- python 3.6.1 安装scrapy踩坑之旅
系统环境:win10 64位系统安装 python基础环境配置不做过多的介绍 window环境安装scrapy需要依赖pywin32,下载对应python版本的exe文件执行安装,下载的pywin32 ...
- Laravel认证模块开发
菜鸟学Laravel(二) Laravel认证模块开发 laravel内部已经做好了一个简单的登录模块,我们可以用如下命令来生成: 1 php artisan make:auth 我们查看一下路由 ...
- 4 Past progressive VS simple past
1 一般过去时用来谈论过去开始和结束的活动.过去进行时用来谈论过去正在进行或者发生的活动. Why were you at office so later yesterday? I was worki ...
- [转帖]SPU、SKU、ID,它们都是什么意思,三者又有什么区别和联系呢?
SPU.SKU.ID,它们都是什么意思,三者又有什么区别和联系呢? http://blog.sina.com.cn/s/blog_5ff11b130102wx0p.html 电商时代,数据为王. 所以 ...
- picker-view 组件 的value失效问题
首先检查是不是漏了绑定关系 组件内 组件引用 如过还不行就用下面的方法,顺序问题 在给暂时列表赋值之后再对value赋值
- Day 4-10 logging模块
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- VUE项目问题之:去掉url中的#/
一.问题 使用VUE路由,项目的url总是带有锚点,如下: http://localhost:8082/#/ 二.解决 修改路由文件中 index.js 文件,即 src --> router ...
- 莫烦keras学习自修第五天【CNN卷积神经网络】
1.代码实战 #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ import numpy as np np.random.seed(1337) # for r ...