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 ;
}

最新文章

  1. 未能解析目标框架“.NETFramework,Version=v4.0”的 mscorlib的解决方法
  2. 用Prime31实现Google Play In-App-Blling
  3. leetcode 13
  4. VIM 技巧 (一)全文统一添加
  5. NDK(13)JNIEnv和JavaVM
  6. L007-oldboy-mysql-dba-lesson07
  7. javaScript常用方法整合(项目中用到过的)
  8. Python - 元组(tuple) 详解 及 代码
  9. Highcharts下载与使用_数据报表图2
  10. [翻译]初识SQL Server 2005 Reporting Services Part 3
  11. spring boot / cloud (四) 自定义线程池以及异步处理@Async
  12. 爬虫时遇到的&#39; 编码错误gbk &#39; 的解决方案
  13. RNQOJ PID28 / [Stupid]愚蠢的宠物
  14. oracle数据库自增主键重复
  15. SpringJDBC数据库的基本使用
  16. Css_button样式对不齐
  17. java集合之vector容器
  18. MYSQL多行合并成一行多列
  19. VS2013 未找到与约束ContractName ...
  20. html 源码 引入样式

热门文章

  1. 新特性:postgresql的vacuum漫谈
  2. SQL Operations Studio的安装和使用
  3. 高可用OpenStack(Queen版)集群-5.Glance集群
  4. Codeforces1084 | Round526Div2 | 瞎讲报告
  5. 【深度学习的实用层面】(一)训练,验证,测试集(Train/Dev/Test sets)
  6. xocde missing file 解决方法
  7. (转)Django配置数据库读写分离
  8. jenkins设置定时任务
  9. JS特效@缓动框架封装及应用
  10. WebGL七点二