• 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]


    题目描述

    Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

    FJ is pumping milk between  pairs of stalls (). For the th such pair, you are told two stalls  and , 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 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  to , then it counts as being pumped through the endpoint stalls  and

    , 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  and .

    The next  lines each contain two integers  and  () describing a pipe

    between stalls  and .

    The next  lines each contain two integers  and  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+树上差分。

    树结点的权值等于其子树所有节点的差分结果。

    704ms,不知道用树剖求LCA会有多快

     1 /*by SilverN*/
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 using namespace std;
     8 const int mxn=50010;
     9 int read(){
    10     int x=0,f=1;char ch=getchar();
    11     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    12     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    13     return x*f;
    14 }
    15 struct edge{
    16     int v,nxt;
    17 }e[mxn<<1];
    18 int hd[mxn],mct=0;
    19 void add_edge(int u,int v){
    20     e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
    21 }
    22 int n,k;
    23 int dep[mxn];
    24 int fa[mxn][17];
    25 int a[mxn];//差分 
    26 void DFS(int u,int f){
    27     dep[u]=dep[f]+1;
    28     for(int i=1;i<17;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
    29     for(int i=hd[u];i;i=e[i].nxt){
    30         int v=e[i].v;
    31         if(v==f)continue;
    32         fa[v][0]=u;
    33         DFS(v,u);
    34     }
    35     return;
    36 }
    37 int LCA(int x,int y){
    38     if(dep[x]<dep[y])swap(x,y);
    39     for(int i=16;i>=0;i--){
    40         if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
    41     }
    42     if(x==y)return x;
    43     for(int i=16;i>=0;i--){
    44         if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
    45     }
    46     return fa[x][0];
    47 }
    48 int ans=-1e9;
    49 int clc(int u,int f){
    50     int res=a[u];
    51     for(int i=hd[u];i;i=e[i].nxt){
    52         int v=e[i].v;
    53         if(v==f)continue;
    54         res+=clc(v,u);
    55     }
    56     ans=max(ans,res);
    57     return res;
    58 }
    59 int main(){
    60     n=read();k=read();
    61     int i,j,x,y;
    62     for(i=1;i<n;i++){
    63         x=read();y=read();
    64         add_edge(x,y);
    65         add_edge(y,x);
    66     }
    67     DFS(1,0);
    68     for(i=1;i<=k;i++){
    69         x=read();y=read();
    70         a[x]++;a[y]++;
    71         int tmp=LCA(x,y);
    72         a[tmp]--;
    73         if(fa[tmp][0])a[fa[tmp][0]]--;
    74     }
    75     clc(1,0);
    76     printf("%d
    ",ans);
    77     return 0;
    78 }
  • 相关阅读:
    C# DateTimePicker控件详解
    python2.7虚拟环境virtualenv安装及使用
    Python2.7 安装numpy报错解决方法
    关于C语言中递归的一点点小问题
    Drozer--AndroidApp安全评估工具
    Android--native层so文件调试
    New Blog
    小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
    2017年度回忆与总结 – 心态
    基于文本图形(ncurses)的文本搜索工具 ncgrep
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6102558.html
Copyright © 2020-2023  润新知