zoukankan      html  css  js  c++  java
  • LUOGU P3128 [USACO15DEC]最大流Max Flow

    题目描述

    Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls in his barn ( 2 leq N leq 50,0002≤N≤50,000 ), conveniently numbered 1 ldots N1…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 leq K leq 100,0001≤K≤100,000 ). For the ii th such pair, you are told two stalls s_is
    i
    ​ and t_it
    i
    ​ , 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_is
    i
    ​ to t_it
    i
    ​ , then it counts as being pumped through the endpoint stalls s_is
    i
    ​ and

    t_it
    i
    ​ , 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-1N−1 lines each contain two integers xx and yy ( x e yx≠y ) 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

    解题思路

    一道裸的树上差分,注意这里它给的压力是点,所以应该sum[x]++,sum[y]++,sum[lca(x,y)]–,sum[fa[lca(x,y)] - -

    代码

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    
    using namespace std;
    const int MAXN = 50005;
    
    inline int rd(){
        int x=0,f=1;char ch=getchar();
        while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
        while(isdigit(ch))  {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
        return x*f;
    }
    
    int n,m,head[MAXN],cnt,ans;
    int to[MAXN<<1],nxt[MAXN<<1];
    int sum[MAXN];
    int lca[MAXN];
    int top[MAXN],fa[MAXN],dep[MAXN],siz[MAXN],son[MAXN];
    
    inline void add(int bg,int ed){
        to[++cnt]=ed,nxt[cnt]=head[bg],head[bg]=cnt;
    }
    
    void dfs1(int x,int father,int deep){
        fa[x]=father;dep[x]=deep;siz[x]=1;
        int maxson=-1;
        for(register int i=head[x];i;i=nxt[i]){
            int u=to[i];if(u==father) continue;
            dfs1(u,x,deep+1);
            siz[x]+=siz[u];
            if(siz[u]>maxson)  {son[x]=u;maxson=siz[u];}
        }
    }
    
    void dfs2(int x,int topf){
        top[x]=topf;
        if(!son[x]) return;
        dfs2(son[x],topf);
        for(register int i=head[x];i;i=nxt[i]){
            int u=to[i];
            if(u==fa[x] || u==son[x]) continue;
            dfs2(u,u);
        }
    }
    
    void dfs(int x){
        for(register int i=head[x];i;i=nxt[i]){
            int u=to[i];
            if(u==fa[x]) continue;
            dfs(u);
            sum[x]+=sum[u];
        }
        ans=ans>sum[x]?ans:sum[x];
    }
    
    int main(){
        n=rd();m=rd();
        for(register int i=1;i<n;i++){
            int x=rd(),y=rd();
            add(x,y);add(y,x);
        }
        dfs1(1,0,1);dfs2(1,1);
        for(register int i=1;i<=m;i++){
            int x=rd(),y=rd();sum[x]++;sum[y]++;
            while(top[x]!=top[y]){
                if(dep[top[x]]<dep[top[y]]) swap(x,y);
                x=fa[top[x]];
            }
            int lca=dep[x]>dep[y]?y:x;
            sum[fa[lca]]--;
            sum[lca]--;
        }
        dfs(1);printf("%d",ans);
        return 0;
    }
  • 相关阅读:
    Spark Scala 读取GBK文件的方法
    Mac OS X 系统下自带的文本文件格式转换工具iconv
    报到
    java 字符串中含有双引号" "与单引号' '问题
    div1嵌套div2,div2居中的解决办法
    ionic4 创建 angular项目 ReactNative下载第三方库出错解决Error: EPERM: operation not permitted, rename
    ionic+cordova 创建项目+打包
    jxl读取excel文件异常:Unable to recognize OLE stream 的解决方法
    学习 javascript (一)javascript 简介
    学习 JavaScript (四)核心概念:操作符
  • 原文地址:https://www.cnblogs.com/sdfzsyq/p/9676913.html
Copyright © 2011-2022 走看看