洛谷 P3128 [USACO15DEC] 最大流Max Flow

15 篇文章 0 订阅

题目描述

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 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
输出样例#1:
9


























~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lca+树上差分~

刚开始写了个T到飞起的裸lca……后来才知道还有树上差分这种神奇的东西……

树上差分:对于路径x到y,k为lca(x,y),那么c[x]++,c[y]++,c[k]--,c[fa[k]]--,最后每个点的权值为自己以及以下所有节点的c[i]之和。

然后按照这个写就可以了~

注意lca更新fa[i][j]时先循环j,再循环i!


正确做法:

#include<cstdio>
#include<iostream>
using namespace std;

int n,m,x,y,k,c[50001],fi[50001],w[100001],ne[100001],cnt,fa[50001][17],dep[50001],ans;

int read()
{
	int totnum=0;char ch=getchar();
	while(ch<'0' || ch>'9') ch=getchar();
	while(ch>='0' && ch<='9') {totnum=(totnum<<1)+(totnum<<3)+ch-'0';ch=getchar();}
	return totnum;
}

void add(int u,int v)
{
	w[++cnt]=v;ne[cnt]=fi[u];fi[u]=cnt;
	w[++cnt]=u;ne[cnt]=fi[v];fi[v]=cnt;
}

void dfs(int u)
{
	for(int i=fi[u];i;i=ne[i])
	  if(fa[u][0]!=w[i])
	  {
	  	fa[w[i]][0]=u;dep[w[i]]=dep[u]+1;dfs(w[i]);
	  }
}

int lca(int u,int v)
{
	if(dep[u]<dep[v]) swap(u,v);
	for(int i=16;~i;i--) if(dep[fa[u][i]]>=dep[v]) u=fa[u][i];
	if(u==v) return u;
	for(int i=16;~i;i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
	return fa[u][0];
}

int cal(int u)
{
	int now=c[u];
	for(int i=fi[u];i;i=ne[i])
	  if(fa[u][0]!=w[i]) now+=cal(w[i]);
	ans=max(ans,now);
	return now;
}

int main()
{
	n=read();m=read();
	for(int i=1;i<n;i++)
	{
		x=read();y=read();add(x,y);
	}
	dep[1]=1;dfs(1);
	for(int j=1;j<=16;j++)
	  for(int i=1;i<=n;i++) fa[i][j]=fa[fa[i][j-1]][j-1];
	while(m--)
	{
		x=read();y=read();
		k=lca(x,y);
		c[x]++;c[y]++;c[k]--;c[fa[k][0]]--;
	}
	cal(1);
	printf("%d\n",ans);
	return 0;
}


裸lca(51分):

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

int n,m,x,y,fi[50001],w[100001],ne[100001],fa[50001][17],cnt,dep[50001],du[50001],ans;

int read()
{
	int totnum=0;char ch=getchar();
	while(ch<'0' || ch>'9') ch=getchar();
	while(ch>='0' && ch<='9') {totnum=(totnum<<1)+(totnum<<3)+ch-'0';ch=getchar();}
	return totnum;
}

void add(int u,int v)
{
	w[++cnt]=v;ne[cnt]=fi[u];fi[u]=cnt;
	w[++cnt]=u;ne[cnt]=fi[v];fi[v]=cnt;
}

void dfs(int u)
{
	for(int i=fi[u];i;i=ne[i])
	  if(fa[u][0]!=w[i])
	  {
	  	dep[w[i]]=dep[u]+1;fa[w[i]][0]=u;dfs(w[i]);
	  }
}

int lca(int u,int v)
{
	if(dep[u]<dep[v]) swap(u,v);
	for(int i=16;~i;i--) if(dep[fa[u][i]]>=dep[v]) u=fa[u][i];
	if(u==v) return u;
	for(int i=16;~i;i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
	return fa[u][0];
}

int main()
{
	n=read();m=read();
	for(int i=1;i<n;i++)
	{
		x=read();y=read();add(x,y);
	}
	dep[1]=1;dfs(1);
	for(int j=1;j<=16;j++)
	  for(int i=1;i<=n;i++) fa[i][j]=fa[fa[i][j-1]][j-1];
	while(m--)
	{
		x=read();y=read();
		int k=lca(x,y);
		du[k]++;ans=max(ans,du[k]);
		while(x!=k)
		{
			du[x]++;ans=max(ans,du[x]);x=fa[x][0];
		}
		while(y!=k)
		{
			du[y]++;ans=max(ans,du[y]);y=fa[y][0];
		}
	}
	printf("%d\n",ans);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值