-
Notifications
You must be signed in to change notification settings - Fork 0
/
YUKI_901.test.cpp
57 lines (52 loc) · 1.27 KB
/
YUKI_901.test.cpp
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
#define PROBLEM "https://yukicoder.me/problems/no/901"
#include "Template/template.hpp"
#include "Utility/fastio.hpp"
#include "Graph/lca.hpp"
#include "Graph/auxiliarytree.hpp"
int main() {
int n;
read(n);
AuxiliaryTree g(n);
LCA lca(n);
using P = pair<int, int>;
vector tree(n, vector<P>());
rep(_, 0, n - 1) {
int u, v, w;
read(u, v, w);
g.add_edge(u, v);
lca.add_edge(u, v);
tree[u].push_back({v, w});
tree[v].push_back({u, w});
}
g.run();
lca.run();
vector<ll> dep(n);
auto dfs = [&](auto &dfs, int v, int p) -> void {
for (auto &[to, w] : tree[v])
if (to != p) {
dep[to] = dep[v] + w;
dfs(dfs, to, v);
}
};
dfs(dfs, 0, -1);
auto dist = [&](int u, int v) -> ll {
return dep[u] + dep[v] - 2 * dep[lca.lca(u, v)];
};
int q;
read(q);
while (q--) {
int k;
read(k);
vector<int> vs(k);
read(vs);
g.query(vs);
ll ret = 0;
for (auto &v : vs)
for (auto &to : g.g[v])
ret += dist(v, to);
ret /= 2;
print(ret);
g.clear(vs);
}
return 0;
}