虚树裸题。
23333以后memset千万慎用。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxv 250050
#define maxe 500050
#define inf 0x7f7f7f7f7f7f7f7fLL
using namespace std;
struct edge
{
long long v,w,nxt;
}e[maxe],tr[maxe<<];
struct ask
{
long long pnt,dfn;
}q[maxv];
using namespace std;
long long n,x,y,z,g[maxv],nume=,numt=,gt[maxv],dfn[maxv],dis[maxv],anc[maxv][],mn[maxv][],dep[maxv];
long long m,k,stack[maxv],top=,times=,val[maxv],hash[maxv],now_cnt,fath[maxv],laste=;
long long read()
{
long long data=;char ch;
while (ch<'' || ch>'') ch=getchar();
do
{
data=data*+ch-'';
ch=getchar();
}while (ch>='' && ch<='');
return data;
}
bool cmp(ask x,ask y)
{
return x.dfn<y.dfn;
}
void addedge(long long u,long long v,long long w)
{
e[++nume].v=v;
e[nume].w=w;
e[nume].nxt=g[u];
g[u]=nume;
}
void addtree(long long u,long long v,long long w)
{
tr[++numt].v=v;
tr[numt].w=w;
if (gt[u]<=laste) tr[numt].nxt=;
else tr[numt].nxt=gt[u];
gt[u]=numt;
}
void dfs(long long x)
{
dfn[x]=++times;
for (long long i=g[x];i;i=e[i].nxt)
{
long long v=e[i].v;
if (v!=anc[x][])
{
anc[v][]=x;mn[v][]=e[i].w;dis[v]=dis[x]+e[i].w;
dep[v]=dep[x]+;
dfs(v);
}
}
}
void get_table()
{
for (long long e=;e<=;e++)
for (long long i=;i<=n;i++)
{
anc[i][e]=anc[anc[i][e-]][e-];
mn[i][e]=min(mn[i][e-],mn[anc[i][e-]][e-]);
}
}
void reset()
{
top=;
}
long long lca(long long x,long long y)
{
if (dep[x]<dep[y]) swap(x,y);
for (long long e=;e>=;e--)
{
if ((dep[anc[x][e]]>=dep[y]) && (anc[x][e]))
x=anc[x][e];
}
if (x==y) return x;
for (long long e=;e>=;e--)
{
if (anc[x][e]!=anc[y][e])
{
x=anc[x][e];
y=anc[y][e];
}
}
return anc[x][];
}
long long ask(long long x,long long y)
{
long long ret=inf;
for (long long e=;e>=;e--)
{
if ((dep[anc[x][e]]>=dep[y]) && (anc[x][e]))
{
ret=min(ret,mn[x][e]);
x=anc[x][e];
}
}
return ret;
}
void dp(long long x)
{
val[x]=;
for (long long i=gt[x];i;i=tr[i].nxt)
{
long long v=tr[i].v;
if (v!=fath[x])
{
dp(v);
if (hash[v]==now_cnt) val[x]+=tr[i].w;
else val[x]+=min(val[v],tr[i].w);
}
}
}
void work()
{
k=read();
for (long long i=;i<=k;i++)
{
x=read();
q[i].pnt=x;q[i].dfn=dfn[x];
hash[x]=now_cnt;
}
sort(q+,q+k+,cmp);
stack[++top]=;
for (long long i=;i<=k;i++)
{
long long x=q[i].pnt,fr=stack[top],se=stack[top-];
long long t=lca(x,fr);
if (t==fr) stack[++top]=x;
else
{
for (;;)
{
long long ret;
fr=stack[top];se=stack[top-];
long long t=lca(x,fr);
if (dfn[t]<dfn[se]) {ret=ask(fr,se);addtree(fr,se,ret);addtree(se,fr,ret);fath[fr]=se;top--;}
else if (dfn[t]==dfn[se]) {ret=ask(fr,se);addtree(fr,se,ret);addtree(se,fr,ret);fath[fr]=se;top--;break;}
else
{
long long ret=ask(fr,t);
addtree(fr,t,ret);addtree(t,fr,ret);
stack[top]=t;fath[fr]=t;break;
}
}
stack[++top]=x;
}
}
while (top>=)
{
long long fr=stack[top],se=stack[top-],ret=ask(fr,se);
addtree(fr,se,ret);
addtree(se,fr,ret);
fath[fr]=se;
top--;
}
dp();printf("%lld\n",val[]);
laste=numt;
return;
}
int main()
{
n=read();
for (long long i=;i<=n-;i++)
{
x=read();y=read();z=read();
addedge(x,y,z);addedge(y,x,z);
}
dfs();
get_table();
m=read();
for (long long i=;i<=m;i++)
{
reset();
now_cnt=i;
work();
}
return ;
}