P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)

P3128 [USACO15DEC]最大流Max Flow

 

题目描述

Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

FJ is pumping milk between  pairs of stalls (). For the th such pair, you are told two stalls  and , 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  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  to , then it counts as being pumped through the endpoint stalls  and

, 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  and .

The next  lines each contain two integers  and  () describing a pipe

between stalls  and .

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

/*
树上差分:对于树上x,y之间的路径区间修改时,设数组为c
则c[x]+1,c[y]+1,c[lca(x,y)]-1,c[father[lca(x,y)]-1.
最后dfs一下,使每个节点c[x]+=每个子节点的c值就ok了..
那就来说明一下树上差分这个据说完爆树剖的东西:
对于两个点,把他们的路径上所有点包括他们权值加一。开始路径上权值都为零,
先把起点终点权值加一,然后把它们分别往LCA权值上传,易知,LCA被加了2遍,所以减一。
因为标记上传时不可避免的把LCA的+1标记也上传了,所以就要把她减一成为零,然后上传。 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100001
#define S 21

using namespace std;
int deep[maxn],head[maxn],p1,p2,n,m,num,ans,s,x,y,fa[maxn][S+5];
int w[maxn],w2[maxn];
struct node {
    int from;
    int to;
    int next;
}e[maxn*2];

void add(int from,int to)
{
    e[num].from=from;
    e[num].to=to;
    e[num].next=head[from];
    head[from]=num;
    num++;
}

int init()
{
    int x=0,f=1;char c=getchar();
    while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

void swap(int &a,int &b)
{
    int t=a;a=b;b=t;
}

void get_fa()
{
    for(int j=1;j<=S;j++)
      for(int i=1;i<=n;i++)
        fa[i][j]=fa[fa[i][j-1]][j-1];
}

void Dfs(int now,int from,int c)
{
    fa[now][0]=from;
    deep[now]=c;
    for(int i=head[now];~i;i=e[i].next)
     {
         int& v=e[i].to;
         if(v!=from)
           Dfs(v,now,c+1);
     }
}

int get_same(int a,int t)
{
    for(int i=0;i<S;i++)
     if(t&(1<<i)) a=fa[a][i];
    return a;
}

int LCA(int a,int b)
{
    if(deep[a]<deep[b]) swap(a,b);
    a=get_same(a,deep[a]-deep[b]);
    if(a==b) return a;
    for(int i=S;i>=0;i--)
                       
    {
        if(fa[a][i]!=fa[b][i])
        {
            a=fa[a][i];
            b=fa[b][i];
        }
    }
        return fa[a][0];
}

void work(int u,int v)//树上差分
{
    int s=LCA(u,v);
    w[u]++;
    w[v]++;
    w[s]--;
    if(fa[s][0]!=-1) w[fa[s][0]]--;
}

int dfs2(int now,int from)
{
    w2[now]=w[now];
    for(int i=head[now];~i;i=e[i].next)
    {
        int& v=e[i].to;
        if(v!=from)
          dfs2(v,now),
        w2[now]+=w2[v];//上传标记 
    }
    ans=max(ans,w2[now]);
    return ans;
}

int main()
{
    memset(head,-1,sizeof head);
    n=init();m=init();
    int x,y;
    for(int i=1;i<n;i++)
    { 
        x=init();y=init();
        add(x,y);
        add(y,x); 
    } 
    Dfs(1,-1,0);
    get_fa();
    for(int i=1;i<=m;i++)
    {
        x=init();y=init();
        work(x,y);
    }
    ans=dfs2(1,-1);
    printf("%d\n",ans);
    return 0;
}

 

 

posted @ 2017-02-26 19:08  安月冷  阅读(637)  评论(2编辑  收藏  举报