题目描述
Farmer John has installed a new system of pipes to transport milk between the stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.
FJ is pumping milk between pairs of stalls (). For the th such pair, you are told two stalls and , 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 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 to , then it counts as being pumped through the endpoint stalls and
, 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 and .
The next lines each contain two integers and () describing a pipe
between stalls and .
The next lines each contain two integers and 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
虽然名叫Max Flow,但是和网络流半点关系没有。
正解似乎是利用树剖求LCA,然后按照差分权值。(树上差分,结点权值等于其子树的差分累计结果)
然而并没有多想就写了树剖+线段树暴力维护链修改和区间最大值……
1825ms龟速水过。
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
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;
}
int n,k;
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;
}
struct node{
int f,son;
int top,size,dep;
int w,e;
}tr[mxn];
struct segtree{
int mk,mx;
}st[mxn<<];
int sz=;
void DFS1(int u){
tr[u].size=;
tr[u].son=;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==tr[u].f)continue;
tr[v].f=u;
tr[v].dep=tr[u].dep+;
DFS1(v);
tr[u].size+=tr[v].size;
if(tr[v].size>tr[tr[u].son].size)tr[u].son=v;
}
return;
}
void DFS2(int u,int top){
tr[u].top=top;
tr[u].w=++sz;
if(tr[u].son){
DFS2(tr[u].son,top);
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v!=tr[u].f && v!=tr[u].son)
DFS2(v,v);
}
}
tr[u].e=sz;
return;
}
void pushdown(int l,int r,int rt){
if(l==r)return;
int mid=(l+r)>>;
st[rt<<].mk+=st[rt].mk;
st[rt<<|].mk+=st[rt].mk;
st[rt<<].mx+=st[rt].mk;
st[rt<<|].mx+=st[rt].mk;
st[rt].mk=;
return;
}
void add(int L,int R,int v,int l,int r,int rt){
if(L<=l && r<=R){
st[rt].mk+=v;
st[rt].mx+=v;
return;
}
if(st[rt].mk)pushdown(l,r,rt);
int mid=(l+r)>>;
if(L<=mid)add(L,R,v,l,mid,rt<<);
if(R>mid)add(L,R,v,mid+,r,rt<<|);
st[rt].mx=max(st[rt<<].mx,st[rt<<|].mx);
return;
}
int qmx(int L,int R,int l,int r,int rt){
if(L<=l && r<=R)return st[rt].mx;
if(st[rt].mk)pushdown(l,r,rt);
int mid=(l+r)/;
int res=-1e9;
if(L<=mid)res=max(res,qmx(L,R,l,mid,rt<<));
if(R>mid)res=max(res,qmx(L,R,mid+,r,rt<<|));
return res;
}
void qadd(int x,int y){
while(tr[x].top!=tr[y].top){
if(tr[tr[x].top].dep<tr[tr[y].top].dep)swap(x,y);
add(tr[tr[x].top].w,tr[x].w,,,n,);
x=tr[tr[x].top].f;
}
if(tr[x].w>tr[y].w)swap(x,y);
add(tr[x].w,tr[y].w,,,n,);
return;
}
int main(){
n=read();k=read();
int i,j,u,v,x,y;
for(i=;i<n;i++){
u=read();v=read();
add_edge(u,v);
add_edge(v,u);
}
DFS1();
DFS2(,);
for(i=;i<=k;i++){
/*
for(i=1;i<=sz;i++){
printf("%d ",qmx(tr[i].w,tr[i].w,1,n,1));
}
printf("\n");*/
x=read();y=read();
qadd(x,y);
}
printf("%d\n",st[].mx);
return ;
}