-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualTree.cpp
More file actions
169 lines (150 loc) · 3.87 KB
/
VirtualTree.cpp
File metadata and controls
169 lines (150 loc) · 3.87 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Vittual tree solved for "P_2495_SDOI_2011_消耗战" on luogu
#include<bits/stdc++.h>
using namespace std;
// #define _DEBUG
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#ifdef _DEBUG
#define debug(a) cout << #a << '=' << a << endl;
#else
#define debug(a) ;
#endif
#define eb emplace_back
#define pb push_back
#define int long long
typedef long long LL;
typedef pair<int, int> pii;
const LL inf = 0x3f3f3f3f3f3f3f3f;
bool Mbe;
const int N = 2.5e5 + 10, M = 5e5 + 10;
int h[N], e[N << 1], ne[N << 1], val[N << 1], pre[N][25], dep[N], dfn[N], cur, cnt;
int h2[N], e2[N << 1], ne2[N << 1], cur2;
bitset<M> need;
int minv[N];
void add(int a, int b, int w) {
e[cur] = b, ne[cur] = h[a], h[a] = cur, val[cur++] = w;
}
void add2(int a, int b) {
e2[cur2] = b, ne2[cur2] = h2[a], h2[a] = cur2++;
}
void dfs(int u, int f) {
if(u == 1) dep[1] = 1, minv[1] = inf;
dfn[u] = ++cnt;
for(int i = h[u]; ~i; i = ne[i]) {
int node = e[i];
// debug(node)
if(node == f) continue;
dep[node] = dep[u] + 1;
pre[node][0] = u;
// what we need!
minv[node] = min(minv[u], val[i]);
dfs(node, u);
}
}
int lca(int a, int b) {
if(dep[a] < dep[b]) swap(a, b);
// debug(dep[a]) debug(dep[b])
for(int i = 22; ~i; i--) {
// debug(dep[pre[a][i]])
if(dep[pre[a][i]] >= dep[b]) {
a = pre[a][i];
}
}
if(a == b) {
return a;
}
for(int i = 22; ~i; i--) {
if(pre[a][i] != pre[b][i]) {
a = pre[a][i];
b = pre[b][i];
}
}
return pre[a][0];
}
bool cmp(int a, int b) {
return dfn[a] < dfn[b];
}
LL dfs2(int u) {
LL res = 0;
for(int i = h2[u]; ~i; i = ne2[i]) {
int node = e2[i];
// if(node == f) continue;
res += dfs2(node);
// debug(node)
// // debug(val2[i])
// debug(val2[i])
// dp[u] += (st.count(node) ? val2[i] : min(val2[i], dp[node]));
}
if(need[u]) {
res = minv[u];
}
else {
res = min(res, 1LL * minv[u]);
}
// ayeeeeeeeeeeeeeeeeeeeeee
need[u] = 0;
h2[u] = -1;
debug(u)
return res;
}
void solve() {
#define only_one
int n; cin >> n;
memset(h, -1, sizeof h);
for (int i = 0; i < n - 1; i++) {
int u, v, w; cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
dfs(1, -1);
//debug("okdfs")
for(int i = 1; i <= 22; i++) {
for(int j = 1; j <= n; j++) {
// debug(i) debug(j)
pre[j][i] = pre[pre[j][i - 1]][i - 1];
}
}
//debug("ok")
int m; cin >> m;
memset(h2, -1, sizeof(int) * (n + 5));//the number of nodes must be within boundary
while(m--) {
// why is it this place???
int num; cin >> num;
cur2 = 0;
//debug(num)
vector<int> q(num);
for(auto &c: q) cin >> c, need[c] = 1;
q.push_back(1);
sort(q.begin(), q.end(), cmp);
vector<int> tmp;
for(int i = 0; i < q.size() - 1; i++) {
tmp.eb(q[i]);
int l = lca(q[i], q[i + 1]);
tmp.eb(l);
}
tmp.eb(q.back());
sort(tmp.begin(), tmp.end(), cmp);
tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
for(auto c: tmp) debug(c)
// for(auto c: q) debug(c)
for(int i = 0; i < tmp.size() - 1; i++) {
int l = lca(tmp[i], tmp[i + 1]);
//debug(l) debug(q[i + 1])
add2(l, tmp[i + 1]);//if you add the weight, you are completely ignorant of trDP
}
cout << dfs2(1) << '\n';
}
}
// bool Med;
signed main() {
std::ios::sync_with_stdio(false);
//fprintf(stderr, "%.3lf MB\n", (&Med - &Mbe) / 1048576.0);
cin.tie(0);
int T = 1;
#ifndef only_one
cin >> T;
#endif
while(T--) solve();
return 0;
}