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

hdu 5638 Toposort (拓扑排序+线段树)的更多相关文章

  1. HDU 5638 Toposort 拓扑排序 优先队列

    Toposort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 Description There is a directed acycli ...

  2. hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  3. CF798E. Mike and code of a permutation [拓扑排序 线段树]

    CF798E. Mike and code of a permutation 题意: 排列p,编码了一个序列a.对于每个i,找到第一个\(p_j > p_i\)并且未被标记的j,标记这个j并\( ...

  4. Nowcoder Hash Function ( 拓扑排序 && 线段树优化建图 )

    题目链接 题意 : 给出一个哈希表.其避免冲突的方法是线性探测再散列.现在问你给出的哈希表是否合法.如果合法则输出所有元素插入的顺序.如果有多解则输出字典序最小的那一个.如果不合法则输出 -1 分析 ...

  5. P3588 [POI2015]PUS(拓扑排序+线段树)

    P3588 [POI2015]PUS 对于每个$(l,r,k)$,将$k$个位置向剩下$r-l-k+1$个位置连边,边权为$1$,这样就保证$k$个位置比剩下的大 先给所有位置填$1e9$保证最优 然 ...

  6. Luogu5289 十二省联考2019字符串问题(后缀数组+拓扑排序+线段树/主席树/KDTree)

    先考虑80分做法,即满足A串长度均不小于B串,容易发现每个B串对应的所有A串在后缀数组上都是一段连续区间,线段树优化连边然后判环求最长链即可.场上就写了这个. 100分也没有什么本质区别,没有A串长度 ...

  7. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  8. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  9. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

随机推荐

  1. java随记

    jdk1.8中新增了 LocalDate 与 LocalDateTime等类来解决日期处理方法,同时引入了一个新的类DateTimeFormatter来解决日期格式化问题. LocalDateTime ...

  2. 将exe依赖运行的dll,合并入exe中,整个程序仅存在一个exe文件

    方法一: 使用ILMerge合并winform生成的exe和引用的dll文件 参考:https://blog.csdn.net/u010108836/article/details/76782375 ...

  3. ESP8266/ESP32模块晶振频偏调试

    ESP8266/ESP32模块晶振频偏调试 !> 前提:晶振频偏调试是需要仪器设备的支持才能完成的. 测试环境:IQ2010综合测试仪 本文仅记录有关频偏调试的主要内容,其余不在赘述. IQ20 ...

  4. Spring Boot之发送HTTP请求(RestTemplate详解)

    原文作者:微笑面对生活 https://www.javazhiyin.com/19714.html#comment-345 RestTemplate是Spring提供的用于访问Rest服务的客户端,R ...

  5. python多线程创建与使用(转)

    原文:http://codingpy.com/article/python-201-a-tutorial-on-threads/ 创建多线程 创建多线程主要有2种方式. 使用threading.Thr ...

  6. docker 部署 zookeeper+kafka 集群

    主机三台172.16.100.61172.16.100.62172.16.100.63Docker 版本 当前最新版 # 部署zk有2种方法 ## 注意 \后不要跟空格 一 . 端口映射 172.16 ...

  7. 华为笔试——C++最高分问题

    题目介绍:现在输入一组数据,写入学生的考试分数.已知学生数为N,学生编号为1到N,且0<N<=30000,每个学生都有一个分数:操作数为M且0<M<5000.输入第一行为N M ...

  8. Annotation 使用备忘2

    title: Annotation 使用备忘 date: 2018-01-02 20:48:43 tags: [Annotation] categories: [Programming,Java] - ...

  9. trustbox文件破解

    常见的破解方式,是要还原内容的二进制文件,删除加密壳部分的对应二进制数值,然后把剩下的内容保存下来,就实现了破解的任务.  淘宝破解链接:https://item.taobao.com/item.ht ...

  10. (转)Django 数据库

         转:https://blog.csdn.net/ayhan_huang/article/details/77575186      目录 数据库说明 配置数据库 在屏幕输出orm操作对应的s ...