BZOJ4390: [Usaco2015 dec]Max Flow-树上差分-LCA

有任何问题欢迎留言或私聊

题目链接:BZOJ4390

题目:

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.

题意:

 一颗有N 个节点的树,输入N-1条边;然后输入P次操作,每次将a,b路径上每个点的权值加一(权值初始为0)。求最后权值最大的点的权值。

思路:

 1.建树,LCA搞上。
 2.sum[i]维护 i 点的虚权值。读入a,b后,sum[a]和sum[b]加一,a
 和b的LCA点Fa,sum[Fa]和sum[fa[Fa]]减一
 3.最后搜索一遍,每个点的真权值等于该节点及其子树的sum之和。取最大值。

AC代码:

#include<bits/stdc++.h> 
#define mm1(a) memset((a),-1,sizeof((a)))  
#define mm0(a) memset((a),0,sizeof((a)))  
#define mmx(a) memset((a),0x3f,sizeof((a)))  
using namespace std;
const int N = 100005;
int n,p,tot,ans;
int fa[N],head[N];
int sum[N],dep[N],size[N];
int f[N][30];
struct lp
{
    int v,nex;
}cw[N*10];
void add(int x,int y){
    cw[++tot].v=y;cw[tot].nex=head[x];
    head[x]=tot;
}
void init(){
    ans=0;tot=-1;
    mm0(f);mm0(fa);mm0(sum);mm0(dep);mm0(size);
    mm1(head);
}
void dfs(int u,int Fa,int h){
    dep[u]=h;f[u][0]=Fa;fa[u]=Fa;
    for(int i=1;i<20;++i){
        int cf=f[u][i-1];
        f[u][i]=f[cf][i-1];
    }
    for(int i=head[u];~i;i=cw[i].nex){
        int v=cw[i].v;
        if(v==Fa)continue;
        dfs(v,u,h+1);
    }
}
int lca(int x,int y){
    if(dep[x]<dep[y])x^=y^=x^=y;
    for(int i=19;i>=0;--i){
        if(dep[f[x][i]]>=dep[y]){
            x=f[x][i];
        }
    }
    if(x==y)return x;
    for(int i=19;i>=0;--i){
        if(f[x][i]!=f[y][i]){
            x=f[x][i];y=f[y][i];
        }
    }
    return f[x][0];
}
void solve(int u,int fa){
    size[u]=sum[u];
    for(int i=head[u];~i;i=cw[i].nex){
        int v=cw[i].v;
        if(v==fa)continue;
        solve(v,u);
        size[u]+=size[v];
    }
    ans=max(ans,size[u]);
}
int main(){
    int x,y;
    while(~scanf("%d%d",&n,&p)){
        init();
        for(int i=0;i<n-1;++i){
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        f[1][0]=1;
        dfs(1,1,1);
        while(p--){
            scanf("%d%d",&x,&y);
            int father = lca(x,y);
            sum[x]++,sum[y]++;
            sum[father]--;
            if(father!=1)sum[fa[father]]--;
        }
        solve(1,1);
        printf("%d\n",ans );
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值