[Usaco2015 dec]Max Flow

Description

Farmer John has installed a new system of N?1 pipes to transport milk between the N stalls in his ba
rn (2≤N≤50,000), conveniently numbered 1…N. Each pipe connects a pair of stalls, and all stalls a
re connected to each-other via paths of pipes.FJ is pumping milk between KK pairs of stalls (1≤K≤1
00,000). For the iith such pair, you are told two stalls sisi and titi, endpoints of a path along wh
ich milk is being pumped at a unit rate. FJ is concerned that some stalls might end up overwhelmed w
ith 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 bein
g pumped through any stall. If milk is being pumped along a path from sisi to titi, then it counts a
s being pumped through the endpoint stalls sisi and titi, as well as through every stall along the p
ath 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

树上差分,这是一道模板题,思路和普通的差分是一样的,就是需要在一棵树上进行差分的操作

那么,肯定的是s和t到他们的最近公共祖先的路径上的点的边权都是要加1的,那么就可以做

sum[s]++,sum[t]++,sum[lca(s,t)]--;

但是如果lca(s,t)不是根节点那么sum[father[lca(s,t)]]--,因为我们在加的时候s和t一共加了

两遍,所以需要把sum[lca(s,t)]--,把sum[father[lca(s,t)]]--就不用我说了吧,学过差分的都知道,如果没学过就没必要来看这个了。

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
int son[100001],nex[100001],ans,pre[100001],fa[100001],f[100001][20],n,m,a,b,root,tot,sum[100001],father[100001],now[100001],deep[100001];
void add(int x,int y)
{
    tot++;
    son[tot]=y;
    nex[tot]=pre[x];
    pre[x]=tot;
}
void dfs(int now,int fa)
{
    father[now]=fa;
    for(int i=1;i<=19;i++)
    {
        if(deep[now]<(1<<i))break;
        f[now][i]=f[f[now][i-1]][i-1];
    }
    for(int i=pre[now];i;i=nex[i])
    if(son[i]!=fa)
    {
        int k=son[i];
        deep[k]=deep[now]+1;
        f[k][0]=now;
        dfs(k,now);
    }
}
int lca(int x,int y)
{
    if(deep[x]>deep[y])swap(x,y);
    int poor=deep[y]-deep[x];
    for(int i=0;i<=19;i++)
        if((1<<i)&poor)
            y=f[y][i];
    for(int i=19;i>=0;i--)
        if(f[x][i]!=f[y][i])
            x=f[x][i],y=f[y][i];
    if(x==y)return x;
    return f[x][0];
}
void get(int x,int fa)
{
    now[x]=sum[x];
    for(int i=pre[x];i;i=nex[i])
    if(son[i]!=fa)
    {
        get(son[i],x);
        now[x]+=now[son[i]];
    }
    ans=max(ans,now[x]);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<n;i++)
        scanf("%d%d",&a,&b),add(a,b),add(b,a);
    dfs(1,0);
    int begin,end;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&begin,&end);
        int r=lca(begin,end);
        sum[begin]++,sum[end]++,sum[r]--;
        if(r!=1)sum[father[r]]--;
    }
    get(1,0);
    printf("%d",ans);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值