QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#267690#5418. Color the Treejrjyy#RE 1ms3420kbC++203.5kb2023-11-27 16:30:202023-11-27 16:30:21

Judging History

你现在查看的是最新测评结果

  • [2023-11-27 16:30:21]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3420kb
  • [2023-11-27 16:30:20]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

template <typename T, typename Cmp = std::less<T>>
struct RMQ {
    const Cmp cmp;
    int n;
    std::vector<std::vector<T>> a;
    RMQ() = default;
    RMQ(const std::vector<T> &v) : cmp{} {
        init(v);
    }
    RMQ(const std::vector<T> &v, const Cmp &cmp_) : cmp{cmp_} {
        init(v);
    }
    void init(const std::vector<T> &ini) {
        n = int(ini.size());
        int lg = std::__lg(n);
        a.assign(lg + 1, std::vector<T>(n));
        a[0] = ini;
        for (int i = 0; i < lg; ++i) {
            for (int j = 0; j <= n - (2 << i); ++j) {
                a[i + 1][j] = std::min(a[i][j], a[i][j + (1 << i)], cmp);
            }
        }
    }
    T operator()(int l, int r) const {
        int k = std::__lg(r - l);
        return std::min(a[k][l], a[k][r - (1 << k)], cmp);
    }
};

void solve() {
    int n;
    std::cin >> n;

    std::vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i];
    }

    std::vector<std::vector<int>> adj(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        std::cin >> u >> v;
        --u, --v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    std::vector<int> fa(n, -1), dep(n), dfn(n);
    int cur = 0;
    std::vector<std::vector<int>> pos(n);
    auto dfs = [&](auto self, int u) -> void {
        dfn[u] = cur++;
        pos[dep[u]].push_back(u);
        for (auto v : adj[u]) {
            if (v == fa[u]) {
                continue;
            }
            fa[v] = u;
            dep[v] = dep[u] + 1;
            self(self, v);
        }
    };
    dfs(dfs, 0);

    fa[0] = 0;
    auto cmp = [&](int x, int y) {
        return dfn[x] < dfn[y];
    };
    RMQ<int, decltype(cmp)> frmq(fa, cmp);
    auto lca = [&](int u, int v) {
        if (u == v) {
            return u;
        }
        u = dfn[u];
        v = dfn[v];
        if (!cmp(u, v)) {
            std::swap(u, v);
        }
        return frmq(u + 1, v + 1);
    };

    RMQ rmq(a);

    i64 ans = 0;
    std::vector<std::vector<int>> e(n);
    std::vector<i64> w(n), f(n);
    for (int x = 0; x < n; ++x) {
        auto s = pos[x];
        if (s.empty()) {
            continue;
        }
        s.push_back(0);
        std::sort(s.begin(), s.end(), cmp);
        s.erase(std::unique(s.begin(), s.end()), s.end());
        for (int i = int(s.size()) - 1; i > 0; --i) {
            s.push_back(lca(s[i], s[i - 1]));
        }
        std::sort(s.begin(), s.end(), cmp);
        s.erase(std::unique(s.begin(), s.end()), s.end());

        for (int i = 1; i < int(s.size()); ++i) {
            int u = s[i], f = lca(s[i], s[i - 1]);
            e[f].push_back(u);
            w[u] = rmq(x - dep[u], x - dep[f]);
        }
        w[0] = a[x];

        auto dfs = [&](auto self, int u) -> void {
            for (auto v : e[u]) {
                self(self, v);
                f[u] += f[v];
            }
            if (dep[u] == x) {
                f[u] = 1e18;
            }
            f[u] = std::min(f[u], w[u]);
        };
        dfs(dfs, 0);
        ans += f[0];

        for (auto u : s) {
            e[u].clear();
            f[u] = 0;
        }
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3420kb

input:

3
4
10 15 40 1
1 2
2 3
2 4
5
10 5 1 100 1000
1 2
2 3
2 4
4 5
4
1000 200 10 8
1 2
2 3
3 4

output:

35
17
1218

result:

ok 3 number(s): "35 17 1218"

Test #2:

score: -100
Runtime Error

input:

3000
54
43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44
1 2
1 3
2 4
3 5
4 6
2 7
4 8
1 9
3 10
1 11
8 12
2 13
9 14
1 15
1 16
15 17
15 18
7 19
1 20
19 21
13 22
10 23
17 24
9 25
9 26
24 27
8 28...

output:


result: