bzoj4390 Max Flow

177 篇文章 1 订阅

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.

对每条路径求lca打标记,最后dfs一遍统计。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int fir[50010],ne[100010],to[100010],fa[50010][20],tag[50010],dep[50010],n,m,ans=-1;
void add(int num,int u,int v)
{
    ne[num]=fir[u];
    fir[u]=num;
    to[num]=v;
}
void dfs1(int u)
{
    int v;
    for (int i=fir[u];i;i=ne[i])
        if ((v=to[i])!=fa[u][0])
        {
            fa[v][0]=u;
            dep[v]=dep[u]+1;
            dfs1(v);
        }
}
int lca(int u,int v)
{
    if (dep[u]<dep[v]) swap(u,v);
    for (int k=17;k>=0;k--)
        if (dep[u]-(1<<k)>=dep[v]) u=fa[u][k];
    if (u==v) return u;
    for (int k=17;k>=0;k--)
        if (fa[u][k]!=fa[v][k])
        {
            u=fa[u][k];
            v=fa[v][k];
        }
    return fa[u][0];
}
void dfs2(int u)
{
    int v;
    for (int i=fir[u];i;i=ne[i])
        if ((v=to[i])!=fa[u][0])
        {
            dfs2(v);
            tag[u]+=tag[v];
        }
    ans=max(ans,tag[u]);
}
int main()
{
    int u,v,x;
    scanf("%d%d",&n,&m);
    for (int i=1;i<n;i++)
    {
        scanf("%d%d",&u,&v);
        add(i*2,u,v);
        add(i*2+1,v,u);
    }
    dep[1]=1;
    dfs1(1);
    for (int k=1;k<=17;k++)
        for (int i=1;i<=n;i++)
            fa[i][k]=fa[fa[i][k-1]][k-1];
    while (m--)
    {
        scanf("%d%d",&u,&v);
        x=lca(u,v);
        tag[u]++;
        tag[v]++;
        tag[x]--;
        tag[fa[x][0]]--;
    }
    dfs2(1);
    printf("%d\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值