USACO 2015 Dec Platinum 1.Max Flow

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);
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值