[USACO15DEC] Maximum Flow Max Flow (differential trees)

Subject description:

Farmer John has installed a new system of N−1N-1N−1 pipes to transport milk between the NNN stalls in his barn (2≤N≤50,0002 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1…N1 \ldots N1…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 KKK pairs of stalls (1≤K≤100,0001 \leq K \leq 100,0001≤K≤100,000). For the iiith such pair, you are told two stalls sis_isi and tit_iti, 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 KKK 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 sis_isi to tit_iti, then it counts as being pumped through the endpoint stalls sis_isi and

tit_iti, as well as through every stall along the path between them.

FJ to his barn N installed between N-1 of conduits (2≤N≤50,000) compartments, the compartments are numbered from 1 to N. All the compartments are in communication pipe.

There FJ K (1≤K≤100,000) milk transport route, a route leading from the i-th compartment to compartment si ti. Pressure a transport route will give its compartment and all the compartments of the middle route of two endpoints to bring pressure on the transportation of a unit, you need to calculate the maximum pressure compartment is.

Input Format

The first line of the input contains NNN and KKK.

The next N−1N-1N−1 lines each contain two integers xxx and yyy (x≠yx \ne yx≠y) describing a pipe

between stalls xxx and yyy.

The next KKK lines each contain two integers sss and ttt describing the endpoint

stalls of a path through which milk is being pumped.

Output Format

An integer specifying the maximum amount of milk pumped through any stall in the

barn.

Sample input and output

Input # 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

Output # 1

9

Ideas:

Here's a sample drawn at the map can be more understand the meaning of the title. It is entitled to a view of k asked to give a time from one point to another along the original point of the edge, the k-th query, asking the highest number of points through which a.

Thus speaking, think of the tree points difference, it is assumed from the point s to the point t, the difference array by the \ (dif [s] ++, dif [t] ++, dif [lca (s, t )] -, DIF [F [LCA (S, T)] [0]] - \) , the number of points passed in each of the statistics, the maximum value can be obtained during backtracking.

Realization on the front with a chain to the star, was seeking lca method can be found on a blog.

Code:

#include <iostream>
#include <cstdio>
using namespace std;
#define max_n 50005
//前向星
int head[max_n];
struct edge
{
    int v;
    int next;
}e[max_n<<1];
int cnt = 0;
void add(int u,int v)
{
    ++cnt;
    e[cnt].v = v;
    e[cnt].next = head[u];
    head[u] = cnt;
}
//读入优化
inline void read(int& x)
{
    x = 0;int f=0;char ch = getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')f=1;ch=getchar();}
    while('0'<=ch&&ch<='9') {x = 10*x+ch-'0';ch=getchar();}
    x = f?-x:x;
}
//题目数据
int n,k;
int ans = 0;
int f[max_n][23];
int depth[max_n];
int dif[max_n];//差分数组
//求lca
void dfs(int u,int from)
{
    depth[u] = depth[from]+1;
    for(int i = 1;(1<<i)<=depth[u];i++)
    {
        f[u][i] = f[f[u][i-1]][i-1];
    }
    for(int i = head[u];i;i=e[i].next)
    {
        int v = e[i].v;
        if(from==v) continue;
        f[v][0] = u;
        dfs(v,u);
    }
}
int lca(int s,int t)
{
    if(depth[s]<depth[t]) swap(s,t);
    for(int i = 20;i>=0;i--)
    {
        if(depth[f[s][i]]>=depth[t])
        {
            s =f[s][i];
        }
        if(s==t)
        {
            return s;
        }
    }
    for(int i = 20;i>=0;i--)
    {
        if(f[s][i]!=f[t][i])
        {
            s = f[s][i];
            t = f[t][i];
        }
    }
    return f[s][0];
}
//统计节点最大经过次数
void maxsum(int u,int from)
{
    for(int i = head[u];i;i=e[i].next)
    {
        int v = e[i].v;
        if(v==from) continue;
        maxsum(v,u);
        dif[u] += dif[v];
    }
    ans = max(ans,dif[u]);
}

int main()
{
    read(n);read(k);
    //cout << "n " << n << " k " << k << endl;
    for(int i = 1;i<n;i++)
    {
        int u,v;
        read(u);
        read(v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    for(int i = 0;i<k;i++)
    {
        int u,v;
        read(u);
        read(v);
        int LCA = lca(u,v);
        dif[u]++;
        dif[v]++;
        dif[LCA]--;
        dif[f[LCA][0]]--;
    }
    maxsum(1,0);
    cout << ans << endl;
    return 0;
}

Reference article:

顾z,差分数组 and 树上差分,https://rpdreamer.blog.luogu.org/ci-fen-and-shu-shang-ci-fen (洛谷出品!必属精品!,讲的虽然基础,但hin清晰)

思结,树上差分的两种思路,https://www.luogu.org/blog/sincereactor/shu-shang-ci-fen-di-liang-zhong-sai-lu (同为洛谷博客,可对照参考)

Guess you like

Origin www.cnblogs.com/zhanhonhao/p/11299945.html