P3128 [USACO15DEC] Max Flow P (tree chain division + line segment tree maintenance max)

https://www.luogu.com.cn/problem/P3128


 

FJ installed N-1 pipes between the N (2≤N≤50,000) compartments of his cowshed. The compartment numbers are from 1 to N. All compartments are connected by pipes.

FJ has K (1≤K≤100,000) routes to transport milk, and the i-th route is transported from compartment si to compartment ti. A transportation route will bring a unit of transportation pressure to the compartments at its two endpoints and all the compartments in the middle. You need to calculate the pressure of the most stressed compartment.


Idea: After the tree chain is split, the operation and maintenance interval of the line segment tree is the best value. Each time the +1 operation is performed, the tagged subtree maxval+1, the parent maxval is transferred from the maximum maxval of the two subtrees. Finally, directly check tree[1].maxval.

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=5e4+100;
typedef long long LL;
struct Tree{
    LL l,r,maxval,tag;
}tree[maxn*4];
LL a[maxn];
///线段树部分
void push_up(LL p){
    tree[p].maxval=max(tree[p*2].maxval,tree[p*2+1].maxval);
}
void addtag(LL p,LL d){
    tree[p].tag+=d;
    tree[p].maxval+=d;
}
void push_down(LL p){
    if(tree[p].tag!=0){
        addtag(p*2,tree[p].tag);
        addtag(p*2+1,tree[p].tag);
        tree[p].tag=0;
    }
}
void build(LL p,LL l,LL r){
    tree[p].l=l;tree[p].r=r;tree[p].tag=0;tree[p].maxval=0;
    if(l==r){return;}
    LL mid=(l+r)>>1;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    push_up(p);
}
void modify(LL p,LL l,LL r,LL d){
    if(l<=tree[p].l&&r>=tree[p].r){
        addtag(p,d);
        return;
    }
    push_down(p);
    LL mid=(tree[p].l+tree[p].r)>>1;
    if(l<=mid) modify(p*2,l,r,d);
    if(r>mid) modify(p*2+1,l,r,d);
    push_up(p);
}
LL query(LL p,LL l,LL r){
    return tree[1].maxval;
}
///树剖部分
LL son[maxn],siz[maxn],dep[maxn],fa[maxn];
LL top[maxn];
LL id[maxn],tot=0;
LL new_w[maxn];
vector<LL>g[maxn];
void predfs(LL u,LL father){
    siz[u]=1;dep[u]=dep[father]+1;
    fa[u]=father;
    for(LL i=0;i<g[u].size();i++){
        LL v=g[u][i];
        if(v==father) continue;
        predfs(v,u);
        siz[u]+=siz[v];
        if(siz[v]>siz[son[u]]){
            son[u]=v;
        }
    }
}
void dfs(LL u,LL topx){
    id[u]=++tot;
    top[u]=topx;
    new_w[tot]=a[u];
    if(!son[u]) return;///叶子节点访问完了
    dfs(son[u],topx);///先处理重儿子
    for(LL i=0;i<g[u].size();i++){
        LL v=g[u][i];
        if(v==fa[u]||v==son[u]) continue;
        dfs(v,v);
    }
}
void modify_path(LL u,LL v,LL d)
{
    while(top[u]!=top[v]){
        if(dep[top[u]]<dep[top[v]]) swap(u,v);
        modify(1,id[top[u]],id[u],1);
        u=fa[top[u]];
    }
    if(dep[u]>dep[v]) swap(u,v);
    modify(1,id[u],id[v],1);
}
LL query_path(LL u,LL v){
    return tree[1].maxval;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<n;i++){
    LL u,v;cin>>u>>v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  predfs(1,0);
  dfs(1,0);
  build(1,1,n);
  while(m--){
    LL u,v;cin>>u>>v;
    modify_path(u,v,1);
  }
  cout<<query_path(1,n)<<endl;
return 0;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/109864107