题目描述

Farmer John has installed a new system of AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 pipes to transport milk between the AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 stalls in his barn (AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128), conveniently numbered AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128. Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

FJ is pumping milk between AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 pairs of stalls (AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128). For the AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128th such pair, you are told two stalls AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 and AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128, 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 AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 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 AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 to AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128, then it counts as being pumped through the endpoint stalls AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 and

AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128, 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 AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 and AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128.

The next AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 lines each contain two integers AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 and AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 (AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128) describing a pipe

between stalls AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 and AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128.

The next AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 lines each contain two integers AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 and AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128 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



思路:
  裸树剖;
  (感觉正确的代码样例没过,错误的代码ac。。。)


来,上代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

#define maxn 50005

using namespace std;

struct TreeNodeType {
    int l,r,dis,mid,flag;
};
struct TreeNodeType tree[maxn<<2];

struct EdgeType {
    int to,next;
};
struct EdgeType edge[maxn<<1];

int if_z,n,m,cnt,head[maxn],deep[maxn],id[maxn];
int size[maxn],top[maxn],f[maxn];

char Cget;

inline void in(int &now)
{
    now=0,if_z=1,Cget=getchar();
    while(Cget>'9'||Cget<'0')
    {
        if(Cget=='-') if_z=-1;
        Cget=getchar();
    }
    while(Cget>='0'&&Cget<='9')
    {
        now=now*10+Cget-'0';
        Cget=getchar();
    }
    now*=if_z;
}

inline void edge_add(int u,int v)
{
    cnt++;
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt;
}

void search_1(int now,int fa)
{
    int pos=cnt++;
    f[now]=fa,deep[now]=deep[fa]+1;
    for(int i=head[now];i;i=edge[i].next)
    {
        if(edge[i].to==fa) continue;
        search_1(edge[i].to,now);
    }
    size[now]=cnt-pos;
}

void search_2(int now,int chain)
{
    id[now]=++cnt,top[now]=chain;
    int pos=0;
    for(int i=head[now];i;i=edge[i].next)
    {
        if(edge[i].to==f[now]) continue;
        if(size[edge[i].to]>size[pos]) pos=edge[i].to;
    }
    if(pos==0) return ;
    search_2(pos,chain);
    for(int i=head[now];i;i=edge[i].next)
    {
        if(edge[i].to==f[now]||edge[i].to==pos) continue;
        search_2(edge[i].to,edge[i].to);
    }
}

void tree_build(int now,int l,int r)
{
    tree[now].l=l,tree[now].r=r;
    if(l==r) return ;
    tree[now].mid=(l+r)>>1;
    tree_build(now<<1,l,tree[now].mid);
    tree_build(now<<1|1,tree[now].mid+1,r);
}

void tree_change(int now,int l,int r)
{
    if(tree[now].l==l&&tree[now].r==r)
    {
        tree[now].dis++;
        tree[now].flag++;
        return ;
    }
    if(tree[now].flag)
    {
        tree[now<<1].dis+=tree[now].flag,tree[now<<1|1].dis+=tree[now].flag;
        tree[now<<1].flag+=tree[now].flag,tree[now<<1|1].flag+=tree[now].flag;
        tree[now].flag=0;
    }
    if(l>tree[now].mid) tree_change(now<<1|1,l,r);
    else if(r<=tree[now].mid) tree_change(now<<1,l,r);
    else
    {
        tree_change(now<<1,l,tree[now].mid);
        tree_change(now<<1|1,tree[now].mid+1,r);
    }
    tree[now].dis=max(tree[now<<1].dis,tree[now<<1|1].dis);
}

int main()
{
    in(n),in(m);int u,v;
    for(int i=1;i<n;i++)
    {
        in(u),in(v);
        edge_add(u,v);
        edge_add(v,u);
    }
    cnt=0,search_1(1,0);
    cnt=0,search_2(1,1);
    tree_build(1,1,n);
    while(m--)
    {
        in(u),in(v);
        while(top[u]!=top[v])
        {
            if(deep[top[u]]<deep[top[v]]) swap(u,v);
            tree_change(1,id[top[u]],id[u]);
            u=f[top[u]];
        }
        //if(u==v) continue;
        if(deep[u]>deep[v]) swap(u,v);
        tree_change(1,id[u],id[v]);
    }
    cout<<tree[1].dis;
    return 0;
}

 

相关文章:

  • 2021-07-09
  • 2022-12-23
  • 2021-08-11
  • 2021-08-17
  • 2021-11-10
  • 2021-07-16
  • 2021-10-28
  • 2021-09-17
猜你喜欢
  • 2021-12-26
  • 2021-10-17
  • 2022-01-04
  • 2022-02-06
  • 2022-01-01
  • 2021-08-31
相关资源
相似解决方案