Or the problem of virtual tree ...
If there is only one inquiry, we think so, can be set dp[x] to only delete the X subtree within and X to the father's edge, so that x this subtrees tree in the energy islands are not connected with the father of X the minimum cost.
So if x itself is an energy island, then dp[x]=fe[x], otherwise dp[x]=min (Fe[x],sum{dp[son[x]}) is similar to this. (Fe means Father side, Fe[1]=inf)
So there are a number of groups asked us to do this on the virtual tree, it is important to note that a parent-child side of the virtual tree corresponds to a parent-child chain on the original tree, and the weight is the minimum value of the parent-child chain. Can be maintained together with multiplication.
It is important to note that ... I'm going to use a long long.
#include <iostream>#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<string.h>#include<math.h>#include<Set>#include<map>using namespaceStd;typedefLong Longll;intinf=1000000000; ll Inf_ll=10000000000000000LL;#defineGC GetChar ()intg_i () {inttmp=0;BOOLfu=0;Chars; while(s=gc,s!='-'&& (s<'0'|| S>'9')) ; if(s=='-') fu=1;Elsetmp=s-'0'; while(s=gc,s>='0'&&s<='9') tmp=tmp*Ten+s-'0'; if(FU)return-tmp;Else returntmp;}#defineGI G_i ()#definePob#definePC (x) Putchar (x)namespaceIB {Charb[ -];} InlinevoidPLL (ll x) {if(x==0) {PC ( -);return;} if(x<0) {PC ('-'); x=-x;} Char*s=ib::b; while(x) * (++s) =x%Ten, x/=Ten; while(S!=IB::B) PC ((* (s--)) + -);}#defineSZ 1234567#defineD 20#define_els; else//Real Treenamespacert{intn,fst[sz],nxt[sz],vb[sz],vc[sz],fa[sz],dep[sz],m=0, dfsn[sz],c=0, Sz[sz],fe[sz];intUp[sz][d],mine[sz][d];voidAd_de (intAintBintc) {++m; nxt[m]=fst[a]; fst[a]=m; vb[m]=b; vc[m]=C;}voidAdde (intAintBintc) {ad_de (a,b,c); Ad_de (b,a,c);}voidDfsintp) {Dfsn[p]=++c; sz[p]=1; for(intE=fst[p];e;e=Nxt[e]) { intB=vb[e];if(B==fa[p])Continue; FA[B]=up[b][0]=p; Fe[b]=vc[e]; mine[b][0]=Fe[b]; DEP[B]=dep[p]+1; DFS (b); sz[p]+=Sz[b]; }}voidbuild () {DFS (1); for(intg=1; g<d;g++) { for(intI=1; i<=n;i++) mine[i][g]=inf; } for(intg=1; g<d;g++) { for(intI=1; i<=n;i++) { if(!up[i][g-1])Continue; UP[I][G]=up[up[i][g-1]][g-1]; MINE[I][G]=min (mine[i][g-1],mine[up[i][g-1]][g-1]); } }}//Jump up (X=fa[x]) until Dep[x]=dintjmpintXintd) { for(inti=d-1; i>=0; i--) { if(!up[x][i]| | DEP[UP[X][I]]<D) _els x=Up[x][i]; } returnx;}intGminintXintd) { intminn=inf; for(inti=d-1; i>=0; i--) { if(!up[x][i]| | DEP[UP[X][I]]<D) _els minn=min (Minn,mine[x][i]), x=Up[x][i]; } returnMinn;}intLcaintXinty) { if(dep[x]>Dep[y]) swap (x, y); Y=jmp (y,dep[x]); if(x==y)returnx; for(inti=d-1; i>=0; i--) { if(Up[x][i]!=up[y][i]) x=up[x][i], y=Up[y][i]; } returnfa[x];}}//Virtual Treenamespacevt{#defineF_ First#defineS_ Secondtypedef pair<int,int>PII;//vs:points in Vtreeintsn,ss[sz],vn,vs[sz],stn=0, St[sz],vfa[sz],fc[sz],nc[sz];ll F[sz],vfe[sz];BOOLSaf[sz];BOOLCMP_DFSN (intAintb) {returnRT::d fsn[a]<RT::d fsn[b];} LL DP (intx) { if(!saf[x])returnf[x]=Vfe[x]; ll Sum=0; for(intC=FC[X];C;C=NC[C]) sum+=DP (c); returnf[x]=min (ll) vfe[x],sum);}voidbuild () {vn=stn=0; ss[++sn]=1; Sort (ss+1, ss+1+SN,CMP_DFSN); for(intI=1; i<=sn;i++) Vs[++vn]=ss[i], saf[ss[i]]=i==1; for(intI=1; i<=sn;i++) { intx=Ss[i]; if(!STN) {st[++stn]=x; vfa[x]=0;Continue;} intLca=Rt::lca (X,ST[STN]); for(; RT::d ep[st[stn]]>rt::d ep[lca];--STN) { if(RT::d ep[st[stn-1]]<=rt::d Ep[lca]) vfa[st[stn]]=LCA; } if(st[stn]!=LCA) {vs[++vn]=LCA; SAF[LCA]=1; VFA[LCA]=ST[STN]; st[++stn]=LCA; } Vfa[x]=lca; st[++stn]=x; } sort (vs+1, vs+1+VN,CMP_DFSN); for(intI=1; i<=vn;i++) fc[vs[i]]=0; for(intI=1; i<=vn;i++) { intX=vs[i]; f[x]=Inf_ll; if(i>1) vfe[x]=rt::gmin (x,rt::d ep[vfa[x]]); Elsevfe[x]=Inf_ll; intf=Vfa[x]; NC[X]=FC[F]; fc[f]=x; } PLL (DP (1)); PcTen);}}intMain () {rt::n=gi; for(intI=1; i<rt::n;i++) { intx=gi,y=gi,z=gi; Rt::adde (x, y, z); } rt::build (); intq=gi; while(q--) {VT::SN=gi; for(intI=1; i<=vt::sn;i++) vt::ss[i]=gi; Vt::build (); }}
bzoj2286 consumption War