bzoj3611

时间:2024-10-25 22:37:26

会构建虚树之后就是noip提高组的题目了
稍微难一点的是求代价和,只要注意按一个方向避免重复计算贡献即可

 const inf=;
type node=record
po,next:longint;
end; var mi,mx,fa,p,q,size,d,a,b:array[..] of longint;
e:array[..] of node;
f:array[..] of int64;
v:array[..] of boolean;
anc:array[..,..] of longint;
ans2,ans3,z,t,h,i,len,n,m,s,j,x,y:longint;
dis,ans1:int64; procedure add(x,y:longint);
begin
inc(len);
e[len].po:=y;
e[len].next:=p[x];
p[x]:=len;
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; procedure dfs(x:longint);
var i,y:longint;
begin
inc(h);
a[x]:=h;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if fa[x]<>y then
begin
fa[y]:=x;
anc[y,]:=x;
d[y]:=d[x]+;
dfs(y);
end;
i:=e[i].next;
end;
end; procedure sort(l,r:longint);
var i,j,x:longint;
begin
i:=l;
j:=r;
x:=a[b[(l+r) shr ]];
repeat
while a[b[i]]<x do inc(i);
while x<a[b[j]] do dec(j);
if not(i>j) then
begin
swap(b[i],b[j]);
inc(i);
dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; function lca(x,y:longint):longint;
var i,p:longint;
begin
if x=y then exit(x);
if d[x]<d[y] then swap(x,y);
p:=trunc(ln(d[x])/ln());
if d[x]>d[y] then
for i:=p downto do
if d[x]- shl i>=d[y] then x:=anc[x,i];
if x=y then exit(x);
for i:=p downto do
if (anc[y,i]<>anc[x,i]) then
begin
x:=anc[x,i];
y:=anc[y,i];
end;
exit(fa[x]);
end; procedure dp(x:longint);
var i,y:longint;
begin
f[x]:=;
if v[x] then
begin
size[x]:=; //size[]表示虚树中包含的关键点个数
mi[x]:=;
mx[x]:=;
end
else begin
size[x]:=;
mi[x]:=inf;
mx[x]:=-inf;
end;
i:=p[x];
while i<> do
begin
y:=e[i].po;
dp(y);
dis:=d[y]-d[x];
ans1:=ans1+(f[x]+int64(size[x])*dis)*int64(size[y])+f[y]*int64(size[x]);
size[x]:=size[x]+size[y];
f[x]:=f[x]+f[y]+int64(size[y])*dis;
ans2:=min(ans2,mi[x]+dis+mi[y]);
ans3:=max(ans3,mx[x]+dis+mx[y]);
mx[x]:=max(mx[x],dis+mx[y]);
mi[x]:=min(mi[x],dis+mi[y]);
i:=e[i].next;
end;
p[x]:=;
end; begin
readln(n);
for i:= to n- do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
dfs();
t:=trunc(ln(n)/ln());
for i:= to t do
for j:= to n do
begin
y:=anc[j,i-];
if y<> then anc[j,i]:=anc[y,i-];
end;
fillchar(p,sizeof(p),);
readln(m);
for i:= to m do
begin
readln(s);
for j:= to s do
begin
read(b[j]);
v[b[j]]:=true;
end;
readln;
sort(,s);
ans1:=;
ans2:=inf;
ans3:=;
len:=;
t:=;
q[]:=;
for j:= to s do
begin
x:=b[j];
z:=lca(x,q[t]);
while d[z]<d[q[t]] do
begin
if d[z]>=d[q[t-]] then
begin
add(z,q[t]);
dec(t);
if q[t]<>z then
begin
inc(t);
q[t]:=z;
end;
break;
end;
add(q[t-],q[t]);
dec(t);
end;
if q[t]<>x then
begin
inc(t);
q[t]:=x;
end;
end;
while t> do
begin
add(q[t-],q[t]);
dec(t);
end;
dp();
writeln(ans1,' ',ans2,' ',ans3);
for j:= to s do
v[b[j]]:=false;
end;
end.