【bzoj4390】[Usaco2015 dec]Max Flow LCA
创始人
2024-02-17 06:56:42
0

题目描述

Farmer John has installed a new system of N−1 pipes to transport milk between the N stalls in his barn (2≤N≤50,000), conveniently numbered 1…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≤K≤100,000). For the iith such pair, you are told two stalls sisi and titi, 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 sisi to titi, then it counts as being pumped through the endpoint stalls sisi and titi, as well as through every stall along the path between them.

给定一棵有N个点的树,所有节点的权值都为0。

有K次操作,每次指定两个点s,t,将s到t路径上所有点的权值都加一。

请输出K次操作完毕后权值最大的那个点的权值。

输入

The first line of the input contains NN and KK.
The next N−1 lines each contain two integers x and y (x≠y,x≠y) describing a pipe between stalls x and y.
The next K lines each contain two integers ss and t 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.

样例输入

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

样例输出

9


题解

LCA

在x到y的路径上加1,差分一下就是:在x和y上加1,在lca和fa[lca]上减1。

于是使用看得过去一点的求LCA方法求出LCA(代码写了树上倍增),打上差分标记,然后自下而上统计一遍答案即可。

#include 
#include 
#include 
#define N 50010
using namespace std;
int head[N] , to[N << 1] , next[N << 1] , cnt , fa[N][20] , deep[N] , log[N] , sum[N];
void add(int x , int y)
{to[++cnt] = y , next[cnt] = head[x] , head[x] = cnt;
}
void dfs(int x)
{int i;for(i = 1 ; (1 << i) <= deep[x] ; i ++ ) fa[x][i] = fa[fa[x][i - 1]][i - 1];for(i = head[x] ; i ; i = next[i])if(to[i] != fa[x][0])fa[to[i]][0] = x , deep[to[i]] = deep[x] + 1 , dfs(to[i]);
}
int lca(int x , int y)
{int i;if(deep[x] < deep[y]) swap(x , y);for(i = log[deep[x] - deep[y]] ; ~i ; i -- )if((1 << i) <= deep[x] - deep[y])x = fa[x][i];if(x == y) return x;for(i = log[deep[x]] ; ~i ; i -- )if((1 << i) <= deep[x] && fa[x][i] != fa[y][i])x = fa[x][i] , y = fa[y][i];return fa[x][0];
}
int solve(int x)
{int i , ans = 0;for(i = head[x] ; i ; i = next[i])if(to[i] != fa[x][0])ans = max(ans , solve(to[i])) , sum[x] += sum[to[i]];return max(ans , sum[x]);
}
int main()
{int n , m , i , x , y , z;scanf("%d%d" , &n , &m);for(i = 2 ; i <= n ; i ++ ) scanf("%d%d" , &x , &y) , add(x , y) , add(y , x) , log[i] = log[i >> 1] + 1;dfs(1);for(i = 1 ; i <= m ; i ++ ) scanf("%d%d" , &x , &y) , z = lca(x , y) , sum[x] ++ , sum[y] ++ , sum[z] -- , sum[fa[z][0]] -- ;printf("%d\n" , solve(1));return 0;
}

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...