【bzoj 4390】 [Usaco2015 dec]Max Flow(树上差分)

33 篇文章 0 订阅
4 篇文章 0 订阅

4390: [Usaco2015 dec]Max Flow

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 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

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

HINT

Source

[ Submit][ Status][ Discuss]

【题解】【树上差分】

在树中将所有路径起、始权值加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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值