码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷 P3128 [USACO15DEC]最大流Max Flow

时间:2017-11-19 11:11:10      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:eterm   base   pos   ber   desc   blank   thml   线段树   name   

题目描述

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

标签:eterm   base   pos   ber   desc   blank   thml   线段树   name   

原文地址:http://www.cnblogs.com/sy1in/p/7859081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!