#include <bits/stdc++.h>
typedef long long ll;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n;
std::cin >> n;
std::vector<std::vector<std::pair<ll, ll>>> adj(n);
for (ll i = 0, u, v, w; i < n - 1; ++i) {
std::cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
std::vector<std::vector<ll>> lift(n, std::vector<ll>(16));
std::vector<std::vector<ll>> lift_sum(n, std::vector<ll>(16));
std::vector<ll> depth(n);
std::function<void(ll, ll)> dfs;
dfs = [&](ll node, ll par) {
for (auto &[i, w] : adj[node]) {
if (i == par) {
continue;
}
lift[i][0] = node;
lift_sum[i][0] = w;
depth[i] = depth[node] + 1;
dfs(i, node);
}
};
dfs(0, -1);
for (ll bt = 1; bt < 16; ++bt) {
for (ll i = 0; i < n; ++i) {
lift[i][bt] = lift[lift[i][bt - 1]][bt - 1];
lift_sum[i][bt] = lift_sum[i][bt - 1] + lift_sum[lift[i][bt - 1]][bt - 1];
}
}
auto lca = [&](ll u, ll v) {
if (depth[u] > depth[v]) {
std::swap(u, v);
}
ll k = depth[v] - depth[u];
for (ll bt = 0; bt < 16; ++bt) {
if (k & (1 << bt)) {
v = lift[v][bt];
}
}
if (u == v) {
return u;
}
for (ll bt = 15; bt >= 0; --bt) {
if (lift[u][bt] != lift[v][bt]) {
u = lift[u][bt];
v = lift[v][bt];
}
}
return lift[u][0];
};
auto lca_mult = [&](const std::vector<ll> &nodes) {
ll ans = nodes[0];
for (ll i = 1; i < nodes.size(); ++i) {
ans = lca(ans, nodes[i]);
}
return ans;
};
auto sum = [&](ll from, ll to) {
ll k = depth[from] - depth[to];
ll ans = 0;
for (ll bt = 0; bt < 16; ++bt) {
if (k & (1 << bt)) {
ans += lift_sum[from][bt];
from = lift[from][bt];
}
}
return ans;
};
ll q;
std::cin >> q;
while (q--) {
std::vector<ll> arr(5);
std::cin >> arr[0] >> arr[1] >> arr[2] >> arr[3] >> arr[4];
ll ans = 0;
while (arr.size() > 1) {
std::pair<ll, std::pair<ll, ll>> best;
for (ll i = 0; i < arr.size(); ++i) {
for (ll j = i + 1; j < arr.size(); ++j) {
best = std::max(best, {depth[lca(arr[i], arr[j])], {arr[i], arr[j]}});
}
}
auto &[u, v] = best.second;
auto LCA = lca(u, v);
arr.erase(std::remove(arr.begin(), arr.end(), u), arr.end());
arr.erase(std::remove(arr.begin(), arr.end(), v), arr.end());
arr.push_back(LCA);
ans += sum(u, LCA);
ans += sum(v, LCA);
}
std::cout << ans << '\n';
}
}
Compilation message
roadsideadverts.cpp: In lambda function:
roadsideadverts.cpp:66:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (ll i = 1; i < nodes.size(); ++i) {
| ~~^~~~~~~~~~~~~~
roadsideadverts.cpp: In function 'int main()':
roadsideadverts.cpp:92:24: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (ll i = 0; i < arr.size(); ++i) {
| ~~^~~~~~~~~~~~
roadsideadverts.cpp:93:30: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (ll j = i + 1; j < arr.size(); ++j) {
| ~~^~~~~~~~~~~~
roadsideadverts.cpp:64:8: warning: variable 'lca_mult' set but not used [-Wunused-but-set-variable]
64 | auto lca_mult = [&](const std::vector<ll> &nodes) {
| ^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
24604 KB |
Output is correct |
2 |
Correct |
91 ms |
27640 KB |
Output is correct |
3 |
Correct |
108 ms |
27624 KB |
Output is correct |
4 |
Correct |
99 ms |
27672 KB |
Output is correct |
5 |
Correct |
105 ms |
27656 KB |
Output is correct |
6 |
Correct |
119 ms |
27476 KB |
Output is correct |
7 |
Correct |
91 ms |
27476 KB |
Output is correct |
8 |
Correct |
99 ms |
27544 KB |
Output is correct |
9 |
Correct |
97 ms |
27456 KB |
Output is correct |
10 |
Correct |
87 ms |
27476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
21580 KB |
Output is correct |
2 |
Correct |
47 ms |
26216 KB |
Output is correct |
3 |
Correct |
42 ms |
26156 KB |
Output is correct |
4 |
Correct |
49 ms |
26228 KB |
Output is correct |
5 |
Correct |
50 ms |
26320 KB |
Output is correct |
6 |
Correct |
50 ms |
26312 KB |
Output is correct |
7 |
Correct |
41 ms |
26204 KB |
Output is correct |
8 |
Correct |
48 ms |
26324 KB |
Output is correct |
9 |
Correct |
42 ms |
26100 KB |
Output is correct |
10 |
Correct |
47 ms |
26200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
113 ms |
24604 KB |
Output is correct |
3 |
Correct |
91 ms |
27640 KB |
Output is correct |
4 |
Correct |
108 ms |
27624 KB |
Output is correct |
5 |
Correct |
99 ms |
27672 KB |
Output is correct |
6 |
Correct |
105 ms |
27656 KB |
Output is correct |
7 |
Correct |
119 ms |
27476 KB |
Output is correct |
8 |
Correct |
91 ms |
27476 KB |
Output is correct |
9 |
Correct |
99 ms |
27544 KB |
Output is correct |
10 |
Correct |
97 ms |
27456 KB |
Output is correct |
11 |
Correct |
87 ms |
27476 KB |
Output is correct |
12 |
Correct |
33 ms |
21580 KB |
Output is correct |
13 |
Correct |
47 ms |
26216 KB |
Output is correct |
14 |
Correct |
42 ms |
26156 KB |
Output is correct |
15 |
Correct |
49 ms |
26228 KB |
Output is correct |
16 |
Correct |
50 ms |
26320 KB |
Output is correct |
17 |
Correct |
50 ms |
26312 KB |
Output is correct |
18 |
Correct |
41 ms |
26204 KB |
Output is correct |
19 |
Correct |
48 ms |
26324 KB |
Output is correct |
20 |
Correct |
42 ms |
26100 KB |
Output is correct |
21 |
Correct |
47 ms |
26200 KB |
Output is correct |
22 |
Correct |
102 ms |
24660 KB |
Output is correct |
23 |
Correct |
64 ms |
22100 KB |
Output is correct |
24 |
Correct |
92 ms |
26452 KB |
Output is correct |
25 |
Correct |
86 ms |
26480 KB |
Output is correct |
26 |
Correct |
88 ms |
26452 KB |
Output is correct |
27 |
Correct |
82 ms |
26580 KB |
Output is correct |
28 |
Correct |
89 ms |
26452 KB |
Output is correct |
29 |
Correct |
86 ms |
26452 KB |
Output is correct |
30 |
Correct |
85 ms |
26492 KB |
Output is correct |
31 |
Correct |
88 ms |
26456 KB |
Output is correct |