• [Usaco2015 dec]Max Flow


    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 204  Solved: 129
    [Submit][Status][Discuss]

    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.

    Sample Input

    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

    Sample Output

    9

    Source

    Platinum鸣谢Claris提供译文

    思路

    树链剖分

    代码实现

     1 #include<cstdio>
     2 const int maxn=5e4+10;
     3 inline int min_(int x,int y){return x<y?x:y;}
     4 inline int max_(int x,int y){return x>y?x:y;}
     5 inline int swap_(int&x,int&y){x^=y,y^=x,x^=y;}
     6 int n,k;
     7 int a,b;
     8 int eh[maxn],hs,et[maxn<<1],en[maxn<<1];
     9 int pd[maxn],pf[maxn],pws[maxn],psz[maxn],pps,pp[maxn],pt[maxn];
    10 int ts[maxn<<2],tf[maxn<<2];
    11 void dfs1(int k,int f,int d){
    12     psz[k]=1,pd[k]=d,pf[k]=f;
    13     for(int i=eh[k];i;i=en[i])
    14     if(et[i]!=f){
    15         dfs1(et[i],k,d+1);
    16         psz[k]+=psz[et[i]];
    17         if(psz[et[i]]>psz[pws[k]]) pws[k]=et[i];
    18     }
    19 }
    20 void dfs2(int k,int t){
    21     pp[k]=++pps,pt[k]=t;
    22     if(pws[k]) dfs2(pws[k],t);
    23     for(int i=eh[k];i;i=en[i])
    24     if(et[i]!=pf[k]&&et[i]!=pws[k])
    25     dfs2(et[i],et[i]);
    26 }
    27 void down(int k){
    28     int ls=k<<1,rs=ls|1;
    29     ts[ls]+=tf[k],ts[rs]+=tf[k];
    30     tf[ls]+=tf[k],tf[rs]+=tf[k];
    31     tf[k]=0;
    32 }
    33 void change(int k,int l,int r,int al,int ar){
    34     if(l==al&&r==ar){ts[k]++,tf[k]++;return;}
    35     if(tf[k]) down(k);
    36     int mid=l+r>>1,ls=k<<1,rs=ls|1;
    37     if(al<=mid) change(ls,l,mid,al,min_(ar,mid));
    38     if(ar>mid) change(rs,mid+1,r,max_(al,mid+1),ar);
    39     ts[k]=max_(ts[ls],ts[rs]);
    40 }
    41 int main(){
    42     scanf("%d%d",&n,&k);
    43     for(int i=1;i<n;i++){
    44         scanf("%d%d",&a,&b);
    45         ++hs,et[hs]=b,en[hs]=eh[a],eh[a]=hs;
    46         ++hs,et[hs]=a,en[hs]=eh[b],eh[b]=hs;
    47     }
    48     dfs1(1,0,1);
    49     dfs2(1,1);
    50     while(k--){
    51         scanf("%d%d",&a,&b);
    52         while(pt[a]!=pt[b]){
    53             if(pd[pt[a]]<pd[pt[b]]) swap_(a,b);
    54             change(1,1,n,pp[pt[a]],pp[a]);
    55             a=pf[pt[a]];
    56         }
    57         if(pd[a]<pd[b]) swap_(a,b);
    58         change(1,1,n,pp[b],pp[a]);
    59     }
    60     printf("%d
    ",ts[1]);
    61     return 0;
    62 }
  • 相关阅读:
    Crystal Reports 参考帮助信息
    五个寓言故事令你受益匪浅【来自:Chinabyte】
    设计五原则
    清除SQL日志
    SQL SERVER 2008 函数大全 字符串函数
    Java的内存管理与内存泄露
    Drawable和Bitmap的区别
    Java内存分配原理精讲
    周鸿祎:用户体验,简而未减
    Java编程中“为了性能”需做的26件事
  • 原文地址:https://www.cnblogs.com/J-william/p/6972568.html
Copyright © 2020-2023  润新知