洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]

时间:2022-10-09 22:44:53

题目描述

Farmer John has installed a new system of 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] pipes to transport milk between the 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] stalls in his barn (洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]), conveniently numbered 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]. Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

FJ is pumping milk between 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] pairs of stalls (洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]). For the 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]th such pair, you are told two stalls 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] and 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA], 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 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]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 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] to 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA], then it counts as being pumped through the endpoint stalls 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] and

洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA], 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 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] and 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA].

The next 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] lines each contain two integers 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] and 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] (洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]) describing a pipe

between stalls 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] and 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA].

The next 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] lines each contain two integers 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] and 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA] 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

倍增LCA+树上差分。

树结点的权值等于其子树所有节点的差分结果。

704ms,不知道用树剖求LCA会有多快

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v){
e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
}
int n,k;
int dep[mxn];
int fa[mxn][];
int a[mxn];//差分
void DFS(int u,int f){
dep[u]=dep[f]+;
for(int i=;i<;i++)fa[u][i]=fa[fa[u][i-]][i-];
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==f)continue;
fa[v][]=u;
DFS(v,u);
}
return;
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=;i>=;i--){
if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
}
if(x==y)return x;
for(int i=;i>=;i--){
if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
}
return fa[x][];
}
int ans=-1e9;
int clc(int u,int f){
int res=a[u];
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==f)continue;
res+=clc(v,u);
}
ans=max(ans,res);
return res;
}
int main(){
n=read();k=read();
int i,j,x,y;
for(i=;i<n;i++){
x=read();y=read();
add_edge(x,y);
add_edge(y,x);
}
DFS(,);
for(i=;i<=k;i++){
x=read();y=read();
a[x]++;a[y]++;
int tmp=LCA(x,y);
a[tmp]--;
if(fa[tmp][])a[fa[tmp][]]--;
}
clc(,);
printf("%d\n",ans);
return ;
}