【bzoj 4390】 [Usaco2015 dec]Max Flow(树上差分)
4390: [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 156 Solved: 100
[Submit][Status][Discuss]
Description
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次操作完毕后权值最大的那个点的权值。
Input
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.
Output
An integer specifying the maximum amount of milk pumped through any stall in the barn.
Sample Input
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
Sample Output
HINT
Source
【题解】【树上差分】
【在树中将所有路径起、始权值加1,起、始点的lca权值减2,从所有叶节点开始把权值往上累加。最终权值为路径数的点到其父亲的边为所求边。】
[写树链剖分T掉了。。。然后才知道还有这么一种神奇的方式]
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[100010],nxt[100010],p[50010],tot;
int f[50010][20],father[50010],dep[50010],size[50010],sum[50010];
int mi[20],n,k,ans;
inline void add(int x,int y)
{
tot++; a[tot]=y; nxt[tot]=p[x]; p[x]=tot;
tot++; a[tot]=x; nxt[tot]=p[y]; p[y]=tot;
}
void dfs(int x,int fa,int h)
{
dep[x]=h; father[x]=fa;
for(int i=1;i<16;++i) f[x][i]=f[f[x][i-1]][i-1];
for(int i=p[x];i!=-1;i=nxt[i])
if(a[i]!=fa)
{
f[a[i]][0]=x;
dfs(a[i],x,h+1);
}
}
inline int lca(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
for(int i=15;i>=0;--i)
while(dep[f[x][i]]>=dep[y])
x=f[x][i];
if(x==y) return x;
for(int i=15;i>=0;--i)
if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return f[x][0];
}
inline void solve(int x,int fa)
{
size[x]=sum[x];
for(int i=p[x];i!=-1;i=nxt[i])
if(a[i]!=fa)
{
solve(a[i],x);
size[x]+=size[a[i]];
}
ans=max(ans,size[x]);
}
int main()
{
//freopen("int.txt","r",stdin);
//freopen("my.txt","w",stdout);
int i,j;
memset(p,-1,sizeof(p));
memset(nxt,-1,sizeof(nxt));
scanf("%d%d",&n,&k);
for(i=1;i<n;++i)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
}
dfs(1,0,1);
for(i=1;i<=k;++i)
{
int x,y;
scanf("%d%d",&x,&y);
int l=lca(x,y);
sum[x]++; sum[y]++;
sum[l]--;
if(l!=1) sum[father[l]]--;
}
solve(1,0);
printf("%d",ans);
return 0;
}