zoukankan      html  css  js  c++  java
  • 【洛谷 3128】最大流Max Flow

    题目描述

    Farmer John has installed a new system of N-1N1 pipes to transport milk between the NNstalls 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 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

    题解:LCA题目里,考树上差分考的很多蛮,又来一题。
    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int N=100002;
    int n,m,x,y;
    struct node{
        int next;
        int to;
    }e[N*2];
    int cnt,head[N],yc=-1;
    int f[N][22],d[N],ans[N];
    void add(int x,int y){
        e[++cnt].to=y;
        e[cnt].next=head[x];
        head[x]=cnt;
    }
    
    void dfs_YCLL(int u,int fa){
        d[u]=d[fa]+1;
        for(int i=0;i<=19;i++)
            f[u][i+1]=f[f[u][i]][i];
        for(int i=head[u];i;i=e[i].next){
            int v=e[i].to;
            if(v==fa) continue;
            f[v][0]=u; dfs_YCLL(v,u);
        }
    }
    int lca(int x,int y){
        if(d[x]<d[y]) swap(x,y);
        for(int i=20;i>=0;i--){
            if(d[f[x][i]]>=d[y]) x=f[x][i];
            if(x==y)  return x;
        }
        for(int i=20;i>=0;i--)
            if(f[x][i]!=f[y][i])
               { x=f[x][i]; y=f[y][i]; }
        return f[x][0];
    }
    
    void dfs(int x){
        for(int i=head[x];i;i=e[i].next){
            int v=e[i].to;
            if(v==f[x][0]) continue;
            dfs(v); ans[x]+=ans[v];
        }
        yc=max(yc,ans[x]);
    }
    int main(){
        freopen("3128.in","r",stdin);
        freopen("3128.out","w",stdout);
        scanf("%d %d",&n,&m);
        for(int i=1;i<n;i++){
            scanf("%d %d",&x,&y);
            add(x,y); add(y,x);
        }
        dfs_YCLL(1,0);
        for(int i=1;i<=m;i++){
            scanf("%d %d",&x,&y);
            ans[x]++; ans[y]++;
            int z=lca(x,y);
            ans[z]--; ans[f[z][0]]--;
        }
        dfs(1);
        //for(int i=1;i<=n;i++)
        //    yc=max(yc,ans[i]);
        printf("%d
    ",yc);
        return 0;
    }


  • 相关阅读:
    整数的位数,及反转
    判断1~n有多少个1
    C语言常用字符串函数
    C语言类型转换原理
    printf()函数压栈a++与++a的输出
    49 丑数( 时间空间效率的平衡)
    42 连续子数组的最大和(时间效率)
    41 数据流中的中位数(时间效率)
    40 最小的K个数(时间效率)
    一、简介 ELO商户类别推荐有助于了解客户忠诚度
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11842033.html
Copyright © 2011-2022 走看看