通过这题我知道了一个鬼故事,trunc(ln(128)/ln(2))=6……以后不敢轻易这么写了
好了言归正传,这题明显的构建虚树,但构建虚树后怎么树形dp呢?
由于虚树上的点不仅是议事会还有可能是议事会的LCA,所以
我们要先求出虚树上每个点是被那个议事会管理的,这我们可以通过两遍dfs求出(儿子更新父亲,父亲更新儿子)
然后我们考虑虚树上每条边所代表原数的结点归属就可以了,这个地方细节比较多,建议自己想,具体见代码注释
type node=record
po,next:longint;
end;
point=record
fr,ds:longint;
end; var e:array[..] of node;
f,b,c,d,a,st,ans,p,s:array[..] of longint;
anc:array[..,..] of longint;
w:array[..] of point;
n,q,m,i,len,j,x,y,t,z:longint;
v:array[..] of boolean; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure add(x,y:longint);
begin
inc(len);
e[len].po:=y;
e[len].next:=p[x];
p[x]:=len;
end; procedure dfs(x:longint);
var i,y:longint;
begin
inc(t);
a[x]:=t;
s[x]:=;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if s[y]= then
begin
anc[y,]:=x;
d[y]:=d[x]+;
dfs(y);
s[x]:=s[x]+s[y];
end;
i:=e[i].next;
end;
end; procedure sort(l,r:longint);
var i,j,x:longint;
begin
i:=l;
j:=r;
x:=c[(l+r) shr ];
repeat
while a[c[i]]<a[x] do inc(i);
while a[x]<a[c[j]] do dec(j);
if not(i>j) then
begin
swap(c[i],c[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:longint;
begin
if x=y then exit(x);
if d[x]<d[y] then swap(x,y);
if d[x]>d[y] then
begin
for i:= downto do
if d[x]- shl i>=d[y] then x:=anc[x,i];
end;
if x=y then exit(x);
for i:= downto do
if anc[x,i]<>anc[y,i] then
begin
x:=anc[x,i];
y:=anc[y,i];
end;
exit(anc[x,]);
end; procedure get(var a:point;x,y:longint);
begin
if (a.ds>x) or (a.ds=x) and (a.fr>y) then
begin
a.ds:=x;
a.fr:=y;
end;
end; function find(x,h:longint):longint;
var i:longint;
begin
if h= then exit(x);
for i:= downto do
if h- shl i>= then
begin
x:=anc[x,i];
h:=h- shl i;
if h= then break;
end;
exit(x);
end; procedure work1(x:longint);
var i,y:longint;
begin
if v[x] then
begin
w[x].fr:=x;
w[x].ds:=;
end
else begin
w[x].fr:=n+;
w[x].ds:=;
end;
f[x]:=s[x];
i:=p[x];
while i<> do
begin
y:=e[i].po;
f[x]:=f[x]-s[find(y,d[y]-d[x]-)];
work1(y);
get(w[x],w[y].ds+d[y]-d[x],w[y].fr);
i:=e[i].next;
end;
end; procedure work2(x:longint);
var i,y:longint;
begin
i:=p[x];
while i<> do
begin
y:=e[i].po;
get(w[y],w[x].ds+d[y]-d[x],w[x].fr);
work2(y);
i:=e[i].next;
end;
end; procedure calc(x:longint);
var i,y,l,h:longint;
begin
inc(ans[b[w[x].fr]],f[x]); //我们先单独考虑边的端点
i:=p[x];
while i<> do
begin
y:=e[i].po;
if w[x].fr=w[y].fr then
inc(ans[b[w[x].fr]],s[find(y,d[y]-d[x]-)]-s[y])
else begin
l:=w[x].ds+w[y].ds+d[y]-d[x];
h:=l div -w[y].ds; //均分
if (l mod =) and (w[x].fr<w[y].fr) then dec(h); //注意临界情况
h:=find(y,h); //寻找向上d个单位的点
inc(ans[b[w[x].fr]],s[find(y,d[y]-d[x]-)]-s[h]); //注意这里的结点归属
inc(ans[b[w[y].fr]],s[h]-s[y]);
end;
calc(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();
for j:= to trunc(ln(n)/ln()) do
for i:= to n do
begin
x:=anc[i,j-];
anc[i,j]:=anc[x,j-];
end; len:=;
fillchar(p,sizeof(p),);
readln(m);
while m> do
begin
dec(m);
len:=;
readln(q);
for i:= to q do
begin
read(c[i]);
b[c[i]]:=i;
v[c[i]]:=true;
end;
sort(,q);
st[]:=;
t:=;
for i:= to q do
begin
x:=c[i];
z:=lca(x,st[t]);
while d[z]<d[st[t]] do
begin
if d[z]>=d[st[t-]] then
begin
add(z,st[t]);
dec(t);
if st[t]<>z then
begin
inc(t);
st[t]:=z;
end;
break;
end;
add(st[t-],st[t]);
dec(t);
end;
if st[t]<>x then
begin
inc(t);
st[t]:=x;
end;
end;
while t> do
begin
add(st[t-],st[t]);
dec(t);
end;
work1();
work2();
calc();
for i:= to q do
begin
write(ans[i],' ');
ans[i]:=;
b[c[i]]:=;
v[c[i]]:=false;
end;
writeln;
end;
end.