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. Scrapy中的POST请求发送和递归爬取

    POST请求发送 重写爬虫应用文件中继承Spider类的 类的里面的start_requests(self)这个方法 def start_requests(self): #请求的url post_ur ...

  2. 【日常训练】数据中心(CSP 201812-4)

    分析 题目实际上是在要在给定的边上构建出一个树,使得这个树的最长边尽可能小. 这实际上是最小生成树的性质(反证法).问题从而得到解决. 代码 /* * Code name => csp20181 ...

  3. Rxjava - 操作符,线程操作的简单使用

    目录 创建操作符 10种常用的操作符定义 下面做几个操作符的demo演示 create from repeat defer interval Scheduler 什么是Scheduler? 如何使用S ...

  4. python简单计时器实现

    实现程序运行时间的显示与相互之间的计算: 实现代码: import time as t class Mytimer(): def __init__(self): self.unit=["年& ...

  5. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  6. leetcode-填充同一层的兄弟节点Ⅱ

    给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } 填充它的每个 ...

  7. string类型和int类型之间的转换

    一.string转int 1. 使用string流 /* 字符串转整型 */ /* * istringstream:从 string 读取数据 * ostringstream:向 string 写入数 ...

  8. 2017年第八届蓝桥杯【C++省赛B组】

    1.标题: 购物单 小明刚刚找到工作,老板人很好,只是老板夫人很爱购物.老板忙的时候经常让小明帮忙到商场代为购物.小明很厌烦,但又不好推辞. 这不,XX大促销又来了!老板夫人开出了长长的购物单,都是有 ...

  9. Chapter 9 软件实现

    软件实现包括代码设计.设计审查.代码编写.代码走查.代码编译和单元测试等活动.程序设计语言有很多,从机器语言到高级语言一直发展.软件编码需要遵循一些规范,JAVA代码有适当的空行,代码行及行内空格.分 ...

  10. 《Spring1之第十次站立会议》

    <第十次站立会议> 昨天:试着把用C#写的代码转换为java语言. 今天:已基本转换为java语言了,也能够实现视频聊天这个功能了. 遇到的问题:在进行视频通话时没有考虑到声音优化功能,实 ...