Exercises
After the consumption war, the difficulty of this problem is only in the statistical ans.
I originally asked for the longest (short) chain will only retain the suboptimal value, and then open three arrays to write a particularly troublesome ...
Studied today, Orz POPOQQQ.
InlinevoidDfsintx) {f[x]=v[x];g[x]=0; MI[X]=V[X]?0: INF; MX[X]=V[X]?0:-inf; For4 (i,x) {dfs (y); Ans+ = (G[X]+F[X]*E[I].W) *f[y]+g[y]*F[x]; F[X]+=F[y]; G[X]+=g[y]+ (LL) e[i].w*F[y]; Ans1=min (ans1,mi[x]+mi[y]+E[I].W); Ans2=max (ans2,mx[x]+mx[y]+E[I].W); MI[X]=min (mi[x],mi[y]+E[I].W); MX[X]=max (mx[x],mx[y]+E[I].W); } Head[x]=0; }
Instead, the LCA of the endpoint of the longest chain is enumerated directly, and the "prefix and" nature of the MI,MX array is fully exploited. Orzzzzzzzzzzzzz
Then the sum of the statistics is very simple, divided into two parts of the contribution can be.
Code:
1#include <cstdio>2#include <cstdlib>3#include <cmath>4#include <cstring>5#include <algorithm>6#include <iostream>7#include <vector>8#include <map>9#include <Set>Ten#include <queue> One#include <string> A #defineINF 1000000000 - #defineMAXN 1000000+5 - #defineMAXM 100000+5 the #defineEPS 1e-10 - #definell Long Long - #definePA pair<int,int> - #defineFor0 (i,n) for (int i=0;i<= (n); i++) + #defineFor1 (i,n) for (int i=1;i<= (n); i++) - #defineFor2 (i,x,y) for (int i= (x); i<= (y); i++) + #defineFor3 (i,x,y) for (int i= (x); i>= (y); i--) A #defineFor4 (i,x) for (int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) at #defineMoD 1000000007 - using namespacestd; -InlineintRead () - { - intx=0, f=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} in while(ch>='0'&&ch<='9') {x=Ten*x+ch-'0'; ch=GetChar ();} - returnx*F; to } + intn,m,id[maxn],cnt,dep[maxn],fa[maxn][ +],F[MAXN],MI[MAXN],MX[MAXN]; - ll G[MAXN]; the BOOLV[MAXN]; * intAns1,ans2; $ ll ans;Panax NotoginsengInlineintLcaintXinty) - { the if(dep[x]<Dep[y]) swap (x, y); + intt=dep[x]-Dep[y]; AFor0 (I, -)if(t& (1<<i)) x=Fa[x][i]; the if(x==y)returnx; +For3 (I, -,0)if(Fa[x][i]!=fa[y][i]) x=fa[x][i],y=Fa[y][i]; - returnfa[x][0]; $ } $ structGraph - { - intHead[maxn],tot; the structedge{intGo,next,w;} e[2*MAXN]; -InlinevoidAddintXinty)Wuyi { theE[++tot]= (Edge) {y,head[x],0};head[x]=tot; -E[++tot]= (Edge) {X,head[y],0};head[y]=tot; Wu } -InlinevoidADDD (intXinty) About { $E[++tot]= (Edge) {y,head[x],dep[y]-dep[x]};head[x]=tot; - } -InlinevoidDfsintXintf) - { Aid[x]=++CNT; +For1 (I, -) the if(dep[x]>=1<<i) fa[x][i]=fa[fa[x][i-1]][i-1]; - Else Break; $For4 (i,x)if(y!=f) the { thedep[y]=dep[x]+1; fa[y][0]=x; the DFS (y,x); the } - } inInlinevoidDfsintx) the { thef[x]=v[x];g[x]=0; AboutMI[X]=V[X]?0: INF; theMX[X]=V[X]?0:-inf; the For4 (i,x) the { + dfs (y); -ans+= (G[X]+F[X]*E[I].W) *f[y]+g[y]*F[x]; thef[x]+=F[y];Bayig[x]+=g[y]+ (LL) e[i].w*F[y]; theAns1=min (ans1,mi[x]+mi[y]+E[I].W); theAns2=max (ans2,mx[x]+mx[y]+E[I].W); -Mi[x]=min (mi[x],mi[y]+E[I].W); -Mx[x]=max (mx[x],mx[y]+E[I].W); the } thehead[x]=0; the } the }g1,g2; - intA[maxn],sta[maxn],top; theInlineBOOLcmpintXintY) {returnid[x]<id[y];} the intMain () the {94Freopen ("Input.txt","R", stdin); theFreopen ("output.txt","W", stdout); then=read (); theFor1 (i,n-1) G1.add (read (), read ());98G1.dfs (1,0); About intt=read (); - while(t--)101 {102M=read (); ans=0; ans1=inf;ans2=-inf;103For1 (i,m) a[i]=read ();104Sort (A +1, a+m+1, CMP); theFor1 (i,m) v[a[i]]=1;106sta[top=1]=1; g2.tot=0;107 For1 (i,m)108 {109 intx=a[i],f=LCA (X,sta[top]); the while(dep[f]<Dep[sta[top]])111 { the if(dep[f]>=dep[sta[top-1]])113 { theG2.ADDD (f,sta[top--]); the if(sta[top]!=f) sta[++top]=F; the Break;117 }118G2.ADDD (sta[top-1],sta[top]); top--;119 } - if(sta[top]!=x) sta[++top]=x;121 }122 while(--top) G2.ADDD (sta[top],sta[top+1]);123G2.dfs (1);124printf"%lld%d%d\n", ans,ans1,ans2); theFor1 (i,m) v[a[i]]=0;126 } 127 return 0; -}
View Code
BZOJ3611: [Heoi2014] Big project