#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
using ll = long long;
const int N = 50'000 + 10;
int n, q;
vector<pair<int, int>> g[N];
int Lg, dist[N], parent[20][N];
int cnt, in[N];
int sDist[N];
void dfs(int u, int p) {
in[u] = ++ cnt;
for (const auto& x : g[u]) {
int v = x.first, w = x.second;
if (v == p) continue;
parent[0][v] = u;
dist[v] = dist[u] + 1;
sDist[v] = sDist[u] + w;
dfs(v, u);
}
};
int lca(int u, int v) {
if (dist[u] < dist[v]) swap(u, v);
int delta = dist[u] - dist[v];
for (int lg = Lg; lg >= 0; -- lg) {
if (((delta >> lg) & 1)) {
u = parent[lg][u];
}
}
if (u == v) return u;
for (int lg = Lg; lg >= 0; -- lg) {
if (parent[lg][u] != parent[lg][v]) {
u = parent[lg][u];
v = parent[lg][v];
}
}
return parent[0][u];
}
int getSdist(int u, int p) {
return sDist[u] - sDist[p];
}
int32_t main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr); std::cout.tie(nullptr);
cin >> n;
for (int i = 2; i <= n; ++ i) {
int u, v, w; cin >> u >> v >> w;
++ u, ++ v;
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
}
Lg = __lg(n);
dist[1] = 1; dfs(1, 0);
for (int lg = 1; lg <= Lg; ++ lg) {
for (int u = 1; u <= n; ++ u) {
parent[lg][u] = parent[lg - 1][parent[lg - 1][u]];
}
}
cin >> q;
while (q -- > 0) {
int ver[5];
for (int i = 0; i < 5; ++ i) cin >> ver[i], ++ ver[i];
sort(ver, ver + 5, [&] (int u, int v) -> bool {
return in[u] < in[v];
});
int anc = lca(ver[0], ver[1]);
for (int i = 2; i < 5; ++ i) {
anc = lca(anc, ver[i]);
}
int res = getSdist(ver[0], anc);
for (int i = 1; i < 5; ++ i) {
res += getSdist(ver[i], lca(ver[i], ver[i - 1]));
}
cout << res << '\n';
}
return (0 ^ 0);
}
/// Code by vuavisao
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
7852 KB |
Output is correct |
2 |
Correct |
29 ms |
9552 KB |
Output is correct |
3 |
Correct |
31 ms |
9752 KB |
Output is correct |
4 |
Correct |
30 ms |
9724 KB |
Output is correct |
5 |
Correct |
31 ms |
9560 KB |
Output is correct |
6 |
Correct |
30 ms |
9612 KB |
Output is correct |
7 |
Correct |
31 ms |
9552 KB |
Output is correct |
8 |
Correct |
36 ms |
9560 KB |
Output is correct |
9 |
Correct |
30 ms |
9564 KB |
Output is correct |
10 |
Correct |
30 ms |
9556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
7000 KB |
Output is correct |
2 |
Correct |
20 ms |
9044 KB |
Output is correct |
3 |
Correct |
19 ms |
9044 KB |
Output is correct |
4 |
Correct |
19 ms |
9048 KB |
Output is correct |
5 |
Correct |
19 ms |
9084 KB |
Output is correct |
6 |
Correct |
17 ms |
9048 KB |
Output is correct |
7 |
Correct |
20 ms |
9048 KB |
Output is correct |
8 |
Correct |
17 ms |
9180 KB |
Output is correct |
9 |
Correct |
17 ms |
9052 KB |
Output is correct |
10 |
Correct |
17 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
31 ms |
7852 KB |
Output is correct |
3 |
Correct |
29 ms |
9552 KB |
Output is correct |
4 |
Correct |
31 ms |
9752 KB |
Output is correct |
5 |
Correct |
30 ms |
9724 KB |
Output is correct |
6 |
Correct |
31 ms |
9560 KB |
Output is correct |
7 |
Correct |
30 ms |
9612 KB |
Output is correct |
8 |
Correct |
31 ms |
9552 KB |
Output is correct |
9 |
Correct |
36 ms |
9560 KB |
Output is correct |
10 |
Correct |
30 ms |
9564 KB |
Output is correct |
11 |
Correct |
30 ms |
9556 KB |
Output is correct |
12 |
Correct |
16 ms |
7000 KB |
Output is correct |
13 |
Correct |
20 ms |
9044 KB |
Output is correct |
14 |
Correct |
19 ms |
9044 KB |
Output is correct |
15 |
Correct |
19 ms |
9048 KB |
Output is correct |
16 |
Correct |
19 ms |
9084 KB |
Output is correct |
17 |
Correct |
17 ms |
9048 KB |
Output is correct |
18 |
Correct |
20 ms |
9048 KB |
Output is correct |
19 |
Correct |
17 ms |
9180 KB |
Output is correct |
20 |
Correct |
17 ms |
9052 KB |
Output is correct |
21 |
Correct |
17 ms |
9052 KB |
Output is correct |
22 |
Correct |
30 ms |
8792 KB |
Output is correct |
23 |
Correct |
27 ms |
8284 KB |
Output is correct |
24 |
Correct |
31 ms |
9552 KB |
Output is correct |
25 |
Correct |
41 ms |
9512 KB |
Output is correct |
26 |
Correct |
32 ms |
9552 KB |
Output is correct |
27 |
Correct |
30 ms |
9472 KB |
Output is correct |
28 |
Correct |
29 ms |
9560 KB |
Output is correct |
29 |
Correct |
29 ms |
9564 KB |
Output is correct |
30 |
Correct |
33 ms |
9556 KB |
Output is correct |
31 |
Correct |
31 ms |
9500 KB |
Output is correct |