[USACO15DEC]最大流Max Flow(树上差分)
题目描述:
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(树上差分)的更多相关文章
- 洛谷P3128 [USACO15DEC]最大流Max Flow(树上差分)
题意 题目链接 Sol 树上差分模板题 发现自己傻傻的分不清边差分和点差分 边差分就是对边进行操作,我们在\(u, v\)除加上\(val\),同时在\(lca\)处减去\(2 * val\) 点差分 ...
- 洛谷3128 [USACO15DEC]最大流Max Flow——树上差分
题目:https://www.luogu.org/problemnew/show/P3128 树上差分.用离线lca,邻接表存好方便. #include<iostream> #includ ...
- 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> ...
- luoguP3128 [USACO15DEC]最大流Max Flow 题解(树上差分)
链接一下题目:luoguP3128 [USACO15DEC]最大流Max Flow(树上差分板子题) 如果没有学过树上差分,抠这里(其实很简单的,真的):树上差分总结 学了树上差分,这道题就极其显然了 ...
- 洛谷 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 ...
- 树上差分学习笔记 + [USACO15DEC]最大流$Max \ \ Flow \ \ By$
#\(\mathcal{\color{red}{Description}}\) \(Link\) \(FJ\)给他的牛棚的\(N(2≤N≤50,000)\)个隔间之间安装了\(N-1\)根管道,隔间编 ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow (树上差分)
###题目链接### 题目大意: 给你一棵树,k 次操作,每次操作中有 a b 两点,这两点路上的所有点都被标记一次.问你 k 次操作之后,整棵树上的点中被标记的最大次数是多少. 分析: 1.由于数 ...
随机推荐
- 【python库模块】Python subprocess模块功能与常见用法实例详解
前言 这篇文章主要介绍了Python subprocess模块功能与常见用法,结合实例形式详细分析了subprocess模块功能.常用函数相关使用技巧. 参考 1. Python subprocess ...
- Vmware解决虚拟机不能联网的问题
1. 设置为NAT模式 2. 启动win7的服务,命令窗口输入services.msc 回车 3. 修改Vmware的设置
- 海康—SADP激活(设备网络搜索)
海康sadp搜索工具(SADPTool)用于从网络上搜索同一网段内的所有在线设备.可以修改设备的缺省密码,修改网络IP地址及端口号 ,子网掩码及网关地址,IPV6地址网关地址,HTTP端口号和设备序列 ...
- cmake安装与使用
CMake(cross platform make)是一个开源的跨平台工具系列,旨在构建,测试和打包软件. 使用指定名为CMakeLists.txt的配置文件可以控制软件的构建.测试和打包等流程. 通 ...
- c#之break和continue的区别
break:跳出循环,执行循环外的语句: continue:跳出此次循环,进入下一次循环:
- Ajax返回的数据存放到js数组
js定义数组比较简单: var array = [ ] ; 即可 今天记录一下 js 数组的常用规则: 1. b = [1,'da',"sdaf"]; //定义数组给数组添加默认 ...
- IDEA界面太丑??尝试一下这几个风格
>>>>>>>>>>原文地址<<<<<<<<<< >>>> ...
- 【转】Isim——基本技巧
来源:电子产品世界: 注:本文由NingHeChuan本人多出整理所得,原文章图片不清晰,自己整理配图后重新发表 安装好ISE,系统已经自带了ISim仿真软件,相比于专业的仿真软件Modelsim,I ...
- Python 基础 常用运算符
Python 基础 常用运算符 计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算术运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算. 今天我们暂只学习 算术运算. ...
- 构建helm chart应用
使用helm命令创建基础目录 helm create t2cp [root@node04 ~]# tree t2cp t2cp ├── charts ├── Chart.yaml ├── templa ...