您的位置:首页 > 其它

USACO 2015 Dec Platinum 1.Max Flow

2017-08-15 09:43 423 查看

Description

Farmer John has installed a new system of

pipes to transport milk betweenthe
stalls in his barn (),
conveniently numbered. Each pipe connects a pair of stalls,
and all stalls areconnected to each-other via paths of pipes.
FJ is pumping milk between

pairs of stalls (). Forthe
th such pair, you are told two stalls
and
, endpoints of apath along which milk is being pumped at a
unit rate. FJ is concerned that somestalls 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 whichmilk is being pumped. Please help him determine
the maximum amount of milkbeing 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.

Input

The first line of the input contains

and .
The next

lines each contain two integers
and
()
describing a pipebetween stalls
and

.

The next

lines each contain two integers
and

describing the endpointstalls of a path through which milk is being pumped.

Output

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

Sample Input

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

Sample Output

9

#include<stdio.h>
int n,k,x,y,s,t,r,ans,idx,point[500001],next[1000001],v[1000001],h[500001],fa[500001],sum[500001],size[500001],f[500001][21];
void addedge(int x,int y)
{
next[++idx]=point[x];
point[x]=idx;
v[idx]=y;
}
int max(int a,int b)
{
if(a>b)
return a;
return b;
}
void swap(int &m,int &k)
{
int o=m;
m=k;
k=o;
}
void dfs(int x,int y,int deep)
{
h[x]=deep;
fa[x]=y;
for (int i=1;i<17;i++)
f[x][i]=f[f[x][i-1]][i-1];
for(int i=point[x];i;i=next[i])
if (v[i]!=y)
{
f[v[i]][0]=x;
dfs(v[i],x,deep+1);
}
}
int lca(int x,int y)
{
if(h[x]<h[y])
swap(x,y);
for(int i=17;i>=0;i--)
while (h[f[x][i]]>=h[y])
x=f[x][i];
if(x==y) return x;
for(int i=17;i>=0;i--)
if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return f[x][0];
}
void dfs2(int x,int fa)
{
size[x]=sum[x];
for(int i=point[x];i;i=next[i])
if(v[i]!=fa)
{
dfs2(v[i],x);
size[x]+=size[v[i]];
}
ans=max(ans,size[x]);
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
dfs(1,0,1);
for(int i=1;i<=k;i++)
{
scanf("%d%d",&s,&t);
r=lca(s,t);
sum[s]++;
sum[t]++;
sum[r]--;
if(r!=1)
sum[fa[r]]--;
}
dfs2(1,0);
printf("%d\n",ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: