不一样的拓扑排序 
给定一些标记为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. #Leetcode# 1016. Binary String With Substrings Representing 1 To N

    https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/ Given a binary stri ...

  2. [转][mysql]创建函数失败(1418错误)mysql双主模式导致的问题

    https://blog.csdn.net/qq523786283/article/details/75102170

  3. 墨者学院——密码学加解密实训(Base64转义)

    地址:https://www.mozhe.cn/bug/detail/SW5ObnVFa05vSHlmTi9pcWhRSjRqZz09bW96aGUmozhe 在靶场中找到内容 解密 访问直接得key

  4. java设计模式:面向对象设计的7个原则

    在软件开发中,为了提高软件系统的可维护性和可复用性,增加软件的可扩展性和灵活性,程序员要尽量根据7条原则来开发程序,从而提高软件开发效率,节约软件开发成本和维护成本. 这7条原则分别是:开闭原则.里氏 ...

  5. CSS3 transform-style 属性

    语法     transform-style: flat | preserve-3d   语法项目 说明  初始值         flat  适用于         块元素和行内元素 可否继承    ...

  6. linux之nload和iftop查看网络使用情况

    操作系统: centos7 nload: yum install -y gcc yum install -y gcc-c++ yum install -y ncurses-devel yum inst ...

  7. saltstack二

    配置管理 haproxy的安装部署 haproxy各版本安装包下载路径https://www.haproxy.org/download/1.6/src/,跳转地址为http,改为https即可 创建相 ...

  8. 集合转数组的toArray()和toArray(T[] a)方法

    参考:集合转数组的toArray()和toArray(T[] a)方法 1.ArrayList的toArray ArrayList提供了一个将List转为数组的一个非常方便的方法toArray.toA ...

  9. Postman & API

    Postman & API https://www.getpostman.com/ https://www.getpostman.com/downloads/ Postman Canary h ...

  10. 老男孩python学习自修第十天【三元表达式与lambda表达式】

    例如: 1.使用三元表达式给变量赋值 result = '空' if x == None else x 2.使用lambda定义函数 add = lambda x, y: x+y