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
| #include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 250005;
const LL INF = 1000000007ll*1000000007ll;
inline int getint() {
int r = 0; bool b = true; char c = getchar();
while ('0' > c || c > '9') {if (c == '-') b = false; c = getchar();}
while ('0' <= c && c <= '9') {r = r * 10 + (c - '0'); c = getchar();}
if (b) return r;
return -r;
}
struct edge_T { int to, next; LL w; };
bool vis[maxn];
LL query(int x, int y);
namespace VTree
{
edge_T edge[maxn];
int h[maxn], cnte, dsth[maxn], tot;
inline void ins(int x, int y) {
dsth[tot++] = x;
edge[++cnte].to = y;
edge[cnte].next = h[x];
edge[cnte].w = query(x, y);
h[x] = cnte;
}
LL dp(int now, LL W) {
if (vis[now]) return W;
LL sum = 0ll;
for (int i = h[now]; i; i = edge[i].next) sum += dp(edge[i].to, edge[i].w);
return min(sum, W);
}
inline void clear() {
for (int i = 0; i < tot; ++i) h[dsth[i]] = 0;
cnte = tot = 0;
}
}
edge_T edge[maxn << 1];
int n, m, q, cnte, sta[maxn], tail, h[maxn], dep[maxn], fa[maxn], top[maxn], siz[maxn], son[maxn], clo, dfn[maxn], a[maxn];
inline bool cmpdfn(int x, int y) {return dfn[x] < dfn[y];}
inline void ins(int x, int y, LL z) {
edge[++cnte].to = y;
edge[cnte].next = h[x];
edge[cnte].w = z;
h[x] = cnte;
}
inline int lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) x = fa[top[x]];
else y = fa[top[y]];
}
if (dep[x] < dep[y]) return x;
return y;
}
int anc[maxn][20];
LL ST[maxn][20];
void dfs1(int now, int father, int deep) {
dep[now] = deep;
anc[now][0] = fa[now] = father;
siz[now] = 1;
dfn[now] = ++clo;
for (int i = h[now]; i; i = edge[i].next) {
if (edge[i].to == father) continue;
ST[edge[i].to][0] = edge[i].w;
dfs1(edge[i].to, now, deep + 1);
siz[now] += siz[edge[i].to];
if (siz[edge[i].to] > siz[son[now]]) son[now] = edge[i].to;
}
}
void dfs2(int now, int tp) {
top[now] = tp;
if(son[now]) {
dfs2(son[now], tp);
for (int i = h[now]; i; i = edge[i].next) {
if (edge[i].to == fa[now] || edge[i].to == son[now]) continue;
dfs2(edge[i].to, edge[i].to);
}
}
}
int main() {
int x, y, z;
n = getint();
for (int i = 1; i < n; ++i) {
x = getint(); y = getint(); z = getint();
ins(x, y, z); ins(y, x, z);
}
dfs1(1, 0, 1);
dfs2(1, 1);
ST[1][0] = INF;
for (int k = 1; k < 20; ++k)
for (int i = 1; i <= n; ++i) {
anc[i][k] = anc[anc[i][k-1]][k-1];
if (anc[i][k] != 0) ST[i][k] = min(ST[i][k-1], ST[anc[i][k-1]][k-1]);
else ST[i][k] = INF;
}
sta[0] = 1;
q = getint();
while (q--) {
m = getint();
for (int i = 0; i < m; ++i) {a[i] = getint(); vis[a[i]] = true;}
sort(a, a + m, cmpdfn);
for (int i = 0; i < m; ++i) {
x = lca(a[i], sta[tail]);
while (tail > 0 && dep[sta[tail-1]] >= dep[x]) {
y = sta[tail--];
VTree::ins(sta[tail], y);
}
if (sta[tail] != x) {
VTree::ins(x, sta[tail--]);
sta[++tail] = x;
}
sta[++tail] = a[i];
}
while (tail) {
y = sta[tail--];
VTree::ins(sta[tail], y);
}
printf("%lld\n", VTree::dp(1, INF));
for (int i = 0; i < m; ++i) vis[a[i]] = false;
VTree::clear();
}
}
inline LL query(int x, int y) {
LL ret = INF;
for (int k = 19; k >= 0; --k) {
if (dep[anc[y][k]] >= dep[x]) {
ret = min(ret, ST[y][k]);
y = anc[y][k];
}
}
return ret;
}
|