It is clear that the minimum distance traveled by the DFS sequence is the total distance.
If not modified, it is actually a virtual tree.
Now that the changes have been brought ... In fact, it is the DFS sequence that maintains the key points.
It's good to maintain a balance tree, which involves inserting, deleting, finding the predecessor, and finding the maximum minimum value.
It's a two-point journey and an LCA.
(So this problem becomes treap full board ...)
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 #definell Long Long6 using namespacestd;7 Const intmaxn=100233;8 structzs{intToo,pre,dis;} e[maxn<<1];intTOT,LAST[MAXN];9 intlc[maxn<<1],rc[maxn<<1],p[maxn<<1],rnd[maxn<<1],tt,v,rt,pre,aft,st,ed;Ten intDep[maxn],bel[maxn],fa[maxn],sz[maxn],dfn[maxn],tim; One ll Dis[maxn],ans; A inti,j,k,n,m,cnt; - BOOL is[MAXN]; - the intRA,FH;CharRx; -InlineintRead () { -Rx=getchar (), ra=0, fh=1; - while((rx<'0'|| Rx>'9') &&rx!='-') rx=GetChar (); + if(rx=='-') fh=-1, rx=GetChar (); - while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnra*fh; + } A atInlinevoidLturn (int&X) {intr=rc[x];rc[x]=lc[r],lc[r]=x,x=R;} -InlinevoidRturn (int&X) {intl=lc[x];lc[x]=rc[l],rc[l]=x,x=L;} - voidInsertint&x) { - if(!x) {X=++tt,p[x]=v,rnd[x]=rand () +tt;return;} - if(dfn[v]<Dfn[p[x]]) { - Insert (lc[x]); in if(rnd[lc[x]]<rnd[x]) Rturn (x); -}Else{ to Insert (rc[x]); + if(rnd[rc[x]]<rnd[x]) Lturn (x); - } the } * voidDelint&x) { $ if(p[x]==V) {Panax Notoginseng if(!lc[x]| |! RC[X]) x=lc[x]|Rc[x]; - Else if(rnd[lc[x]]<Rnd[rc[x]]) Rturn (x), Del (x); the ElseLturn (x), Del (x); +}Else A if(Dfn[v]<dfn[p[x]] del (lc[x]);Elsedel (rc[x]); the } + voidGetpre (intx) { - if(!x)return; $ if(Dfn[p[x]]<dfn[v]) Pre=P[x],getpre (rc[x]); $ ElseGetpre (lc[x]); - } - voidGetaft (intx) { the if(!x)return; - if(Dfn[p[x]]>dfn[v]) aft=P[x],getaft (lc[x]);Wuyi ElseGetaft (rc[x]); the } -Inlinevoidgetst () { Wu intx=RT; - while(Lc[x]) x=Lc[x]; Aboutst=P[x]; $ } -Inlinevoidgeted () { - intx=RT; - while(Rc[x]) x=Rc[x]; AEd=P[x]; + } the - $ voidDfsintx) { thesz[x]=1, dep[x]=dep[fa[x]]+1; the for(intI=LAST[X];I;I=E[I].PRE)if(e[i].too!=Fa[x]) theFa[e[i].too]=x,dis[e[i].too]=dis[x]+e[i].dis,dfs (E[i].too), sz[x]+=Sz[e[i].too]; the } - voidDFS2 (intXintchain) { in intmx=0, i;dfn[x]=++tim,bel[x]=chain; the for(I=last[x];i;i=e[i].pre)if(E[i].too!=fa[x]&&sz[e[i].too]>sz[mx]) mx=E[i].too; the if(!MX)return; About DFS2 (mx,chain); the for(I=last[x];i;i=e[i].pre)if(e[i].too!=fa[x]&&e[i].too!=mx) DFS2 (e[i].too,e[i].too); the } theInlineintGetlca (intAintb) { + while(bel[a]!=Bel[b]) { - if(dep[bel[a]]<dep[bel[b]) swap (A, b); theA=Fa[bel[a]];Bayi } the returnDEP[A]<DEP[B]?a:b; the } -inline ll Getdis (intAintb) { - intC=Getlca (A, b); the returndis[b]-dis[c]+dis[a]-Dis[c]; the } theInlinevoidInsertintAintBintc) { thee[++tot].too=b,e[tot].dis=c,e[tot].pre=last[a],last[a]=tot, -e[++tot].too=a,e[tot].dis=c,e[tot].pre=last[b],last[b]=tot; the } the intMain () { theN=read (), m=read ();94 for(i=1; i<n;i++) J=read (), k=read (), insert (J,k,read ()); theDfs1), DFS2 (1,1); the while(m--){ theI=read ();98 if(! is[i]) { Aboutv=i,pre=aft=0, Getpre (RT), Getaft (RT); - if(pre| |AFT) {101 if(! PRE) pre=ed;if(! AFT) aft=St;102Ans=ans-getdis (Pre,aft) +getdis (pre,i) +Getdis (i,aft);103 }104Insert (RT), is[i]=1, cnt++; the}Else{106v=i,pre=aft=0, Getpre (RT), Getaft (RT);107 if(pre| |AFT) {108 if(! PRE) pre=ed;if(! AFT) aft=St;109Ans=ans+getdis (Pre,aft)-getdis (pre,i)-Getdis (i,aft); the }111Del (RT), is[i]=0, cnt--; the }113 if(CNT) getst (), geted (); theprintf"%lld\n", ans); the } the return 0;117}
View Code
[bzoj3991] [SDOI2015] Treasure Hunt Game