|
| 1 | +/** |
| 2 | + * @file 5418.cpp |
| 3 | + * @author Macesuted ([email protected]) |
| 4 | + * @date 2025-10-21 |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2025 |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include <bits/stdc++.h> |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +#define endl '\n' |
| 14 | + |
| 15 | +#define maxn 100005 |
| 16 | +#define maxlgn 20 |
| 17 | + |
| 18 | +class STList { |
| 19 | + private: |
| 20 | + int f[maxlgn][maxn], lgLen[maxn]; |
| 21 | + |
| 22 | + public: |
| 23 | + void build(int n, int a[]) { |
| 24 | + for (int i = 2; i <= n; i++) lgLen[i] = lgLen[i >> 1] + 1; |
| 25 | + for (int i = 0; i < n; i++) f[0][i] = a[i]; |
| 26 | + for (int t = 1; t <= lgLen[n]; t++) |
| 27 | + for (int i = 0; i + (1 << t) <= n; i++) f[t][i] = min(f[t - 1][i], f[t - 1][i + (1 << (t - 1))]); |
| 28 | + return; |
| 29 | + } |
| 30 | + int64_t getMin(int l, int r) { |
| 31 | + int t = lgLen[r - l + 1]; |
| 32 | + return min(f[t][l], f[t][r - (1 << t) + 1]); |
| 33 | + } |
| 34 | +} STL; |
| 35 | + |
| 36 | +int a[maxn], dep[maxn], fa[maxn][maxlgn], dfni[maxn]; |
| 37 | +vector<vector<int>> graph, ngraph, rec; |
| 38 | + |
| 39 | +int lca(int x, int y) { |
| 40 | + if (dep[x] < dep[y]) swap(x, y); |
| 41 | + for (int i = maxlgn - 1; ~i; i--) |
| 42 | + if (dep[fa[x][i]] >= dep[y]) x = fa[x][i]; |
| 43 | + if (x == y) return x; |
| 44 | + for (int i = maxlgn - 1; ~i; i--) |
| 45 | + if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; |
| 46 | + return fa[x][0]; |
| 47 | +} |
| 48 | + |
| 49 | +int dfnt = 0; |
| 50 | +void dfs1(int p, int pre = 0) { |
| 51 | + rec[dep[p] = dep[pre] + 1].push_back(p), dfni[p] = ++dfnt, fa[p][0] = pre; |
| 52 | + for (int t = 1; t < maxlgn; t++) fa[p][t] = fa[fa[p][t - 1]][t - 1]; |
| 53 | + for (auto q : graph[p]) |
| 54 | + if (q != pre) dfs1(q, p); |
| 55 | + return; |
| 56 | +} |
| 57 | + |
| 58 | +int64_t dfs2(int p, int tar) { |
| 59 | + if (ngraph[p].empty()) return STL.getMin(0, tar - 1); |
| 60 | + int64_t sum = 0; |
| 61 | + for (auto q : ngraph[p]) sum += dfs2(q, tar); |
| 62 | + return min(sum, STL.getMin(tar - dep[p], tar - 1)); |
| 63 | +} |
| 64 | + |
| 65 | +void solve(void) { |
| 66 | + int n; |
| 67 | + cin >> n; |
| 68 | + |
| 69 | + for (int i = 0; i < n; i++) cin >> a[i]; |
| 70 | + STL.build(n, a); |
| 71 | + |
| 72 | + graph.clear(), graph.resize(n + 1); |
| 73 | + for (int i = 1, x, y; i < n; i++) cin >> x >> y, graph[x].push_back(y), graph[y].push_back(x); |
| 74 | + |
| 75 | + rec.clear(), rec.resize(n + 1); |
| 76 | + dfnt = 0, dfs1(1); |
| 77 | + |
| 78 | + int64_t ans = 0; |
| 79 | + |
| 80 | + ngraph.clear(), ngraph.resize(n + 1); |
| 81 | + for (int d = 1; d <= n; d++) { |
| 82 | + if (rec[d].empty()) continue; |
| 83 | + |
| 84 | + sort(rec[d].begin(), rec[d].end(), [&](int x, int y) -> bool { return dfni[x] < dfni[y]; }); |
| 85 | + |
| 86 | + vector<int> tmp; |
| 87 | + stack<int> S; |
| 88 | + int head = 1; |
| 89 | + for (auto x : rec[d]) { |
| 90 | + int t = lca(head, x); |
| 91 | + while (!S.empty() && dep[S.top()] >= dep[t]) |
| 92 | + tmp.push_back(S.top()), ngraph[S.top()].push_back(head), head = S.top(), S.pop(); |
| 93 | + if (head != t) tmp.push_back(t), ngraph[t].push_back(head), head = t; |
| 94 | + if (x != t) S.push(head), head = x; |
| 95 | + } |
| 96 | + |
| 97 | + while (!S.empty()) tmp.push_back(S.top()), ngraph[S.top()].push_back(head), head = S.top(), S.pop(); |
| 98 | + |
| 99 | + ans += dfs2(1, d); |
| 100 | + |
| 101 | + for (auto x : tmp) ngraph[x].clear(); |
| 102 | + } |
| 103 | + |
| 104 | + cout << ans << endl; |
| 105 | + |
| 106 | + return; |
| 107 | +} |
| 108 | + |
| 109 | +int main() { |
| 110 | + ios::sync_with_stdio(false), cin.tie(nullptr); |
| 111 | + |
| 112 | + int _ = 1; |
| 113 | + cin >> _; |
| 114 | + while (_--) solve(); |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
0 commit comments