没什么好说的,虚树+dp
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 500021
#define LL long long
using namespace std;
void read(int& x){
x=0;char c=getchar();
for(;c>'9'||c<'0';c=getchar());
for(;c>='0'&&c<='9';c=getchar())x=x*10+c-'0';
}
int f[maxn][21],n,m,sta[maxn],top,q[maxn],cnt;
int dfn[maxn],head[maxn],tot=1,h[maxn],vis[maxn];
LL dp[maxn],val[maxn];
struct edge{int v,next;LL w;}e[maxn];
void adde(int a,int b,LL c){
if(a==b)return;
e[tot].v=b,e[tot].next=head[a],e[tot].w=c;head[a]=tot++;
}
bool cmp(const int& a,const int& b){return dfn[a]<dfn[b];}
void dfs1(int u,int fa){
f[u][0]=fa,h[u]=h[fa]+1,dfn[u]=++cnt;
for(int i=1;i<=17;i++){
f[u][i]=f[f[u][i-1]][i-1];
if(f[u][i]==0)break;
}
for(int v,i=head[u];i;i=e[i].next){
if((v=e[i].v)==fa)continue;
val[v]=min(val[u],e[i].w);
dfs1(v,u);
}
}
int lca(int a,int b){
if(h[a]>h[b])swap(a,b);
for(int i=17;i>=0;i--)if(h[f[b][i]]>=h[a])b=f[b][i];
if(a==b)return a;
for(int i=17;i>=0;i--){
if(f[a][i]==f[b][i])continue;
a=f[a][i],b=f[b][i];
}return f[a][0];
}
void dfs2(int u){
LL all=0;
for(int i=head[u];i;i=e[i].next){
dfs2(e[i].v);
all+=dp[e[i].v];
}
head[u]=0;
dp[u]=val[u];
if(!vis[u])dp[u]=min(dp[u],all);
}
void solve(){
memset(head,0,sizeof(head));
int k;
while(m--){
tot=1,top=0;
read(k);
for(int i=1;i<=k;i++)read(q[i]),vis[q[i]]=1;
sort(q+1,q+1+k,cmp);sta[++top]=1;
for(int g,x,i=1;i<=k;i++){
x=q[i];g=lca(x,sta[top]);
while(dfn[sta[top]]>dfn[g]){
if(dfn[sta[top-1]]>=dfn[g])adde(sta[top-1],sta[top],0),top--;
else adde(g,sta[top],0),sta[top]=g;
}sta[++top]=x;
}
while(top>1)adde(sta[top-1],sta[top],0),top--;
dfs2(1);printf("%lld\n",dp[1]);
for(int i=1;i<=k;i++)vis[q[i]]=0;
}
}
int main(){
val[1]=1e15;
read(n);int a,b;int c;
for(int i=1;i<n;i++){
read(a),read(b),read(c);
adde(a,b,c),adde(b,a,c);
}dfs1(1,0);
read(m);
solve();
return 0;
}