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
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
Sample Output
9
差分是一种非常好用的方法。树状数组就是差分的一个非常好的应用。
树上差分顾名思义,就是在一颗树上进行差分。这道题中是对树上的每个点进行差分。
记录下每个点的差分值,对于每次修改,
,使
和
的差分值加一,而
和
的父亲节点的差分值减一。
对于这个图来说,我们记录点的权值为
,点
的差分值为
,如果要使
到
的权值加一,那么显然
要减一,而
和
都要加一,
因为
改变了所以也要减一。即
++,
++,
--,
--。
树状数组中我们在差分后想要知道某个节点的值只需要求一遍前缀和就好了。
在树上差分后,每个点的权值是其子树中差分值的和加上他本身的差分值。所以可以直接dfs一遍,统计出每个点的权值然后找到最大值即可。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
inline int read()
{
int sum=0;
char ch =getchar();
while(ch<'0'||ch>'9')
ch=getchar();
while(ch>='0'&&ch<='9')
{
sum=sum*10+ch-'0';
ch=getchar();
}
return sum;
}
void write(int x)
{
if(x>9) write(x/10);
putchar(x%10+'0');
}
int n,k,tot=0;
int Head[50005],Depth[50005],p[50005][20],w[50005],ans[50005];
struct tree
{
int next,node;
}h[500005];
inline void add(int u,int v)
{
h[++tot].next=Head[u];
h[tot].node=v;
Head[u]=tot;
h[++tot].next=Head[v];
h[tot].node=u;
Head[v]=tot;
}
void dfs(int pos)
{
for(register int i=Head[pos];i;i=h[i].next)
{
int v=h[i].node;
if(p[pos][0]!=v)
{
p[v][0]=pos;
Depth[v]=Depth[pos]+1;
dfs(v);
}
}
}
int lca(int a,int b)
{
if(Depth[a]>Depth[b]) swap(a,b);
int delta=Depth[b]-Depth[a];
for(register int i=0;(1<<i)<=delta;++i)
if((1<<i)&delta)
b=p[b][i];
if(a!=b)
{
for(register int i=(int)log2(n);i>=0;--i)
if(p[a][i]!=p[b][i])
a=p[a][i],b=p[b][i];
a=p[a][0];
}
return a;
}
inline void change(int s,int t)
{
int l=lca(s,t);
++w[s];
++w[t];
--w[l];
--w[p[l][0]];
}
void query(int x)
{
ans[x]+=w[x];
for(register int i=Head[x];i;i=h[i].next)
{
int v=h[i].node;
if(p[x][0]!=v)
{
query(v);
ans[x]+=ans[v];
}
}
}
int main()//LCA+树上差分
{
n=read();k=read();
for(register int i=1;i<n;++i)
add(read(),read());
p[1][0]=1;
Depth[1]=1;
dfs(1);
for(register int j=1;(1<<j)<=n;++j)
for(register int i=1;i<=n;++i)
p[i][j]=p[p[i][j-1]][j-1];
for(register int i=1;i<=k;++i)
change(read(),read());
query(1);
int maxn=0;
for(register int i=1;i<=n;++i)
maxn=max(maxn,ans[i]);
cout<<maxn<<endl;
return 0;
}//EL PSY CONGROO