题目描述:

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 (同为洛谷博客,可对照参考)

最新文章

  1. OpenJDK 编译-Linux环境
  2. intel82599在centos6.5下编译安装
  3. dubbo &amp; zookeeper &amp; springMVC
  4. php用simplexml来操作xml
  5. 不错的 iOS 开发辅助工具
  6. MySQL高可用方案选型参考
  7. php openssl 增加密钥
  8. P1149 火柴棒等式
  9. c编程之排序
  10. 如何让windows服务器IIS支持.apk/.ipa文件下载
  11. BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏( floyd )
  12. java中使用ReentrantLock锁中的Condition实现三个线程之间通信,交替输出信息
  13. Lock、ReentrantLock、ReentrantReadWriteLock区别
  14. 【Java入门提高篇】Day25 史上最详细的HashMap红黑树解析
  15. 解决浏览器跨域限制方案之WebSocket
  16. ida信息获取函数
  17. blog决定不用二级域名,改为二级目录
  18. istio promethus收集不到数据
  19. 【Android】21.4 图片动画缩放示例
  20. 教你如何做一个优雅的Ecmascripter /转

热门文章

  1. 1.4 Linux下对lvm逻辑卷分区大小的调整(针对xfs和ext4不同文件系统)
  2. Fineui 根据datatable结构动态创建grid列,帮助类。动态绑定grid。
  3. C++ 二叉搜索树原理及其实现
  4. 什么是PHP?
  5. DRF框架(八)——drf-jwt手动签发与校验、搜索过滤组件、排序过滤组件、基础分页组件
  6. LeetCode 5073. 进击的骑士(Java)BFS
  7. C语言学习笔记01——C语言概述
  8. Spring mvc请求处理流程详解(一)之视图解析
  9. # - net - cannot access a disposed object r nobject name filebufferingreadstream
  10. WPF 的命令的自动刷新时机——当你 CanExecute 会返回 true 但命令依旧不可用时可能是这些原因