zoukankan      html  css  js  c++  java
  • BZOJ 4390: [Usaco2015 dec]Max Flow

    4390: [Usaco2015 dec]Max Flow

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

    [Submit][Status][Discuss]

    蒟蒻上来就想DFS序+线段树维护区间修改及查询最值,后来看到度娘上好多喊树链剖分的,又看到可以直接两边DFS+LCA切掉。

    对于x到y的路径上的所有点+1,等同于对x做+1,对y做+1,对LCA(x,y)做-1,对LCA(x,y)->father做-1,最后让每个点的权值等于子树权值和即可。

      1 #include <cstdio>
      2 #include <cstring>
      3 
      4 const int siz = 100500;
      5 
      6 int n, m;
      7 
      8 int tot;
      9 int hd[siz];
     10 int to[siz];
     11 int nt[siz];
     12 
     13 inline void add(int x, int y)
     14 {
     15     nt[tot] = hd[x]; to[tot] = y; hd[x] = tot++;
     16     nt[tot] = hd[y]; to[tot] = x; hd[y] = tot++;
     17 }
     18 
     19 int dp[siz];
     20 int fa[siz][25];
     21 
     22 void prework(int u, int f) 
     23 {
     24     for (int i = 1; i < 25; ++i)
     25         fa[u][i] = fa[fa[u][i - 1]][i - 1];
     26     
     27     for (int i = hd[u]; ~i; i = nt[i])
     28         if (to[i] != f) 
     29         {
     30             int v = to[i];
     31             dp[v] = dp[u] + 1;
     32             fa[v][0] = u;
     33             prework(v, u);
     34         }
     35 }
     36 
     37 inline int lca(int a, int b)
     38 {
     39     if (dp[a] < dp[b])
     40         a ^= b ^= a ^= b;
     41     
     42     for (int i = 24; i >= 0; --i)
     43         if (dp[fa[a][i]] >= dp[b])
     44             a = fa[a][i];
     45     
     46     if (a == b)return a;
     47     
     48     for (int i = 24; i >= 0; --i)
     49         if (fa[a][i] != fa[b][i])
     50             a = fa[a][i], b = fa[b][i];
     51     
     52     return fa[a][0];
     53 }
     54 
     55 int sm[siz];
     56 
     57 inline void solve(int x, int y)
     58 {
     59     int t = lca(x, y);
     60     
     61     ++sm[x];
     62     ++sm[y];
     63     --sm[t];
     64     --sm[fa[t][0]];
     65 }
     66 
     67 int ans;
     68 
     69 void calc(int u, int f) 
     70 {
     71     for (int i = hd[u]; ~i; i = nt[i])
     72         if (to[i] != f) 
     73         {
     74             int v = to[i];
     75             calc(v, u);
     76             sm[u] += sm[v];
     77         }
     78         
     79     if (ans < sm[u])
     80         ans = sm[u];
     81 }
     82 
     83 signed main(void) 
     84 {
     85     scanf("%d%d", &n, &m);
     86     
     87     memset(hd, -1, sizeof(hd));
     88     
     89     for (int i = 1, x, y; i < n; ++i)
     90         scanf("%d%d", &x, &y), add(x, y);
     91         
     92     dp[1] = 0;
     93     
     94     prework(1, 0);
     95     
     96     for (int i = 1, x, y; i <= m; ++i)
     97         scanf("%d%d", &x, &y), solve(x, y);
     98         
     99     calc(1, 0);
    100     
    101     printf("%d
    ", ans);
    102 }

    @Author: YouSiki

  • 相关阅读:
    redis qps监控
    不要对md5file.read()计算md5值
    Kubernetes-基于helm安装部署高可用的Redis及其形态探索(二)
    Kubernetes-基于helm安装部署高可用的Redis及其形态探索
    mongodb replication set 主从切换
    使用packstack安装pike版本的openstack
    redis性能测试方法
    mysql与mariadb性能测试方法
    Mongodb集群形式探究-一主一从一仲裁。
    Python元类
  • 原文地址:https://www.cnblogs.com/yousiki/p/6249125.html
Copyright © 2011-2022 走看看