P3128 [USACO15DEC]Max Flow P(树链剖分)
题意:
FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N。所有隔间都被管道连通了。
FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。
题解:
#include<bits/stdc++.h> using namespace std; const int maxn=1e6+100; typedef long long ll; int n,m; vector<int> g[maxn]; int son[maxn]; int id[maxn]; int fa[maxn]; int cnt; int dep[maxn]; int size[maxn]; int top[maxn]; int w[maxn]; int wt[maxn]; struct node { int l,r; ll sum; ll lazy; }segTree[maxn*4]; void build (int i,int l,int r) { segTree[i].l=l; segTree[i].r=r; if (l==r) { segTree[i].sum=wt[l]; return; } int mid=(l+r)>>1; build(i<<1,l,mid); build(i<<1|1,mid+1,r); segTree[i].sum=max(segTree[i<<1].sum,segTree[i<<1|1].sum); } void spread (int i) { if (segTree[i].lazy) { segTree[i<<1].sum+=segTree[i].lazy; segTree[i<<1|1].sum+=segTree[i].lazy; segTree[i<<1].lazy+=segTree[i].lazy; segTree[i<<1|1].lazy+=segTree[i].lazy; segTree[i].lazy=0; } } void update (int i,int l,int r,int val) { if (l<=segTree[i].l&&segTree[i].r<=r) { segTree[i].sum+=val; segTree[i].lazy+=val; return; } spread(i); int mid=(segTree[i].l+segTree[i].r)>>1; if (l<=mid) update(i<<1,l,r,val); if (r>mid) update(i<<1|1,l,r,val); segTree[i].sum=max(segTree[i<<1].sum,segTree[i<<1|1].sum); } ll query (int i,int l,int r) { if (l<=segTree[i].l&&r>=segTree[i].r) { return segTree[i].sum; } spread(i); int mid=(segTree[i].l+segTree[i].r)>>1; ll ans=0; if (l<=mid) ans=max(ans,query(i<<1,l,r)); if (r>mid) ans=max(ans,query(i<<1|1,l,r)); return ans; } ll qRange (int x,int y) { ll ans=0; while (top[x]!=top[y]) { if (dep[top[x]]<dep[top[y]]) swap(x,y); ans=max(ans,query(1,id[top[x]],id[x])); x=fa[top[x]]; } if (dep[x]>dep[y]) swap(x,y); ans=max(ans,query(1,id[x],id[y])); return ans; } void upRange (int x,int y,int k) { while (top[x]!=top[y]) { if (dep[top[x]]<dep[top[y]]) swap(x,y); update(1,id[top[x]],id[x],k); x=fa[top[x]]; } if (dep[x]>dep[y]) swap(x,y); update(1,id[x],id[y],k); } ll qSon (int x) { return query(1,id[x],id[x]+size[x]-1); } void dfs1 (int x,int f,int deep) { dep[x]=deep; fa[x]=f; size[x]=1; int maxson=-1; for (int y:g[x]) { if (y==f) continue; dfs1(y,x,deep+1); size[x]+=size[y]; if (size[y]>maxson) son[x]=y,maxson=size[y]; } } void dfs2 (int x,int topf) { id[x]=++cnt; wt[cnt]=w[x]; top[x]=topf; if (!son[x]) return; dfs2(son[x],topf); for (int y:g[x]) { if (y==fa[x]||y==son[x]) continue; dfs2(y,y); } } int main () { scanf("%d%d",&n,&m); for (int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); g[x].push_back(y); g[y].push_back(x); } dfs1(1,0,1); dfs2(1,1); build(1,1,n); while (m--) { int x,y; scanf("%d%d",&x,&y); upRange(x,y,1); } printf("%lld\n",qSon(1)); }