zoukankan      html  css  js  c++  java
  • [Usaco2015 dec]Max Flow 树上差分

    [Usaco2015 dec]Max Flow

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 353  Solved: 236
    [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

    HINT

     

    Source

     
    数上差分,对于x,y 在x处+1,y处+1,lca(x,y)-1 fa[lca(x,y)]-1,就可以了。
     1 #include<cstring>
     2 #include<cmath>
     3 #include<iostream>
     4 #include<cstdio>
     5 #include<algorithm>
     6 
     7 #define N 50007
     8 using namespace std;
     9 inline int read()
    10 {
    11     int x=0,f=1;char ch=getchar();
    12     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    13     while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    14     return x*f;
    15 }
    16 
    17 int n,m,ans;
    18 int val[N],dep[N],fa[N][20];
    19 int cnt,hed[N],nxt[N<<1],rea[N<<1];
    20 
    21 void add(int u,int v)
    22 {
    23     nxt[++cnt]=hed[u];
    24     hed[u]=cnt;
    25     rea[cnt]=v;
    26 }
    27 void add_two_way(int x,int y)
    28 {
    29     add(x,y);
    30     add(y,x);
    31 }
    32 void dfs_init(int u,int par)
    33 {
    34     for (int i=1;(1<<i)<=dep[u];i++)
    35         fa[u][i]=fa[fa[u][i-1]][i-1];
    36     for (int i=hed[u];i!=-1;i=nxt[i])
    37     {
    38         int v=rea[i];
    39         if (v==par) continue;
    40         fa[v][0]=u;dep[v]=dep[u]+1;
    41         dfs_init(v,u);
    42     }
    43 }
    44 int lca(int a,int b)
    45 {
    46     if (dep[a]<dep[b]) swap(a,b);
    47     int i;for (i=0;(1<<i)<=dep[a];i++);i--;
    48     for (int j=i;j>=0;j--)
    49         if (dep[a]-(1<<j)>=dep[b]) a=fa[a][j];
    50     if (a==b) return a;
    51     for (int j=i;j>=0;j--)
    52         if (fa[a][j]!=fa[b][j]) a=fa[a][j],b=fa[b][j];
    53     return fa[a][0];
    54 }
    55 void solve(int u)
    56 {
    57     for (int i=hed[u];i!=-1;i=nxt[i])
    58     {
    59         int v=rea[i];
    60         if (v==fa[u][0]) continue;
    61         solve(v),val[u]+=val[v];
    62     }
    63     ans=max(val[u],ans);
    64 }
    65 int main()
    66 {
    67     memset(hed,-1,sizeof(hed));
    68     n=read(),m=read();
    69     for (int i=1;i<n;i++)
    70         add_two_way(read(),read());
    71     dfs_init(1,0);
    72     for (int i=1;i<=m;i++)
    73     {
    74         int x=read(),y=read();
    75         val[x]++,val[y]++,val[lca(x,y)]--,val[fa[lca(x,y)][0]]--;
    76     }
    77     
    78     solve(1);
    79     printf("%d
    ",ans);
    80 }
  • 相关阅读:
    根据指定月份,打印该月份所属的季节
    求出1~100之间,既是3又是7的倍数的自然数出现的次数
    打印所有的水仙花数
    升景坊单间短期出租
    找出1000以内的所有完数
    ssh config host
    shell获取ip
    mongodb sharding 简单部署记录
    tcp转发
    openssl和Java的keytool证书相关的命令总结
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8682456.html
Copyright © 2011-2022 走看看