[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.由于数 ...
随机推荐
- WIN10设置notepad++默认打开txt文件
修改txt的默认打开方式为notepad++.效果如下图所示 修改方式: 1.新建名称为OpenFromNotepad++的txt文档,并将后缀名修改为reg格式(注册表格式),在文件中输入以下内容. ...
- typescript导入图片报找不到模块的错误
https://www.cnblogs.com/chen-cong/p/10445635.html images.d.ts: declare module '*.svg' declare module ...
- IDEA和JIRA任务联动(redmine同理)
IDEA和JIRA任务联动-操作流程详解 redmine和这个步骤类似,只是第一步选择的工具不一样 操作流程 添加jira配置,File->Setting-->tools-->Tas ...
- js实现html表格<td>标签中带换行的文本显示出换行效果
遇见问题 如下内容中我写了几行,但是表格中并未按行显示,换行符反而变成了空格,于是想自己转换下 思考问题 1.可以看到表格的内容是后端传来的数据,于是想直接在后端转换下,把换行符替换成<br&g ...
- Mysql的BigInt(20),Int(20)以及Bigint(32)有区别吗?
原文: http://stackoverflow.com/questions/3135804/types-in-mysql-bigint20-vs-int20-etcc http://dev.mysq ...
- 【转帖】Infor转型十年启示录:ERP套件厂商为什么要做云平台?
Infor转型十年启示录:ERP套件厂商为什么要做云平台? https://www.tmtpost.com/4199274.html 好像浪潮国际 就是用的infor的ERP软件. 秦聪慧• 2019 ...
- 'cross-env' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 npm ERR! code ELIFECYCLE npm ERR! errno 1
在建立vue项目时报的错误 原因是缺少依赖,在此项目目录下下载依赖即可 cnpm install 发一次建立vue项目的完整顺序吧 //打开要创建的项目路径目录,创建项目 vue init webpa ...
- centos 7 搭建 k8s
环境 Centos 7.2 master 192.168.121.101node-1 192.168.121.134node-2 192.168.121.135 Kubernetes集群组件:– et ...
- hystrix中request cache请求缓存
有一个概念,叫做reqeust context,请求上下文,一般来说,在一个web应用中, 我们会在一个filter里面,对每一个请求都施加一个请求上下文,就是说,tomcat容器内,每一次请求,就是 ...
- C#各种字段类型对比
一.常量.只读字段.静态字段和静态只读字段对比 public class ModelClass { //常量在定义时必须赋初始值 //public const string constField; p ...