不一样的拓扑排序 
给定一些标记为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. python Drools

    python Drools - 国际版 Binghttps://cn.bing.com/search?q=python+Drools&qs=n&FORM=BESBTB&sp=- ...

  2. java 工具

    JClassLib 4.2 发布了,该版本支持 Java 7 和 Java 8 的类文件属性查看. JClassLib不但是一个字节码阅读器而且还包含一个类库允许开发者读取,修改,写入Java Cla ...

  3. C#中闭包的陷阱

    我们在使用lambda的时候会遇到闭包,在闭包中有一个陷阱是在for循环中产生的,先上代码: class Program { static void Main(string[] args) { Act ...

  4. Java Hash集合的equals()与hashCode() 方法

    Java 集合实现类,无论是HashSet.HashMap等所有的Hash算法实现的集合类(后面简称Hash集合),加入的对象必须实现 hashCode() 与 equals() 方法,稍微不同的地方 ...

  5. vue+webpack项目打包后背景图片加载不出来问题解决

    在做VUE +的WebPack脚手架项目打包完成后,在IIS服务器上运行发现项目中的背景图片加载不出来检查项目代码发现是因为CSS文件中,背景图片引用的路径问题;后来通过修改配置文件,问题终于解决了, ...

  6. python之路--JavaScript

    一. JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,希望这门语言能成为国际化标准,于是决定将Jav ...

  7. Mysql Router 的集群

    1. c:\mysql-router, c:\mysql-5.7.23, 这两个目录的bin都要加入path 2. c:\mysql-shell,在bin下,有一个 mysqlsh.exe, 双击,打 ...

  8. python之类和__init__

    构建一个商品类,__init__函数类似于构造方法,self类似于this import random class Goods: def __init__(self, name, price): se ...

  9. Python包的相对导入时出现问题解决

    资料参考: https://www.cnblogs.com/ArsenalfanInECNU/p/5346751.html 在python导入包,如下: from .units import * 经常 ...

  10. 使用layui 做后台管理界面,在Tab中的链接点击后添加一个新TAB的解决方法

    给链接或按钮  添加 onclick="self.parent.addTab('百度','http://www.baidu.com','icon-add')" 如: <a h ...