Toposort

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 499    Accepted Submission(s): 203

Problem Description
There is a directed acyclic graph with n vertices and m edges. You are allowed to delete exact k edges in such way that the lexicographically minimal topological sort of the graph is minimum possible.
 
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains three integers n, m and k (1≤n≤100000,0≤k≤m≤200000) -- the number of vertices, the number of edges and the number of edges to delete.

For the next m lines, each line contains two integers ui and vi, which means there is a directed edge from ui to vi (1≤ui,vi≤n).

You can assume the graph is always a dag. The sum of values of n in all test cases doesn't exceed 106. The sum of values of m in all test cases doesn't exceed 2×106.

 
Output
For each test case, output an integer S=(∑i=1ni⋅pi) mod (109+7), where p1,p2,...,pn is the lexicographically minimal topological sort of the graph.
 
Sample Input
3
4 2 0
1 2
1 3
4 5 1
2 1
3 1
4 1
2 3
2 4
4 4 2
1 2
2 3
3 4
1 4
 
Sample Output
30
27
30
 
思路:
和上道hdu 5195差不多,改点东西就行了。。之前疯狂超时,找了一个小时找不到哪里出了问题,最后问了下徐巨,发现vector忘了清空,清空下就好了。
 
实现代码:
#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 ll m = (l + r) >> 1
const int M = 2e5+;
const int inf = 0x3f3f3f3f;
const int md = 1e9+;
ll n,m,cnt,key;
vector<ll>g[M];
ll sum[M<<],du[M]; void pushup(ll rt){
sum[rt] = min(sum[rt<<],sum[rt<<|]);
} void build(ll l,ll r,ll rt){
if(l == r){
sum[rt] = du[l];
return;
}
mid;
build(lson);
build(rson);
pushup(rt);
} void update(ll p,ll l,ll r,ll rt){
if(l == r){
sum[rt]--;
return ;
}
mid;
if(p <= m) update(p,lson);
else update(p,rson);
pushup(rt);
} void query(ll l,ll r,ll rt){
if(l == r){
cnt -= sum[rt];
key = l;
sum[rt] = inf;
return ;
}
mid;
if(sum[rt<<] <= cnt) query(lson);
else query(rson);
pushup(rt);
} int main(){
ll u,v;
ll t;
scanf("%lld",&t);
while(t--){
scanf("%lld%lld%lld",&n,&m,&cnt);
for(ll i = ;i <= m;i ++){
scanf("%lld%lld",&u,&v);
g[u].push_back(v);
du[v]++;
}
build(,n,);
ll ans = ;
for(ll i = ;i <= n;i ++){
query(,n,);
ans=(ans+key*i)%md;
for(ll j = ;j < g[key].size();j++){
ll x = g[key][j];
update(x,,n,);
}
}
printf("%lld\n",ans);
memset(du,,sizeof(du));
memset(sum,inf,sizeof(sum));
for(ll i=;i<=n;i++)
g[i].clear();
}
return ;
}

最新文章

  1. 利用Civil 3D API更改曲面的样式
  2. Hibernate中Java对象的三种状态
  3. Visual C++2012中CMFCPropertySheet的用法
  4. tornado--SESSION框架,一致性hash,分布式存储
  5. easyUI-combobox 后台导入Json数据的方法
  6. php之CI框架多语言的用法
  7. JavaScript内的类型转换
  8. 在java程序中访问windows有用户名和密码保护的共享目录
  9. php返回的json格式
  10. html5 PACS漫谈
  11. spring-事务实现原理
  12. C#与Java对比学习
  13. GStreamer 简化 Linux 多媒体开发
  14. NetCore版RPC框架NewLife.ApiServer
  15. 常量(constant)
  16. YSLOW(一款实用的网站性能检测工具)
  17. ES6学习笔记三
  18. 性能测试工具--SIEGE安装及使用简介 siege压力测试
  19. python redis 操作
  20. An internal error occurred during: &amp;quot;Building workspace&amp;quot;. GC overhead limit exceeded

热门文章

  1. Cisco Packet Tracer中通过集线器组网
  2. 关于dbw 与dbm 的计算
  3. Android Dalvik虚拟机初识
  4. VGGnet——从TFrecords制作到网络训练
  5. keyup在移动端失效解决方法
  6. 【LeetCode算法题库】Day7:Remove Nth Node From End of List &amp; Valid Parentheses &amp; Merge Two Lists
  7. Spark概述及集群部署
  8. Mac环境搭建以太坊私有链
  9. 笨办法学Python - 习题3: Numbers and Math
  10. partprobe命令详解