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