bzoj4390[usaco2015 Dec]max Flow
Test instructions
Given a tree with n points, all nodes have a weighted value of 0. There are k operations, each time specifying two points s,t, the s to the T path all points on the weight of a plus. Please output the weight of the point with the highest weight after the K operation is completed. n≤50000,k≤100000.
Exercises
First, the chain of the tree into a chain. Then use the array interval addition (that is, in the array interval left endpoint position increment value, the array interval right end point +1 position increases the inverse number of this value, and finally sweep over a[i]+=a[i-1]) accumulated weights. Similar bzoj3631
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 #defineMAXN 500106 using namespacestd;7 8InlineintRead () {9 CharCh=getchar ();intf=1, x=0;Ten while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} One while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); A returnf*x; - } - intSm[maxn],sz[maxn],dep[maxn],fa[maxn],top[maxn],pos[maxn],n,q,ans,tot; the structe{intT,n;} es[maxn*2];intg[maxn],ess; - voidPeintFintT) {es[++ess]= (e) {t,g[f]}; g[f]=ess; es[++ess]= (e) {f,g[t]}; g[t]=ess;} - voidDFS1 (intXintf) { -sz[x]=1; + for(intI=G[X];I;I=ES[I].N)if(es[i].t!=f) { -Fa[es[i].t]=x; dep[es[i].t]=dep[x]+1; DFS1 (ES[I].T,X); sz[x]+=sz[es[i].t]; + } A } at voidDFS2 (intXintFintTP) { -Pos[x]=++tot; TOP[X]=TP;intmx1=0, mx2=0; - for(intI=G[X];I;I=ES[I].N)if(ES[I].T!=F&&SZ[ES[I].T]>MX1) mx1=sz[es[i].t],mx2=es[i].t; - if(!MX2)return; DFS2 (MX2,X,TP); - for(intI=G[X];I;I=ES[I].N)if(es[i].t!=f&&es[i].t!=mx2) DFS2 (es[i].t,x,es[i].t); - } in voidSolveintXinty) { - for(; top[x]!=top[y];sm[pos[top[x]]]++,sm[pos[x]+1]--, X=fa[top[x]])if(dep[top[x]]<Dep[top[y]]) swap (x, y); to if(Dep[x]>dep[y]) swap (x, y); sm[pos[x]]++; sm[pos[y]+1]--; + } - intMain () { theN=read (); Q=read (); Inc (I,1, N-1){intX=read (), Y=read (); PE (x, y);} DFS1 (1,0); DFS2 (1,0,1); *Inc (I,1, q) {intX=read (), y=read (); solve (x, y);} $Inc (I,1, N) sm[i]+=sm[i-1],ans=max (Ans,sm[i]); printf"%d", ans);return 0;Panax Notoginseng}
20160908
bzoj4390[usaco2015 Dec]max flow*