bzoj4390【Usaco2015 Dec】Max Flow

4390: [Usaco2015 dec]Max Flow

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




#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define maxn 50005
#define inf 1000000000
using namespace std;
struct edge_type{int to,next;}e[maxn*2];
struct seg{int l,r,mx,tag;}t[maxn*4];
int fa[maxn][20],d[maxn],size[maxn],head[maxn],pos[maxn],belong[maxn];
int n,m,x,y,tmp,cnt,tot;
bool vst[maxn];
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline void add_edge(int x,int y)
{
	e[++cnt]=(edge_type){y,head[x]};head[x]=cnt;
	e[++cnt]=(edge_type){x,head[y]};head[y]=cnt;
}
inline void dfs1(int x)
{
	size[x]=1;vst[x]=true;
	F(i,1,20)
	{
		if (d[x]<(1<<i)) break;
		fa[x][i]=fa[fa[x][i-1]][i-1];
	}
	for(int i=head[x];i;i=e[i].next)
	{
		int y=e[i].to;
		if (vst[y]) continue;
		d[y]=d[x]+1;fa[y][0]=x;
		dfs1(y);
		size[x]+=size[y];
	}
}
inline void dfs2(int x,int chain)
{
	int k=0;
	pos[x]=++tot;belong[x]=chain;
	for(int i=head[x];i;i=e[i].next)
		if (d[e[i].to]>d[x]&&size[e[i].to]>size[k]) k=e[i].to;
	if (k==0) return;
	dfs2(k,chain);
	for(int i=head[x];i;i=e[i].next)
		if (d[e[i].to]>d[x]&&k!=e[i].to) dfs2(e[i].to,e[i].to);
}
inline int lca(int x,int y)
{
	if (d[x]<d[y]) swap(x,y);
	int t=int(log2(d[x]-d[y]));
	D(i,t,0) if (d[x]-(1<<i)>=d[y]) x=fa[x][i];
	if (x==y) return x;
	t=int(log2(d[x]));
	D(i,t,0) if (fa[x][i]!=fa[y][i])
	{
		x=fa[x][i];
		y=fa[y][i];
	}
	return fa[x][0];
}
inline void pushup(int k)
{
	t[k].mx=max(t[k<<1].mx,t[k<<1|1].mx);
}
inline void update(int k,int x)
{
	t[k].mx+=x;t[k].tag+=x;
}
inline void pushdown(int k)
{
	if (!t[k].tag) return;
	update(k<<1,t[k].tag);update(k<<1|1,t[k].tag);
	t[k].tag=0;
}
inline void build(int k,int l,int r)
{
	t[k].l=l;t[k].r=r;t[k].tag=t[k].mx=0;
	if (l==r) return;
	int mid=(l+r)>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
}
inline void add(int k,int l,int r,int x)
{
	if (t[k].l==l&&t[k].r==r){update(k,x);return;}
	int mid=(t[k].l+t[k].r)>>1;
	pushdown(k);
	if (r<=mid) add(k<<1,l,r,x);
	else if (l>mid) add(k<<1|1,l,r,x);
	else {add(k<<1,l,mid,x);add(k<<1|1,mid+1,r,x);}
	pushup(k);
}
inline void solveadd(int x,int f)
{
	while (belong[x]!=belong[f])
	{
		add(1,pos[belong[x]],pos[x],1);
		x=fa[belong[x]][0];
	}
	add(1,pos[f],pos[x],1);
}
int main()
{
	n=read();m=read();
	F(i,1,n-1){x=read();y=read();add_edge(x,y);}
	dfs1(1);
	dfs2(1,1);
	build(1,1,n);
	F(i,1,m)
	{
		x=read();y=read();
		tmp=lca(x,y);
		solveadd(x,tmp);solveadd(y,tmp);
		add(1,pos[tmp],pos[tmp],-1);
	}
	printf("%d\n",t[1].mx);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值