luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)
题目描述
Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls in his barn (2 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1 \ldots N1…N. Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.
FJ is pumping milk between KK pairs of stalls (1 \leq K \leq 100,0001≤K≤100,000). For the iith such pair, you are told two stalls s_isi and t_iti, endpoints of a path along which milk is being pumped at a unit rate. FJ is concerned that some stalls might end up overwhelmed with all the milk being pumped through them, since a stall can serve as a waypoint along many of the KK paths along which milk is being pumped. Please help him determine the maximum amount of milk being pumped through any stall. If milk is being pumped along a path from s_isi to t_iti, then it counts as being pumped through the endpoint stalls s_isi and
t_iti, as well as through every stall along the path between them.
FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N。所有隔间都被管道连通了。
FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。
输入输出格式
输入格式:
The first line of the input contains NN and KK.
The next N-1N−1 lines each contain two integers xx and yy (x \ne yx≠y) describing a pipe
between stalls xx and yy.
The next KK lines each contain two integers ss and tt describing the endpoint
stalls of a path through which milk is being pumped.
输出格式:
An integer specifying the maximum amount of milk pumped through any stall in the
barn.
输入输出样例
5 10
3 4
1 5
4 2
5 4
5 4
5 4
3 5
4 3
4 3
1 3
3 5
5 4
1 5
3 4
9 思路:
和区间上的差分写法差不多,把u-v划分成两条链, u->lca(u,v) lca(u,v)->v,,因为是点差所以我们对u点+1,v点+1,lca(u,v)-1,fa(lca(u,v))-1.最后跑个dfs求出最大值。 实现代码:
#include<bits/stdc++.h>
using namespace std; const int M = 2e5 + ;
int p[M][],w[M],dep[M],cnt,head[M],n,ans;
struct node{
int to,next;
}e[M]; void Add(int u,int v){
e[++cnt].to = v;e[cnt].next = head[u];head[u] = cnt;
} void dfs(int u,int fa,int deep){
dep[u] = deep;
p[u][] = fa;
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v == fa) continue;
dfs(v,u,deep+);
}
} void get_fa(){
for(int j = ;(<<j)<=n;j++)
for(int i = ;i <= n;i ++)
p[i][j] = p[p[i][j-]][j-];
} int lca(int a,int b){
if(dep[a] > dep[b]) swap(a,b);
int h = dep[b] - dep[a];
for(int i = ;(<<i)<=h;i++){
if((<<i)&h) b = p[b][i];
}
if(a != b){
for(int i = ;i >= ;i --){
if(p[a][i] != p[b][i]){
a = p[a][i]; b = p[b][i];
}
}
a = p[a][];
}
return a;
} void dfs1(int u,int fa){
for(int i = head[u];i;i = e[i].next){
int v = e[i].to;
if(v == fa) continue;
dfs1(v,u);
w[u] += w[v];
}
ans = max(ans,w[u]);
} int main()
{
int m,u,v;
scanf("%d%d",&n,&m); for(int i = ;i < n;i ++){
scanf("%d%d",&u,&v);
Add(u,v); Add(v,u);
}
dfs(,,); get_fa();
for(int i = ;i <= m;i ++){
scanf("%d%d",&u,&v);
int k = lca(u,v);
w[u]++; w[v]++; w[k]--;
w[p[k][]]--;
}
dfs1(,);
printf("%d\n",ans);
}
luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)的更多相关文章
- 洛谷P3128 [USACO15DEC]最大流Max Flow(树上差分)
题意 题目链接 Sol 树上差分模板题 发现自己傻傻的分不清边差分和点差分 边差分就是对边进行操作,我们在\(u, v\)除加上\(val\),同时在\(lca\)处减去\(2 * val\) 点差分 ...
- P3128 [USACO15DEC]最大流Max Flow (树上差分)
题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...
- 洛谷 P3128 [ USACO15DEC ] 最大流Max Flow —— 树上差分
题目:https://www.luogu.org/problemnew/show/P3128 倍增求 lca 也写错了活该第一次惨WA. 代码如下: #include<iostream> ...
- [LUOGU] P3128 [USACO15DEC]最大流Max Flow
题意:一棵树,多次给指定链上的节点加1,问最大节点权值 n个点,n-1条边很容易惯性想成一条链,幸好有样例.. 简单的树剖即可!(划去) 正常思路是树上差分,毕竟它就询问一次.. #include&l ...
- 洛谷3128 [USACO15DEC]最大流Max Flow——树上差分
题目:https://www.luogu.org/problemnew/show/P3128 树上差分.用离线lca,邻接表存好方便. #include<iostream> #includ ...
- 【luogu P3128 [USACO15DEC]最大流Max Flow】 题解
题目链接:https://www.luogu.org/problemnew/show/P3128 菜 #include <cstdio> #include <cstring> ...
- 洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)
因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之 ...
- P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of pipes to transport mil ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
随机推荐
- Git简易的命令入门
Git 全局设置: git config --global user.name "kszsa" git config --global user.email "duyon ...
- 数组去重--ES5和ES6
思路:把去重后的数组放到一个空数组中 ES5实现: function uni(arr) { var result = []; for (var i=0;i<arr.length;i++) { i ...
- C++ 通过ostringstream 实现任意类型转string
#include <iostream> #include <string> using namespace std; int main() { ; double b = 65. ...
- Technical Development Guide---for Google
Technical Development Guide This guide provides tips and resources to help you develop your technica ...
- Vue Router 路由实现原理
一.概念 通过改变 URL,在不重新请求页面的情况下,更新页面视图. 二.实现方式 更新视图但不重新请求页面,是前端路由原理的核心之一,目前在浏览器环境中这一功能的实现主要有2种方式: 1.Hash ...
- elasticsearch判断索引是否存在
一.判断索引是否存在 指定索引名,判断指定的索引是否存在集群中 /** * 判断指定的索引名是否存在 * @param indexName 索引名 * @return 存在:true; 不存在:fal ...
- 使用Vue自己做一个简单的MarkDown在线编辑器
1.首先要下载mark组件. npm install marked --save 2.在Vcontent.vue中简单写一些样式. <template> <div class=&qu ...
- 局域网 服务器 https
局域网内搭建一个服务器,可以使用 https 吗 - V2EXhttps://www.v2ex.com/t/472394 局域网内多台机器使用自签发证书架设https网站二:实施 - 左直拳的马桶_日 ...
- .net WCF WF4.5 状态机、书签与持久化
想看源码请直接翻到最后,使用方式如下图 如果同时需要多个书签可以直接在需要的位置创建书签,会认为是同一个实例. 若需要实现的效果是同时需要好几个部门审核,那么只要在对应的位置同时创建多个书签即可. 编 ...
- jenkins了解一下,讲一下jenkins这个鬼东西
一.jenkins是干什么的? jenkins是一个免费的集成工具,它是基于java开发的.用来做自动化部署,傻瓜化操作. 一般的项目部署流程: 开发代码——>功能测试——>打包(使用ma ...