题目描述:

Farmer John has installed a new system of N−1N-1N−1 pipes to transport milk between the NNN stalls in his barn (2≤N≤50,0002 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1…N1 \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 KKK pairs of stalls (1≤K≤100,0001 \leq K \leq 100,0001≤K≤100,000). For the iiith such pair, you are told two stalls sis_isi and tit_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 KKK 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 sis_isi to tit_iti, then it counts as being pumped through the endpoint stalls sis_isi and

tit_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 NNN and KKK.

The next N−1N-1N−1 lines each contain two integers xxx and yyy (x≠yx \ne yx≠y) describing a pipe

between stalls xxx and yyy.

The next KKK lines each contain two integers sss and ttt 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

思路:

这里画一下样例的图就能比较明白题目的意思了。是这样的,题目给了一个图,再给k个询问,每次从一个点沿着原图的边到另一个点,k次询问后,问经过次数最多的点是哪一个。

这样一讲,就想到了点的树上差分,假设从s点到t点,可以通过差分数组,使\(dif[s]++,dif[t]++,dif[lca(s,t)]--,dif[f[lca(s,t)][0]]--\),在统计每个点经过的次数,在回溯过程中求得最大值即可。

实现上用了链式前向星,求lca得方法可参见上一篇博客。

代码:

#include <iostream>
#include <cstdio>
using namespace std;
#define max_n 50005
//前向星
int head[max_n];
struct edge
{
int v;
int next;
}e[max_n<<1];
int cnt = 0;
void add(int u,int v)
{
++cnt;
e[cnt].v = v;
e[cnt].next = head[u];
head[u] = cnt;
}
//读入优化
inline void read(int& x)
{
x = 0;int f=0;char ch = getchar();
while(ch<'0'||ch>'9') {if(ch=='-')f=1;ch=getchar();}
while('0'<=ch&&ch<='9') {x = 10*x+ch-'0';ch=getchar();}
x = f?-x:x;
}
//题目数据
int n,k;
int ans = 0;
int f[max_n][23];
int depth[max_n];
int dif[max_n];//差分数组
//求lca
void dfs(int u,int from)
{
depth[u] = depth[from]+1;
for(int i = 1;(1<<i)<=depth[u];i++)
{
f[u][i] = f[f[u][i-1]][i-1];
}
for(int i = head[u];i;i=e[i].next)
{
int v = e[i].v;
if(from==v) continue;
f[v][0] = u;
dfs(v,u);
}
}
int lca(int s,int t)
{
if(depth[s]<depth[t]) swap(s,t);
for(int i = 20;i>=0;i--)
{
if(depth[f[s][i]]>=depth[t])
{
s =f[s][i];
}
if(s==t)
{
return s;
}
}
for(int i = 20;i>=0;i--)
{
if(f[s][i]!=f[t][i])
{
s = f[s][i];
t = f[t][i];
}
}
return f[s][0];
}
//统计节点最大经过次数
void maxsum(int u,int from)
{
for(int i = head[u];i;i=e[i].next)
{
int v = e[i].v;
if(v==from) continue;
maxsum(v,u);
dif[u] += dif[v];
}
ans = max(ans,dif[u]);
} int main()
{
read(n);read(k);
//cout << "n " << n << " k " << k << endl;
for(int i = 1;i<n;i++)
{
int u,v;
read(u);
read(v);
add(u,v);
add(v,u);
}
dfs(1,0);
for(int i = 0;i<k;i++)
{
int u,v;
read(u);
read(v);
int LCA = lca(u,v);
dif[u]++;
dif[v]++;
dif[LCA]--;
dif[f[LCA][0]]--;
}
maxsum(1,0);
cout << ans << endl;
return 0;
}

参考文章:

顾z,差分数组 and 树上差分,https://rpdreamer.blog.luogu.org/ci-fen-and-shu-shang-ci-fen (洛谷出品!必属精品!,讲的虽然基础,但hin清晰)

思结,树上差分的两种思路,https://www.luogu.org/blog/sincereactor/shu-shang-ci-fen-di-liang-zhong-sai-lu (同为洛谷博客,可对照参考)

[USACO15DEC]最大流Max Flow(树上差分)的更多相关文章

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

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

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

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

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

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

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

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

  5. luoguP3128 [USACO15DEC]最大流Max Flow 题解(树上差分)

    链接一下题目:luoguP3128 [USACO15DEC]最大流Max Flow(树上差分板子题) 如果没有学过树上差分,抠这里(其实很简单的,真的):树上差分总结 学了树上差分,这道题就极其显然了 ...

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

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

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

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

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

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

  9. 树上差分学习笔记 + [USACO15DEC]最大流$Max \ \ Flow \ \ By$

    #\(\mathcal{\color{red}{Description}}\) \(Link\) \(FJ\)给他的牛棚的\(N(2≤N≤50,000)\)个隔间之间安装了\(N-1\)根管道,隔间编 ...

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

    ###题目链接### 题目大意: 给你一棵树,k 次操作,每次操作中有 a  b 两点,这两点路上的所有点都被标记一次.问你 k 次操作之后,整棵树上的点中被标记的最大次数是多少. 分析: 1.由于数 ...

随机推荐

  1. Jenkins集成TestNG

    1.Jenkins安装插件 TestNG Results Plugin 2. 添加配置 “构建后操作”->“Publish TestNG Results” 保持默认配置即可 3.查看报告 项目构 ...

  2. 缓解DDoS && cc 的最佳Linux内核设置 (转)

    https://javapipe.com/blog/iptables-ddos-protection/ kernel.printk = 4 4 1 7 kernel.panic = 10 kernel ...

  3. SQL - 外链接和内连接

    外链接和内连接: leetcode 题目:编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息: 第一次的答案:(错误) select ...

  4. 测试标题CSS样式

    标题1 内容1 标题2 内容2 标题3 内容3

  5. centos7查看防火墙状态、关闭防火墙

    查看防火墙状态: firewall-cmd --state 关闭防火墙: systemctl stop firewalld.service 禁止firewall开机启动: systemctl disa ...

  6. Resouce Pool的理解

    本篇文章从现象到本质再到具象去理解 , 从理论到实战再到源码回顾去深化. 1.在开发中,无处不在的池. eg 网络通信连接池: HttpClient连接池 HttpClient通过PoolingHtt ...

  7. 挂载一个NFS共享

    在 system2 上挂载一个来自 system1.group8.example.com 的NFS共享,并符合下列要求: 1./public 挂载在下面的目录上 /mnt/nfsmount 2./pr ...

  8. Locust性能测试-参数化批量注册

    前言 实现场景:所有并发虚拟用户共享同一份测试数据,并且保证虚拟用户使用的数据不重复. 例如,模拟10用户并发注册账号,总共有100个手机号,要求注册账号不重复,注册完毕后结束测试 准备数据 虚拟用户 ...

  9. springboot集成drools的方式一

    springboot集成drools的方式一(spring-drools.xml) 本文springboot采用1.5.1.RELEASE版本,drools采用的6.5.0.Final,一共会讲三种方 ...

  10. 必须掌握的Linux用户组

    在 Linux 系统中用户组起着重要作用.用户组提供了一种简单方法供一组用户互相共享文件.用户组也允许系统管理员更加有效地管理用户权限,因为管理员可以将权限分配给用户组而不是逐一分配给单个用户. 尽管 ...