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 init -> 初始化一个git仓库 git clone -> 克隆一个本地库 git pull -> 拉取服务器最新代码 git fetch –p -> 强行 ...
- nginx配置ssl证书后无法访问https
一直听说https更安全,要安装证书,一直没试过,今天终于试了试 首先得有个http的域名网站,服务器. 到阿里云的安全-ssl证书管理申请一个免费的,可以绑定一个域名 然后完善资料,照着例子配置一 ...
- Postman的Post请求方式的四种类型的数据
1. form-data 就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件.当上传的字段是文件时 ...
- 五句话搞定JavaScript作用域(ES5)
JavaScript的作用域一直以来是前端开发中比较难以理解的知识点,对于JavaScript的作用域主要记住几句话,走遍天下都不怕... 一.“JavaScript中无块级作用域” 在Java或C# ...
- 剑指offer(15)
题目: 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 书中的思路: 按照这个思路我们很容易写出以下代码: import java.util.S ...
- webpack+vue 我的视角(持续更新)
最近一直在研究webpack+vue的组合拳,现在分享一下: webpack就是一个项目管理工具,可以各种模块化加载,然后压缩,当然还有热加载技术(时灵时不灵..) vue是mv*模式的框架,组件化开 ...
- js中的arguments
了解这个对象之前先来认识一下javascript的一些功能: 其实Javascript并没有重载函数的功能,但是Arguments对象能够模拟重载.Javascrip中国每个函数都会有一个Argume ...
- Handler主线程子线程之间的互相通信
Handler主线程子线程之间的互相通信 package com.wyl.dansnote; import android.app.Activity; import android.os.Bundle ...
- Appscanner实验还原code2
import _pickle as pickle from sklearn import svm, ensemble import random from sklearn.metrics import ...
- python之路--小数据池,再谈编码,is和 == 的区别
一 . 小数据池 # 小数据池针对的是: int, str, bool 在py文件中几乎所有的字符串都会缓存. # id() 查看变量的内存地址 s = 'attila' print(id(s)) 二 ...