BZOJ 4390: [Usaco2015 dec]Max Flow

来源:互联网 发布:2017年网络热点话题 编辑:程序博客网 时间:2024/06/07 12:59

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

分析

树上差分(树的前缀和)
   近年的NOIp,似乎对于树上差分的题目考察越来越热(参见2015年提高组 运输计划,2016年提高组 天天爱跑步)。这些题目都要知道在树上从某个点到另一个点的所有路径。但是,暴力求解这种题目经常会TLE。这种题目需要使用树上差分。在讲树上差分之前,首先需要知道树的以下两个性质:

  (1)任意两个节点之间有且只有一条路径。

  (2)一个节点只有一个父亲节点

  这两个性质都很容易证明。那么我们知道,如果假设我们要考虑的是从u到v的路径,u与v的lca是a,那么很明显,如果路径中有一点u’已经被访问了,且u’≠a,那么u’的父亲也一定会被访问,这是根据以上性质可以推出的。所以,我们可以将路径拆分成两条链,u->a和a->v。那么树上差分有两种常见形式:(1)关于边的差分;(2)关于节点的差分。

  ①关于边的差分:

  将边拆成两条链之后,我们便可以像差分一样来找到路径了。因为关于边的差分,a是不在其中的,所以考虑链u->a,则就要使cf[u]++,cf[a]–。然后链a->v,也是cf[v]++,cf[a]–。所以合起来便是cf[u]++,cf[v]++,cf[a]-=2。然后,从根节点,对于每一个节点x,都有如下的步骤:

  (1)枚举x的所有子节点u

  (2)dfs所有子节点u

  (3)cf[x]+=cf[u]

  那么,为什么能够保证这样所有的边都能够遍历到呢?因为我们刚刚已经说了,如果路径中有一点u’已经被访问了,且u’≠a,那么u’的父亲也一定会被访问。所以u’被访问几次,它的父亲也就因为u’被访问了几次。所以就能够找出所有被访问的边与访问的次数了。路径求交等一系列问题就是通过这个来解决的。因为每个点都只会遍历一次,所以其时间复杂度为O(n).

  ②关于点的差分:

  还是与和边的差分一样,对于所要求的路径,拆分成两条链。步骤也和上面一样,但是也有一些不同,因为关于点,u与v的lca是需要包括进去的,所以要把lca包括在某一条链中,最后对cf数组的操作便是cf[u]++,cf[v]++,cf[a]–,cf[father[a]]–。其时间复杂度也是一样的O(n).

代码

#include <bits/stdc++.h>const int N = 50555;int read(){    int x = 0, f = 1;    char ch = getchar();    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}    return x * f;}struct Edge{    int to,next;}e[N * 2];int next[N];int cnt;void add(int x,int y){    e[++cnt].to = y, e[cnt].next = next[x], next[x] = cnt;}int size[N],dep[N];int fa[N];void dfs1(int x){    size[x] = 1;    dep[x] = dep[fa[x]] + 1;    for (int i = next[x]; i; i = e[i].next)    {        if (e[i].to == fa[x])            continue;        fa[e[i].to] = x;        dfs1(e[i].to);        size[x] += size[e[i].to];    }}int mx[N],mn[N];int sz;int top[N];void dfs2(int x,int chain){    mx[x] = mn[x] = ++sz;    top[x] = chain;    int k = 0;    for (int i = next[x]; i; i = e[i].next)    {        if (e[i].to != fa[x] && size[k] < size[e[i].to])            k = e[i].to;    }    if (!k)        return;    dfs2(k,chain);    for (int i = next[x]; i; i = e[i].next)    {        if (e[i].to == k || e[i].to == fa[x])            continue;        dfs2(e[i].to,e[i].to);    }    mx[x] = sz;}int getLca(int x,int y){    while (top[x] != top[y])    {        if (dep[top[x]] < dep[top[y]])            std::swap(x,y);        x = fa[top[x]];    }    if (dep[x] < dep[y])        return x;    else return y;}int v[N];int ans;void dfs(int x){    for (int i = next[x]; i; i = e[i].next)    {        if (e[i].to == fa[x])            continue;        dfs(e[i].to);        v[x] += v[e[i].to];    }    if (v[x] > ans)        ans = v[x];}int main(){    int n = read(), m = read();    for (int i = 1; i < n; i++)    {        int x = read(), y = read();        add(x,y); add(y,x);    }    dfs1(1);    dfs2(1,0);    for (int i = 1; i <= m; i++)    {        int x = read(), y = read(), p = getLca(x,y);        v[p]--, v[fa[p]]--;        v[x]++, v[y]++;    }    dfs(1);    printf("%d\n",ans);}
原创粉丝点击