Tree chain split Template
#include <bits/stdc++.h>#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd;Const intmaxn=1000100;Const intInf= (1<< in);intN;Charop[ -];intU,v,w;vector<int>G[MAXN];intDEP[MAXN],SON[MAXN],FA[MAXN],SIZ[MAXN];intTOP[MAXN];intID[MAXN];intnum;intVAL[MAXN];structtree{intU,v,val;}; Tree E[MAXN];structsegtree{intVal;}; Segtree T[MAXN<<2];voidDFS1 (intUintFintd) {Fa[u]=F; Dep[u]=D; Son[u]=0; Siz[u]=1; for(intI=0; I<g[u].size (); i++){ intv=G[u][i]; if(v==f)Continue; DFS1 (V,u,d+1); if(Siz[v]>siz[son[u]]) son[u]=v; Siz[u]+=Siz[v]; }}voidDFS2 (intUintTP) {Top[u]=TP; Id[u]=++num; if(Son[u]) DFS2 (SON[U],TP); for(intI=0; I<g[u].size (); i++){ intv=G[u][i]; if(v==fa[u]| | V==son[u])Continue; DFS2 (V,V); }}voidPUSH_UP (intRT) {T[rt].val=max (t[rt<<1].val,t[rt<<1|1].val);}voidBuildintLintRintRT) { if(l==R) {T[rt].val=Val[l]; return; } intM= (l+r) >>1; Build (Lson); Build (Rson); PUSH_UP (RT);}voidUpdateintPintCintLintRintRT) { if(l==R) {T[rt].val=C; return; } intM= (l+r) >>1; if(p<=m) update (P,c,lson); ElseUpdate (P,c,rson); PUSH_UP (RT);}intQueryintLintRintLintRintRT) { if(L<=L&&R<=R)returnT[rt].val; intM= (l+r) >>1; intres=-INF; if(l<=m) res=Max (Res,query (L,r,lson)); if(r>m) res=Max (Res,query (L,r,rson)); returnRes;}intChaintUintv) { intres=-INF; while(top[u]!=Top[v]) { if(dep[top[u]]<Dep[top[v]) swap (U,V); Res=max (Res,query (Id[top[u]],id[u),1Num1)); U=Fa[top[u]]; } if(u!=v) { if(dep[u]>Dep[v]) swap (U,V); Res=max (Res,query (ID[SON[U]],ID[V),1Num1)); } returnRes;}intMain () {//freopen ("In.txt", "R", stdin); intCasen; CIN>>Casen; while(casen--) {cin>>O; for(intI=1; i<=n;i++) g[i].clear (); for(intI=1; i<n;i++) {scanf ("%d%d%d",&u,&v,&W); E[i]={u,v,w}; G[u].push_back (v); G[v].push_back (U); } num=0; DFS1 (1,0,1); DFS2 (1,1); for(intI=1; i<n;i++){ if(dep[e[i].u]>DEP[E[I].V]) swap (E[I].U,E[I].V); VAL[ID[E[I].V]]=E[i].val; } Build (1Num1); while(~SCANF ("%s", op)) { if(op[0]=='D') Break; if(op[0]=='C') {scanf ("%d%d",&u,&W); intp=ID[E[U].V]; Update (P,W,1Num1); } Else{scanf ("%d%d",&u,&v); printf ("%d\n", Cha (u,v)); } } } return 0;}View Code
SPOJ375, here is the edge of the right pressure into a point right, make a look ... Handwriting is not difficult anyway ... Copy also just copy two DFS save time.
Tree chain split Template