题目描述

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.

输入输出样例

输入样例#1: 复制

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
输出样例#1: 复制

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 (树上差分)的更多相关文章

  1. 洛谷P3128 [USACO15DEC]最大流Max Flow(树上差分)

    题意 题目链接 Sol 树上差分模板题 发现自己傻傻的分不清边差分和点差分 边差分就是对边进行操作,我们在\(u, v\)除加上\(val\),同时在\(lca\)处减去\(2 * val\) 点差分 ...

  2. P3128 [USACO15DEC]最大流Max Flow (树上差分)

    题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...

  3. 洛谷 P3128 [ USACO15DEC ] 最大流Max Flow —— 树上差分

    题目:https://www.luogu.org/problemnew/show/P3128 倍增求 lca 也写错了活该第一次惨WA. 代码如下: #include<iostream> ...

  4. [LUOGU] P3128 [USACO15DEC]最大流Max Flow

    题意:一棵树,多次给指定链上的节点加1,问最大节点权值 n个点,n-1条边很容易惯性想成一条链,幸好有样例.. 简单的树剖即可!(划去) 正常思路是树上差分,毕竟它就询问一次.. #include&l ...

  5. 洛谷3128 [USACO15DEC]最大流Max Flow——树上差分

    题目:https://www.luogu.org/problemnew/show/P3128 树上差分.用离线lca,邻接表存好方便. #include<iostream> #includ ...

  6. 【luogu P3128 [USACO15DEC]最大流Max Flow】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3128 菜 #include <cstdio> #include <cstring> ...

  7. 洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)

    因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之 ...

  8. P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of  pipes to transport mil ...

  9. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

随机推荐

  1. Python_练习题_49

    # 3.用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb name=['alex','wupeiqi','yuanhao','nezha'] def func(item): re ...

  2. Python—os模块介绍

    OS模块 我们平时工作中很常用到的一个模块,通过os模块调用系统命令,获得路径,获取操作系统的类型等都是使用该模块.os 模块提供了很多允许你的程序与操作系统直接交互的功能 得到当前工作目录,即当前P ...

  3. 使用C# HttpWebRequest进行多线程网页提交。Async httpclient/HttpWebRequest实现批量任务的发布及异步提交和超时取消

    使用线程池并发处理request请求及错误重试,使用委托处理UI界面输出. http://www.cnblogs.com/Charltsing/p/httpwebrequest.html for (i ...

  4. Java向下转型的意义

    一开始学习 Java 时不重视向下转型.一直搞不清楚向下转型的意义和用途,不清楚其实就是不会,那开发的过程肯定也想不到用向下转型. 其实向上转型和向下转型都是很重要的,可能我们平时见向上转型多一点,向 ...

  5. Python_内置函数之max

    源码: def max(*args, key=None): # known special case of max """ max(iterable, *[, defau ...

  6. echarts使用笔记五:echarts的Zoom控件

    option = { title: { text: '趋势' }, tooltip : { trigger: 'axis', show:true, axisPointer : { // 坐标轴指示器, ...

  7. Windows 机器上面同时安装mysql5.6 和 mysql5.7 的方法

    1. 自己遇到的两个坑: . mysql 登录的时候 需要使用-P 来指定端口号 不然默认走 呢 . mysql 5.6 和 mysql 5.7 更改用户密码的命令不一样.. 我这边浪费了很长时间: ...

  8. Selenium简单回顾

    一.Selenium介绍 1.Selenium(浏览器自动化测试框架): Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的 ...

  9. vs code配置

    新版的用户设置不是代码, https://blog.csdn.net/zhaojia92/article/details/53862840 https://www.cnblogs.com/why-no ...

  10. mvc学习过程碰到问题

    Fluent API配置 单例模式+Autofac 批量注入