zoukankan      html  css  js  c++  java
  • luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)

    题目描述

    Farmer John has installed a new system of N-1N1 pipes to transport milk between the NN stalls in his barn (2 leq N leq 50,0002N50,000), conveniently numbered 1 ldots N1N. 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 leq K leq 100,0001K100,000). For the iith such pair, you are told two stalls s_isi and t_iti, 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 s_isi to t_iti, then it counts as being pumped through the endpoint stalls s_isi and

    t_iti, as well as through every stall along the path between them.

    FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N。所有隔间都被管道连通了。

    FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。

    输入输出格式

    输入格式:

    The first line of the input contains NN and KK.

    The next N-1N1 lines each contain two integers xx and yy (x e yxy) describing a pipe

    between stalls xx and yy.

    The next KK lines each contain two integers ss and tt describing the endpoint

    stalls of a path through which milk is being pumped.

    输出格式:

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

    barn.

    输入输出样例

    输入样例#1: 复制
    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
    输出样例#1: 复制
    9

    思路:
    和区间上的差分写法差不多,把u-v划分成两条链, u->lca(u,v) lca(u,v)->v,,因为是点差所以我们对u点+1,v点+1,lca(u,v)-1,fa(lca(u,v))-1.最后跑个dfs求出最大值。

    实现代码:
    #include<bits/stdc++.h>
    using namespace std;
    
    const int M = 2e5 + 10;
    int p[M][30],w[M],dep[M],cnt,head[M],n,ans;
    struct node{
        int to,next;
    }e[M];
    
    void Add(int u,int v){
        e[++cnt].to = v;e[cnt].next = head[u];head[u] = cnt;
    }
    
    void dfs(int u,int fa,int deep){
        dep[u] = deep;
        p[u][0] = fa;
        for(int i = head[u];i;i=e[i].next){
            int v = e[i].to;
            if(v == fa) continue;
            dfs(v,u,deep+1);
        }
    }
    
    void get_fa(){
        for(int j = 1;(1<<j)<=n;j++)
            for(int i = 1;i <= n;i ++)
                p[i][j] = p[p[i][j-1]][j-1];
    }
    
    int lca(int a,int b){
        if(dep[a] > dep[b]) swap(a,b);
        int h = dep[b] - dep[a];
        for(int i = 0;(1<<i)<=h;i++){
            if((1<<i)&h) b = p[b][i];
        }
        if(a != b){
            for(int i = 22;i >= 0;i --){
                if(p[a][i] != p[b][i]){
                    a = p[a][i]; b = p[b][i];
                }
            }
            a = p[a][0];
        }
        return a;
    }
    
    void dfs1(int u,int fa){
        for(int i = head[u];i;i = e[i].next){
            int v = e[i].to;
            if(v == fa) continue;
            dfs1(v,u);
            w[u] += w[v];
        }
        ans = max(ans,w[u]);
    }
    
    int main()
    {
        int m,u,v;
        scanf("%d%d",&n,&m);
    
        for(int i = 1;i < n;i ++){
            scanf("%d%d",&u,&v);
            Add(u,v); Add(v,u);
        }
        dfs(1,0,1); get_fa();
        for(int i = 1;i <= m;i ++){
            scanf("%d%d",&u,&v);
            int k = lca(u,v);
            w[u]++; w[v]++; w[k]--;
            w[p[k][0]]--;
        }
        dfs1(1,0);
        printf("%d
    ",ans);
    }
  • 相关阅读:
    解决win10家庭版打不开组策略gpedit.msc
    Centos7上安装配置Redis
    Typora配置图片自动上传到图床
    SSM静态资源访问不了问题
    SSM整合
    opencv基础学习 小知识--绘图+小实战训练
    opencv基础学习详细笔记【1】--读取并显示图片
    Scikit-Learn 源码研读 (第二期)基类的实现细节
    Scikit-Learn 源码研读 (第一期)项目结构介绍
    在Win10上搭建fastai课程环境
  • 原文地址:https://www.cnblogs.com/kls123/p/9990302.html
Copyright © 2011-2022 走看看