1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| #include<iostream> #include<cstdio> #include<algorithm> #define ll long long using namespace std; namespace Ehnaev{ inline ll read() { ll ret=0,f=1;char ch=getchar(); while(ch<48||ch>57) {if(ch==45) f=-f;ch=getchar();} while(ch>=48&&ch<=57) {ret=(ret<<3)+(ret<<1)+ch-48;ch=getchar();} return ret*f; } inline void write(ll x) { static char buf[22];static ll len=-1; if(x>=0) {do{buf[++len]=x%10+48;x/=10;}while(x);} else {putchar(45);do{buf[++len]=-(x%10)+48;x/=10;}while(x);} while(len>=0) putchar(buf[len--]); } }using Ehnaev::read;using Ehnaev::write; inline void writeln(ll x) {write(x);putchar(10);}
const ll N=2e6,inf=(1ll<<31)-1;
ll n,k,tot,tc,cnt; ll flg[N+5],h[N+5],st[N+5]; ll ver[N+5],nxt[N+5],head[N+5],wt[N+5]; ll vc[N+5],nc[N+5],hc[N+5],wc[N+5]; ll st_mi[N+5][30],lg[N+5]; ll fa[N+5],dt[N+5],top[N+5],siz[N+5],id[N+5],a[N+5],hs[N+5];
inline void ST_Pre() { for(ll i=1;i<=n;i++) {st_mi[i][0]=a[i];} for(ll i=1;i<=n;i++) lg[i]=lg[i-1]+((1<<lg[i-1])==i); for(ll j=1;j<=lg[n];j++) { for(ll i=1;i+(1<<j)-1<=n;i++) { st_mi[i][j]=min(st_mi[i][j-1],st_mi[i+(1<<(j-1))][j-1]); } } }
inline ll Askstmi(ll l,ll r) { ll tmp=lg[r-l+1]-1; return min(st_mi[l][tmp],st_mi[r-(1<<tmp)+1][tmp]); }
inline ll Askmi(ll x,ll y) { ll ans=inf; while(top[x]!=top[y]) { if(dt[top[x]]<dt[top[y]]) swap(x,y); ans=min(ans,Askstmi(id[top[x]],id[x])); x=fa[top[x]]; } if(id[x]>id[y]) swap(x,y); if(id[x]+1<=id[y]) ans=min(ans,Askstmi(id[x]+1,id[y])); return ans; }
inline void Addedge(ll u,ll v,ll w) { ver[++tot]=v;nxt[tot]=head[u];wt[tot]=w;head[u]=tot; }
inline void Dfs(ll p,ll fath) { fa[p]=fath;dt[p]=dt[fath]+1;siz[p]=1; for(ll i=head[p];i;i=nxt[i]) { if(ver[i]==fath) continue; Dfs(ver[i],p);siz[p]+=siz[ver[i]]; if(siz[ver[i]]>siz[hs[p]]) hs[p]=ver[i]; } }
inline void Dfs_(ll p,ll fath,ll topf,ll tmpw) { id[p]=++cnt;a[cnt]=tmpw;top[p]=topf; if(!hs[p]) return; for(ll i=head[p];i;i=nxt[i]) {if(ver[i]==hs[p]) {tmpw=wt[i];break;}} Dfs_(hs[p],p,topf,tmpw); for(ll i=head[p];i;i=nxt[i]) { if(ver[i]==fath||ver[i]==hs[p]) continue; Dfs_(ver[i],p,ver[i],wt[i]); } }
inline ll Getlca(ll x,ll y) { while(top[x]!=top[y]) {if(dt[top[x]]<dt[top[y]]) swap(x,y);x=fa[top[x]];} return dt[x]<dt[y]?x:y; }
inline bool cmp(ll x,ll y) {return id[x]<id[y];}
inline void Addc(ll u,ll v) { vc[++tc]=v;nc[tc]=hc[u];wc[tc]=Askmi(u,v);hc[u]=tc; }
inline void Build() { sort(h+1,h+k+1,cmp); ll top=1; st[top]=1;tc=0;hc[1]=0; for(ll i=1,lca;i<=k;i++) { if(h[i]!=1) { lca=Getlca(h[i],st[top]); if(lca!=st[top]) { while(id[lca]<id[st[top-1]]) {Addc(st[top-1],st[top]);top--;} if(id[lca]>id[st[top-1]]) {hc[lca]=0;Addc(lca,st[top]);st[top]=lca;} else {Addc(lca,st[top--]);} } hc[h[i]]=0;st[++top]=h[i]; } } for(ll i=1;i<top;i++) Addc(st[i],st[i+1]); }
inline ll DP(ll p,ll fath) { ll res=0; for(ll i=hc[p];i;i=nc[i]) { if(vc[i]==fath) continue; if(!flg[vc[i]]) res=res+min(DP(vc[i],p),wc[i]); else res=res+wc[i]; } return res; }
int main() {
n=read();
for(ll i=1;i<n;i++) { ll u,v,w;u=read();v=read();w=read(); Addedge(u,v,w);Addedge(v,u,w); }
Dfs(1,0);Dfs_(1,0,1,0);ST_Pre();
ll m;m=read();
while(m--) { k=read(); for(ll i=1;i<=k;i++) {h[i]=read();flg[h[i]]=1;} Build();writeln(DP(1,0)); for(ll i=1;i<=k;i++) flg[h[i]]=0; }
return 0; }
|