不一样的拓扑排序 
给定一些标记为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(拓扑)的更多相关文章

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

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

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

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

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

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

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

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

  5. poj——3687 Labeling Balls

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14835   Accepted: 4346 D ...

  6. POJ 3687 Labeling Balls()

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...

  7. POJ 3687 Labeling Balls (top 排序)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15792   Accepted: 4630 D ...

  8. 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 ...

  9. POJ 3687 Labeling Balls 逆向建图,拓扑排序

    题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...

  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. 爬虫——selenium基础

    Selenium,自动化浏览器技术.主要用于web应用自动测试和自动完成web基本任务管理.官方网站:https://selenium-python.readthedocs.io/getting-st ...

  2. Errors running builder 'DeploymentBuilder' on project

    Errors running builder 'DeploymentBuilder' on project 1.修改java源代码后点击保存,IDE 自动编译并热部署,提示如下错误: Errors o ...

  3. flutter开发vscode用模拟器调试

    android studio的太重,我装的是android sdk,使用avd的模拟器启动黑屏 启动夜神模拟器(已卸载) 建立连接: adb connect 127.0.0.1:62001    (夜 ...

  4. MyCat数据库中间件 - 分库

    MyCat MyCat用于解耦分布式数据库与java,比如分库分表以后,需要查询某条数据时,需要java根据需要查的数据先计算去哪个库查,然而有了Mycat就不用自己计算怎么存储,怎么查询了.MyCa ...

  5. 如何建立一个WCF服务并将其发布到IIS上

    在我们的软件开发中,经常会连接到数据库中,如果是常规的操作,我们经常会将连接数据库的字符串写在配置文件中,然后去读取数据库的连接字符串,其实这种方式是非常不科学的,这会直接暴露我们的数据库,直接暴露我 ...

  6. Java 8 函数式接口

    函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口. 函数式接口可以被隐式转换为 lambda 表达式. Lambda 表达式和方法引用 ...

  7. PHP金额工具类之将阿利伯数字转换为大写中文数字

    1.将阿拉伯数字转换为中文大写数字 <?php namespace core\components; class PriceHelper extends \yii\base\Component{ ...

  8. 数据库迁移(创建关联等操作) Target database is not up to date报错

    使用Mysql-sqlalchemy执行数据库迁移 来更新数据库: 队长试探性的在网上找了几种方案 依然没有解决报错问题: 后来看了https://www.aliyun.com/jiaocheng/4 ...

  9. Python——进程队列

    队列 先进先出 from multiprocessing import Queue q = Queue(5) #队列的大小 q.put(1) #放入内容 q.put(2) #放入内容 q.put(3) ...

  10. MySQL 索引长度和区分度

    首先  索引长度和区分度是相互矛盾的, 索引长度太短,那么区分度就很低,吧索引长度加长,区分度就高,但是索引也是要占内存的,所以我们需要找到一个平衡点: 那么这个平衡点怎么来定? 比如用户表有个字段 ...