洛谷 P3128 [USACO15DEC]最大流Max Flow

本文介绍了一种使用树上差分的方法来解决在给定的树形结构中找出承受最大运输压力的节点的问题。具体地,通过预处理得到最低公共祖先(LCA),并利用DFS遍历树来计算每个节点的累积压力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

Farmer John has installed a new system of pipes to transport milk between the stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

FJ is pumping milk between pairs of stalls (). For the th such pair, you are told two stalls and , 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 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 to , then it counts as being pumped through the endpoint stalls and

, as well as through every stall along the path between them.

FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N。所有隔间都被管道连通了。

FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。

输入输出格式

输入格式:

The first line of the input contains and .

The next lines each contain two integers and () describing a pipe

between stalls and .

The next lines each contain two integers and describing the endpoint

stalls of a path through which milk is being pumped.

输出格式:

An integer specifying the maximum amount of milk pumped through any stall in the

barn.

输入输出样例

输入样例#1:

5 103 41 54 25 45 45 43 54 34 31 33 55 41 53 4

输出样例#1:

9


与运输计划那道题类似,树上差分。

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
const int N=50005;
int n,m,cnt,hd[N],dep[N],f[N][21],ac[N];
struct edge
{
	int to,nxt;
}v[2*N];
void addedge(int x,int y)
{
	++cnt;
	v[cnt].to=y;
	v[cnt].nxt=hd[x];
	hd[x]=cnt;
}
void init(int u,int fa)
{
	dep[u]=dep[fa]+1;
	f[u][0]=fa;
	for(int i=hd[u];i;i=v[i].nxt)
		if(v[i].to!=fa)
			init(v[i].to,u);
}
int lca(int a,int b)
{
	if(dep[a]<dep[b])
		swap(a,b);
	for(int i=16;i>=0;i--)
		if(dep[f[a][i]]>=dep[b])
			a=f[a][i];
	if(a==b)
		return a;
	for(int i=16;i>=0;i--)
		if(f[a][i]!=f[b][i])
			a=f[a][i],b=f[b][i];
	return f[a][0];
}
void dfs(int u,int fa)
{
	for(int i=hd[u];i;i=v[i].nxt)
		if(v[i].to!=fa)
		{
			dfs(v[i].to,u);
			ac[u]+=ac[v[i].to];
		}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n-1;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		addedge(x,y);
		addedge(y,x);
	}
	init(1,0);
	for(int i=1;i<=16;i++)
		for(int j=1;j<=n;j++)
			f[j][i]=f[f[j][i-1]][i-1];
	while(m--)
	{
		int x,y,z;
		scanf("%d%d",&x,&y);
		ac[x]++,ac[y]++;
		z=lca(x,y);
		ac[z]--,ac[f[z][0]]--;
	}
	dfs(1,0);
	printf("%d\n",*max_element(ac+1,ac+n+1));
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值