1. 程式人生 > >洛谷 P3128 [USACO15DEC]最大流Max Flow

洛谷 P3128 [USACO15DEC]最大流Max Flow

eterm poi adg 帶來 orange pri 線段 next argv

題目描述

Farmer John has installed a new system of N-1N?1 pipes to transport milk between the NN stalls 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給他的牛棚的N(2≤N≤50,000)個隔間之間安裝了N-1根管道,隔間編號從1到N。所有隔間都被管道連通了。

FJ有K(1≤K≤100,000)條運輸牛奶的路線,第i條路線從隔間si運輸到隔間ti。一條運輸路線會給它的兩個端點處的隔間以及中間途徑的所有隔間帶來一個單位的運輸壓力,你需要計算壓力最大的隔間的壓力是多少。

輸入輸出格式

輸入格式:

The first line of the input contains NN and KK.

The next N-1N?1 lines each contain two integers xx and yy (x \ne yxy) 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.

輸出格式:

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

barn.

輸入輸出樣例

輸入樣例#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
輸出樣例#1:
9

樹鏈剖分 + 線段樹

題目鏈接

#include <algorithm>
#include <cstdio>
#define N 50005
using namespace std;

int n,k,cnt,tim,maxn,ans,fa[N],dep[N],siz[N],top[N],belong[N],to[N<<1],nxt[N<<1],head[N];
void dfs1(int x,int pr)
{
    int pos=tim++;
    fa[x]=pr;
    dep[x]=dep[pr]+1;
    for(int i=head[x];i;i=nxt[i])
    {
        int v=to[i];
        if(v==pr) continue;
        dfs1(v,x);
    }
    siz[x]=tim-pos;
}
void dfs2(int x,int chain)
{
    top[x]=chain;
    belong[x]=++tim;
    int p=0;
    for(int i=head[x];i;i=nxt[i])
    {
        int v=to[i];
        if(v!=fa[x]&&siz[p]<siz[v]) p=v;
    }
    if(p) dfs2(p,chain);
    for(int i=head[x];i;i=nxt[i])
    {
        int v=to[i];
        if(v!=fa[x]&&v!=p) dfs2(v,v);
    }
}
int mid[N<<2|1],val[N<<2|1],len[N<<2|1],flag[N<<2|1];
void build(int k,int l,int r)
{
    mid[k]=(l+r)>>1;
    len[k]=r-l+1;
    if(l==r) return;
    build(k<<1,l,mid[k]);
    build(k<<1|1,mid[k]+1,r);
}
void pushdown(int k)
{
    flag[k<<1]+=flag[k];
    flag[k<<1|1]+=flag[k];
    val[k<<1]+=flag[k];
    val[k<<1|1]+=flag[k];
    flag[k]=0;
}
void modify(int k,int l,int r,int x,int y,int v)
{
    if(l>=x&&r<=y)
    {
        flag[k]+=v;
        val[k]+=v;
        return;
    }
    if(flag[k]) pushdown(k);
    if(x<=mid[k]) modify(k<<1,l,mid[k],x,y,v);
    if(y>mid[k]) modify(k<<1|1,mid[k]+1,r,x,y,v);
    val[k]=max(val[k<<1],val[k<<1|1]);
}
void solve(int x,int y)
{
    for(;top[x]!=top[y];x=fa[top[x]])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        modify(1,1,n,belong[top[x]],belong[x],1);
    }
    if(dep[x]<dep[y]) swap(x,y);
    modify(1,1,n,belong[y],belong[x],1);
}
int main(int argc,char *argv[])
{
    scanf("%d%d",&n,&k);
    for(int u,v,i=1;i<n;++i)
    {
        scanf("%d%d",&u,&v);
        nxt[++cnt]=head[u];to[cnt]=v;head[u]=cnt;
        nxt[++cnt]=head[v];to[cnt]=u;head[v]=cnt;
    }
    dfs1(1,0);tim=0;dfs2(1,1);
    build(1,1,n);
    for(int u,v,i=1;i<=k;++i)
    {
        scanf("%d%d",&u,&v);
        solve(u,v);
    }
    printf("%d\n",val[1]);
    return 0;
}

洛谷 P3128 [USACO15DEC]最大流Max Flow