DZY Loves Topological Sorting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1250    Accepted Submission(s): 403

Problem Description
A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge (u→v) from vertex u to vertex v, ucomes before v in the ordering.
Now, DZY has a directed acyclic graph(DAG). You should find the lexicographically largest topological ordering after erasing at most k edges from the graph.
 
Input
The input consists several test cases. (TestCase≤5)
The first line, three integers n,m,k(1≤n,m≤105,0≤k≤m).
Each of the next m lines has two integers: u,v(u≠v,1≤u,v≤n), representing a direct edge(u→v).
 
Output
For each test case, output the lexicographically largest topological ordering.
 
Sample Input
5 5 2
1 2
4 5
2 4
3 4
2 3
3 2 0
1 2
1 3
 
Sample Output
5 3 1 2 4
1 3 2

Hint

Case 1.
Erase the edge (2->3),(4->5).
And the lexicographically largest topological ordering is (5,3,1,2,4).

 
思路:
用线段树维护区间最小值。
 
ps.之前没在查询操作加个pushup,一直跑不出答案,因为在查询过程中,sum的值是更新了的,所以要pushup下
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 2e5+;
const int inf = 0x3f3f3f3f;
int n,m,cnt,key;
vector<int>g[M];
int sum[M<<],du[M];
void pushup(int rt){
sum[rt] = min(sum[rt<<],sum[rt<<|]);
} void build(int l,int r,int rt){
if(l == r){
sum[rt] = du[l];
return;
}
mid;
build(lson);
build(rson);
pushup(rt);
} void update(int p,int l,int r,int rt){
if(l == r){
sum[rt]--;
return ;
}
mid;
if(p <= m) update(p,lson);
else update(p,rson);
pushup(rt);
} void query(int l,int r,int rt){
if(l == r){
cnt -= sum[rt];
key = l;
sum[rt] = inf;
return ;
}
mid;
if(sum[rt<<|] <= cnt) query(rson);
else query(lson);
pushup(rt);
} int main(){
int u,v;
while(cin>>n>>m>>cnt){
for(int i = ;i <= m;i ++){
cin>>u>>v;
g[u].push_back(v);
du[v]++; //入度
}
build(,n,);
for(int i = ;i <= n;i ++){
query(,n,);
if(i==) cout<<key;
else cout<<" "<<key;
for(int j = ;j < g[key].size();j++){
int x = g[key][j];
update(x,,n,);
}
}
cout<<endl;
memset(du,,sizeof(du));
}
return ;
}

hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)的更多相关文章

  1. HDU 5195 DZY Loves Topological Sorting 拓扑排序

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  2. hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  3. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  4. hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

    DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

  5. HDU 5195 - DZY Loves Topological Sorting

    题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会 ...

  6. 2019.01.22 hdu5195 DZY Loves Topological Sorting(贪心+线段树)

    传送门 题意简述:给出一张DAGDAGDAG,要求删去不超过kkk条边问最后拓扑序的最大字典序是多少. 思路:贪心帮当前不超过删边上限且权值最大的点删边,用线段树维护一下每个点的入度来支持查询即可. ...

  7. hdu 5266 pog loves szh III(lca + 线段树)

    I - pog loves szh III Time Limit:6000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I ...

  8. Topological Sorting拓扑排序

    定义: Topological Sorting is a method of arranging the vertices in a directed acyclic graph (DAG有向无环图) ...

  9. hdu 5638 Toposort (拓扑排序+线段树)

    Toposort Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

随机推荐

  1. pygrib学习

    pygrib-2.0.3/docs/index.html 导入pygrib模块 >>> import pygrib 打开grib文件,获取grib消息迭代器 >>> ...

  2. Discuz x3.2利用阿里云cdn处理https访问亲测教程

    第一步配置cdn和https 1.首先去阿里云.腾讯云.七牛云等申请免费https证书 2.虚拟主机是不能直接支持https的,需要cdn处理后才可以,并且端口是80 3.开启cdn加速处理,(买一个 ...

  3. 设置JFrame背景图片

    这里我就放上改写的代码吧,不做多的解释,推荐一个好的博文 https://blog.csdn.net/jdsjlzx/article/details/16831815 public void ini_ ...

  4. Frida----基本代码

    代码来自官网:https://www.frida.re/docs/examples/android/ import frida, sys def on_message(message, data): ...

  5. 【转】sshpass-Linux命令之非交互SSH密码验证

      sshpass-Linux命令之非交互SSH密码验证 ssh登陆不能在命令行中指定密码.sshpass的出现,解决了这一问题.sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次 ...

  6. python的字符串格式化

    1.python到底有那几种字符串格式化模块? python有3种格式化字符串的方法: 传统的%字符串格式符 str.format函数 字符串模版template 新的python 3.6+还提供了新 ...

  7. ovs源码阅读--netlink使用

    netlink netlink socket是一种用于用户态进程和内核态进程之间的通信机制.它通过为内核模块提供一组特殊的API,并为用户程序提供了一组标准的socket接口的方式,实现了全双工的通讯 ...

  8. [Github] Github使用教程

    前言 Github是一个面向开源及私有软件项目的托管平台.它可以免费使用,并且速度快速,拥有超多的用户.是目前管理软件开发和发现已有代码的首选平台.下面将向Github新手介绍相关操作. 正文 注册 ...

  9. swapon和swapoff命令详解

    基础命令学习目录首页 原文链接:https://blog.csdn.net/yexiangCSDN/article/details/83182259 swapon命令用于激活Linux系统中交换空间, ...

  10. 定时任务crone表达式demo

    1. cron表达式格式: {秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)} 2. cron表达式各占位符解释: {秒数} ==> 允许值范围: 0~59 ,不允许 ...