首页 > 其他 > 详细

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

时间:2017-02-26 21:05:09      阅读:261      评论:0      收藏:0      [点我收藏+]

P3128 [USACO15DEC]最大流Max Flow

 

 

路过的大神,help!!!!!!!!!!!!

~i  ....为什么要这么写????位运算看不懂啊!

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

 

 

 

题目描述

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;
}

 

 

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

原文:http://www.cnblogs.com/L-Memory/p/6445462.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!