P3128 [USACO15DEC]最大流Max Flow &&bzoj 4390

4390: [Usaco2015 dec]Max Flow

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



树上差分
每次修改tr[u]++,tr[v]++,tr[ lca ]++,tr[ fa [ lca ] ]++
最后自底向上维护一下即可
#include<cstdio>
#include<cstring>
int read()
{
	int ans=0,f=1;char t=getchar();
	while(t<'0'||t>'9')	f=(t=='-'?-1:1),t=getchar();
	while(t>='0'&&t<='9')	ans=ans*10+t-'0',t=getchar();
	return ans*f;
}
const int N=1e5+7,M=N<<1;
struct node
{
	int to,next;
}e[M];
int first[N],cnt;int dep[N];
int qu[M],log[M],f[M][23];
inline void insert(int u,int v)
{
	e[++cnt]=(node){v,first[u]};first[u]=cnt;
	e[++cnt]=(node){u,first[v]};first[v]=cnt;
}
inline void swap(int &a,int &b)
{
	int tmp=a;a=b,b=tmp;
}
inline int minn(int a,int b)
{
	if(dep[a]<dep[b])	return a;
	return b;
}
int tar[N],lg[M],visit[N];
inline int lca(int l,int r)  
{  
    l=tar[l],r=tar[r];  
    if(l>r)  swap(l,r);  
    int t=lg[r-l+1];  
    return minn(f[l][t],f[r-(1<<t)+1][t]);  
}  
int fa[N];
void dfs(int x)
{
	visit[x]=1;qu[++cnt]=x;tar[x]=cnt;
	for(int k=first[x];k;k=e[k].next)
	if(!visit[e[k].to])
	{
		dep[e[k].to]=dep[x]+1;
		fa[e[k].to]=x;
		dfs(e[k].to);
		qu[++cnt]=x;
	}
}
inline void init()
{
    lg[1]=0;  
    for(int i=2;i<=cnt;i++)      lg[i]=lg[i>>1]+1;   
    for(int i=1;i<=cnt;i++)  f[i][0]=qu[i];  
      
    for(int j=1;j<=22;j++)  
    for(int i=1;i<=cnt-( 1<<(j -1) );i++)    
    f[i][j]=minn(f[i][j-1],f[ i+( 1<< (j-1) )][ j-1 ]);  
}
int tr[N];
int ans[N];
void dfs2(int x)
{
	visit[x]=1;ans[x]=tr[x];
	for(int k=first[x];k;k=e[k].next)		
	if(!visit[e[k].to])
	{
		dfs2(e[k].to);
		ans[x]+=ans[e[k].to];
	}
}
#define test(a) printf("std::%d\n",a)
int main()
{
	int n,k;
	n=read(),k=read();int u,v;
	for(int i=1;i<n;i++)	u=read(),v=read(),insert(u,v);
	cnt=0;
	dfs(1);
	init();
	for(int i=1;i<=k;i++)
	{
		u=read(),v=read();
		tr[u]++,tr[v]++;
		int lc=lca(u,v);
		tr[lc]--,tr[fa[lc]]--;
	}
	memset(visit,0,sizeof(visit));
	dfs2(1);
	int max=0;
	for(int i=1;i<=n;i++)	if(ans[i]>max)	max=ans[i];
	printf("%d\n",max);
	return 0;
}


以下是P4087 [USACO17DEC]Milk Measurement的c++代码: ```c++ #include<bits/stdc++.h> using namespace std; int n,d,i,x,minn=1e9,maxn=-1e9,sum=7;//注意sum要初始化为7,因为一开始有三个人挤奶! map<int,int> mp; struct node{ int day,milk,id;//day表示某一天,milk表示这一天的产奶量,id表示这头牛的编号 }a[100010]; bool cmp(node x,node y){ return x.day<y.day; } int main(){ scanf("%d%d",&n,&d); for(i=1;i<=n;i++){ scanf("%d%d%d",&a[i].day,&a[i].id,&a[i].milk); minn=min(minn,a[i].id);//记录最小的牛的编号 maxn=max(maxn,a[i].id);//记录最大的牛的编号 } sort(a+1,a+n+1,cmp);//排序 for(i=1;i<=n;i++){ int p=a[i].id; mp[p]+=a[i].milk;//记录每头牛产奶总量 if(mp[p]-a[i].milk>=mp[minn]&&mp[p]>=mp[minn]){//如果这头牛的产奶总量减去这一天的产奶量后等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 sum--; } if(mp[p]>=mp[maxn]&&mp[p]-a[i].milk<mp[maxn]){//如果这头牛的产奶总量大于等于最大产奶量且这头牛的产奶总量减去这一天的产奶量小于最大产奶量 sum++; } if(mp[p]-a[i].milk<mp[maxn]&&mp[p]>=mp[maxn]){//如果这头牛的产奶总量减去这一天的产奶量小于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]-mp[p]+a[i].milk>0)sum++; } mp[p]-=a[i].milk;//减去这一天的产奶量 if(i==n||a[i].day!=a[i+1].day){//如果到了新的一天或者到了最后一天 if(mp[maxn]!=mp[a[i].id]&&mp[a[i].id]>=mp[maxn])sum++;//如果这头牛的产奶总量不等于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]==mp[a[i].id]){//如果这头牛的产奶总量等于最大产奶量 if(a[i].id==maxn)sum+=0;//如果这头牛就是最大产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } if(mp[minn]!=mp[a[i].id]&&mp[a[i].id]>=mp[minn])sum++;//如果这头牛的产奶总量不等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 if(mp[minn]==mp[a[i].id]){ if(a[i].id==minn)sum+=0;//如果这头牛就是最小产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } } } printf("%d\n",sum); return 0; } ``` 该题的解题思路是模拟,需要注意细节问题。我们可以首先将输入的数据按天数排序,然后模拟每一天挤奶的情况,并根据题目要求进行计数即可。具体细节请见代码注释。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值