Virtual Tree + Tree DP
Orz zyf ... Good god ...
Build the virtual tree first by DFN sort, and then use a monotonous stack to maintain the current "chain", toward the Riga side ... Honestly, I haven't figured it out yet--
Leave a hole ...
Re reason: This chain to go out of time did not write top--, in line 112th ... Cause a cycle of death!
1 /**************************************************************2 problem:22863 User:tunix4 language:c++5 result:accepted6 time:6616 Ms7 memory:60120 KB8 ****************************************************************/9 Ten //Bzoj 2286 One#include <cstdio> A#include <cstring> -#include <cstdlib> -#include <iostream> the#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) + using namespacestd; -InlineintGetint () { + intv=0, sign=1;CharCh=GetChar (); A while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} at while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} - returnv*=Sign ; - } - /******************tamplate*********************/ - Const intn=250010; - Const Long Longinf=100000000000LL; intypedefLong LongLL; - intn,m,dfn[n],dfs_clock=0, dep[n],fa[n][ -],g[n][ -],a[n]; to LL Dp[n]; + BOOLV[n];//Mark Resource points - theInlineintLCA (intXinty) { * if(dep[x]<Dep[y]) swap (x, y); $ intt=dep[x]-Dep[y];Panax NotoginsengF (I,0, -)if(t& (1<<i)) x=Fa[x][i]; - if(x==y)returnx; theD (I, -,0)if(Fa[x][i]!=fa[y][i]) x=fa[x][i],y=Fa[y][i]; + returnfa[x][0]; A } theInlineintDistintXinty) { + intt=dep[x]-dep[y],ans=1000000000; -D (I, -,0)if(t& (1<<i) Ans=min (Ans,g[x][i]), x=Fa[x][i]; $ returnans; $ } - - structgraph{ the inthead[n],to[n<<1],next[n<<1],len[n<<1],cnt; - voidAddintXintYintl) {WuyiTo[++cnt]=y; NEXT[CNT]=HEAD[X]; head[x]=cnt;len[cnt]=l; theTo[++cnt]=x; Next[cnt]=head[y]; head[y]=cnt;len[cnt]=l; - } Wu voidAddintXinty) { -To[++cnt]=y; NEXT[CNT]=HEAD[X]; head[x]=CNT; About } $ voidDfsintx) { -dfn[x]=++Dfs_clock; -F (I,1, -) - if(Dep[x]>= (1<<i)) { Afa[x][i]=fa[fa[x][i-1]][i-1]; +G[x][i]=min (g[x][i-1],g[fa[x][i-1]][i-1]); the } - Else Break; $ for(intI=head[x];i;i=Next[i]) the if(to[i]!=fa[x][0]){ thefa[to[i]][0]=x; theg[to[i]][0]=Len[i]; thedep[to[i]]=dep[x]+1; - DFS (To[i]); in } the } the voidDfsintXintf) { About //Dp[x] Represents the minimum cost of a resource point that causes X to not reach any one descendant thedp[x]=0; the for(intI=head[x];i;i=Next[i]) the if(to[i]!=f) { + DFS (to[i],x); -Dp[x]+=min (V[to[i])?Inf:dp[to[i]], (LL) dist (to[i],x)); the //If this child node is an energy point, you must chop this edgeBayi } thehead[x]=0; the //empty, preprocessing for the next query, but each node will only be visited once ... - } - }g1,g2; theInlineBOOLcmpintAintb) {returndfn[a]<dfn[b];} the intst[n],top=0; the intMain () { then=getint (); - intx, y, z theF (I,2, N) { theX=getint (); Y=getint (); z=getint (); the g1.add (x, y, z);94 } thedep[1]=1; theG1.dfs (1); the intt=getint ();98 while(t--){ Aboutm=getint (); -F (I,1, m) {a[i]=getint (); v[a[i]]=1;}101Sort (A +1, a+m+1, CMP);102 //Stack Maintenance build virtual tree103st[top=1]=1; G2.cnt=0;104F (I,1, M) { the intx=a[i],f=LCA (X,st[top]);106 while(dep[f]<Dep[st[top]]) {107 if(dep[f]>=dep[st[top-1]]){108G2.add (f,st[top--]);109 if(st[top]!=f) st[++top]=F; the Break;111 } theG2.add (st[top-1],st[top]); top--;113 } the if(st[top]!=x) st[++top]=x; the } the while(--top) G2.add (st[top],st[top+1]);117G2.dfs (1,0);118printf"%lld\n", dp[1]);119F (I,1, m) v[a[i]]=0; - }121 return 0;122}
View Code
"Bzoj" "2286" "SDOI2011" consumption war