两个题目的意思差不多 都是希望得出的拓扑序如果有多种 要求输出字典序小的情况

这里引用一个大佬的博客 关于为什么不能直接建图然后用小根堆解决这个问题(http://blog.csdn.net/rgnoH/article/details/75253355 : 出处)

再解答一个小问题:

主要是在和六号@Mogician_Evian 交流之后想到的:这道题可以用小根堆做吗?

看起来可以!

可能的操作是:

1.题目中说什么,就怎么连边,并记录入度。 
2.执行Topsort标准操作,压入小根堆中。 此时从1到N,依次给取出的堆顶元素进行编号。

可能的分析方法和上面的是类似的。

然而并不能!

为什么?看看这一组数据:

3 1 
3 1

正解应该是

2 3 1

然而小根堆会输出

3 1 2

为什么?

我们要的并不是让新编号小的数尽量靠前,而是让答案数组靠前的数尽量小! 
小根堆讨论就只是让新编号小的数尽量靠前了。

豁然开朗!

hdu 4857 ac代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int in[];
int mark[];
vector<int > edge[];
int n,m;
/*
struct node
{
int x;
int point;
bool operator <(node a,node b)
{
return a.point > b.point;
}
};
*/
void init()
{
for(int i=; i<=n; i++)
{
in[i]=;
edge[i].clear();
}
}
void topo()
{
priority_queue<int,vector<int>,less<int> >q; //大根堆
for(int i=; i<=n; i++)
{
if(in[i]==) q.push(i);
}
int ret=n;
while(!q.empty())
{
int now=q.top();
q.pop();
mark[ret--]=now;
for(int i=; i<edge[now].size(); i++)
{
int temp=edge[now][i];
in[temp]--;
if(in[temp]==) q.push(temp);
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
init();
for(int i=; i<=m; i++)
{
int a,b;
scanf("%d %d",&a,&b);
edge[b].push_back(a);
in[a]++;
}
topo();
cout<<mark[];
for(int i=; i<=n; i++) cout<<' '<<mark[i];
cout<<endl;
} return ;
}

codeforce E - Minimal Labels+hdu 4857的更多相关文章

  1. Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图

    E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. Educational Codeforces Round 25 E. Minimal Labels&&hdu1258

    这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...

  3. HDU 4857 拓扑排序 优先队列

    n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...

  4. Codeforces 825E - Minimal Labels

    825E - Minimal Labels 题意 给出 m 条有向边,组成有向无环图,输出一个 1 到 n 组成的排列,每个数只能出现一次,表示每个点的标号.如果有边 \((u, v)\) 那么 \( ...

  5. HDU 4857

    HDU 4857 (反向拓扑排序 + 优先队列) 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须 ...

  6. HDU 4857 topological_sort

    逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  7. Minimal Labels

    Minimal Labels 这个题需要用到拓扑排序的思维,但是这个题还有一个条件--字典序最小,因此可以用一个递增的优先队列来维护,每找到一个入度为 0 的点就把它 push 进去因而每一次判断的点 ...

  8. HDU 4857 逃生 (反向拓扑排序 & 容器实现)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  9. HDU 4857 逃生 (优先队列+反向拓扑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 解题报告:有n个点,有m个条件限制,限制是像这样的,输入a  b,表示a必须排在b的前面,如果不 ...

随机推荐

  1. flask 设置配置文件的方式

    from flask import Flask from flask import current_app """ 配置参数设置与读取 """ ...

  2. fsLayuiPlugin附件上传使用说明

    fsLayuiPlugin 是一个基于layui的快速开发插件,支持数据表格增删改查操作,提供通用的组件,通过配置html实现数据请求,减少前端js重复开发的工作. GitHub下载 码云下载 测试环 ...

  3. Chrome浏览器报错:ERR_UNSAFE_PORT

    今天用Chrome浏览器打开一个页面发现报错了:ERR_UNSAFE_PORT. 所以,去搜了一下发现Chrome浏览器是默认一些端口号为非安全端口的. 遇到这个问题建议更换端口号或者更换浏览器打开. ...

  4. Mac 卸载Python3.6

    Mac 自带的 Python 已经能够满足我们的需要了,因此很多同学在安装完 Python 之后,又想要将其删除,或者称之为卸载. 对于删除 Python,我们首先要知道其具体都安装了什么,实际上,在 ...

  5. 官网引用的axios,lodash文件在脚手架中如何使用?

    对于官网属性与侦听器模块,所引用的以下文件在脚手架中如何使用? <script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/ ...

  6. About Xi’an

    Introduction Ancient Capital It is the birthplace of the Chinese Nation, is one of the four ancient ...

  7. Android Popwindow使用总结

    Android Popwindow使用总结 转 https://www.jianshu.com/p/3812ff5ef272 1.基本使用方法 View view = getLayoutInflate ...

  8. nc简单使用

    1.安装 2.运行

  9. MapReduce编程实例

    MapReduce常见编程实例集锦. WordCount单词统计 数据去重 倒排索引 1. WordCount单词统计 (1) 输入输出 输入数据: file1.csv内容 hellod world ...

  10. 分组卷积+squeezenet+mobilenet+shufflenet的参数及运算量计算

    来一发普通的二维卷积 1.输入feature map的格式为:m * m * h1 2.卷积核为 k * k 3.输出feature map的格式为: n * n * h2 参数量:k * k * h ...