[3128] Luo Gu maximum flow Max Flow

Title Description

Farmer John has installed a new system of N-1N1 pipes to transport milk between the NNstalls in his barn (2 \leq N \leq 50,0002N50,000), conveniently numbered 1 \ldots N1N. 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 \leq K \leq 100,0001K100,000). For the iith such pair, you are told two stalls s_isi and t_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 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 s_isi to t_iti, then it counts as being pumped through the endpoint stalls s_isi and

t_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 NN and KK.

The next N-1N1 lines each contain two integers xx and yy (x \ne yx=y) describing a pipe

between stalls xx and yy.

The next KK lines each contain two integers ss and tt 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 

problem solution: LCA title, the test tree quite a lot of difference test, yet again a problem.
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
const int N=100002;
int n,m,x,y;
struct node{
    int next;
    int to;
}e[N*2];
int cnt,head[N],yc=-1;
int f[N][22],d[N],ans[N];
void add(int x,int y){
    e[++cnt].to=y;
    e[cnt].next=head[x];
    head[x]=cnt;
}

void dfs_YCLL(int u,int fa){
    d[u]=d[fa]+1;
    for(int i=0;i<=19;i++)
        f[u][i+1]=f[f[u][i]][i];
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].to;
        if(v==fa) continue;
        f[v][0]=u; dfs_YCLL(v,u);
    }
}
int lca(int x,int y){
    if(d[x]<d[y]) swap(x,y);
    for(int i=20;i>=0;i--){
        if(d[f[x][i]]>=d[y]) x=f[x][i];
        if(x==y)  return x;
    }
    for(int i=20;i>=0;i--)
        if(f[x][i]!=f[y][i])
           { x=f[x][i]; y=f[y][i]; }
    return f[x][0];
}

void dfs(int x){
    for(int i=head[x];i;i=e[i].next){
        int v=e[i].to;
        if(v==f[x][0]) continue;
        dfs(v); ans[x]+=ans[v];
    }
    yc=max(yc,ans[x]);
}
int main(){
    freopen("3128.in","r",stdin);
    freopen("3128.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(int i=1;i<n;i++){
        scanf("%d %d",&x,&y);
        add(x,y); add(y,x);
    }
    dfs_YCLL(1,0);
    for(int i=1;i<=m;i++){
        scanf("%d %d",&x,&y);
        ans[x]++; ans[y]++;
        int z=lca(x,y);
        ans[z]--; ans[f[z][0]]--;
    }
    dfs(1);
    //for(int i=1;i<=n;i++)
    //    yc=max(yc,ans[i]);
    printf("%d\n",yc);
    return 0;
}

 



Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11842033.html