BZOJ 4390: [Usaco2015 dec]Max Flow

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

题目链接

 

差分是一种非常好用的方法。树状数组就是差分的一个非常好的应用。

树上差分顾名思义,就是在一颗树上进行差分。这道题中是对树上的每个点进行差分。

记录下每个点的差分值,对于每次修改st,使st的差分值加一,而LCA(s,t)LCA(s,t)的父亲节点的差分值减一。

                                                        

对于这个图来说,我们记录点i的权值为w[i],点i的差分值为s[i],如果要使45的权值加一,那么显然s[2]=w[2]-w[4]-w[5]要减一,而s[4]s[5]都要加一,s[1]=w[1]-w[2]-w[3]因为w[2]改变了所以也要减一。即s[4]++,s[5]++,s[2]--,s[1]--。

树状数组中我们在差分后想要知道某个节点的值只需要求一遍前缀和就好了。

在树上差分后,每个点的权值是其子树中差分值的和加上他本身的差分值。所以可以直接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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值