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. centos7安装vim以及在vim中显示中文

    1.centos7安装vim yum -y install vim(简单粗暴安装方法) 2.在vim中显示中文不出现乱码 (1).vim ~/.vimrc (~/.vimrc为vim配置文件) (2) ...

  2. Docker Manager for Kubernetes

    一.Kubernetes介绍 Kubernets是Google开源的容器集群系统,是基于Docker构建一个容器的调度服务,提供资源调度,均衡容灾,服务注册,动态伸缩等功能套件: Kubernets提 ...

  3. 在WebGL场景中进行棋盘操作的实验

    这篇文章讨论如何在基于Babylon.js的WebGL场景中,建立棋盘状的地块和多个可选择的棋子对象,在点选棋子时显示棋子的移动范围,并且在点击移动范围内的空白地块时向目标地块移动棋子.在这一过程中要 ...

  4. Memcached服务器上实现多个实例(约约问题排查)

    约约测试服上出行一个问题,司机收车失败. (1)经查看代码是null指针异常. 针对,之前,同套代码发布到华威测试服,未出现该问题,遂认定不是代码问题. (2)打印异常信息,获取null值异常的收车司 ...

  5. tensorflow enqueue_many传入多个值的列表传入异常问题————Shape () must have rank at least 1

    tf 的队列操作enqueue_many传入的值是列表,但是放入[]列表抛异常 File "C:\Users\lihongjie\AppData\Local\Programs\Python\ ...

  6. 一个基于NodeJS开发的APP管理CMS系统

    花了大概3周独立开发了一个基于NodeJS的CMS系统,用于公司APP的内容管理( **公司APP?广告放在最后 ^_^ ** ,管理员请理解~~~ )晚上看了部电影还不想睡,闲着也是闲着就作下小小总 ...

  7. Daily Scrumming* 2015.10.26(Day 7)

    一.总体情况总结 今天我们开会具体讨论了一下接下来的任务.还详细讨论了一下分数的分配,具体分数分配我们会在下一篇博客中详细说明. 我们下一周大致的工作安排如下: 1.UI:完成社团后台界面的设计,以及 ...

  8. 20172311『Java程序设计』课程 结对编程练习_四则运算第一周阶段总结

    20172311『Java程序设计』课程 结对编程练习_四则运算第一周阶段总结 结对伙伴 学号 :20172307 姓名 :黄宇瑭 伙伴第一周博客地址: http://www.cnblogs.com/ ...

  9. c# bitmap和new bitmap(bitmap)及在System.Drawing.Image.get_RawFormat()报错“参数无效”

    问题情境: 给picturebox赋image属性,我用一下代码,出错: Bitmap theBitmap = convertCameraData.display(rawDataArray, heig ...

  10. Percona XtraDB Cluster 5.7

    附加:相关在线文档https://www.percona.com/software/documentation 安装要求: 1.root权限2.保证开放3306.4444.4567.4568端口3.关 ...