답안 #937573

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
937573 2024-03-04T08:59:36 Z zwezdinv Cat Exercise (JOI23_ho_t4) C++17
100 / 100
157 ms 49076 KB
#include <bits/stdc++.h>

struct DSU {
        int n;
        std::vector<int> p, mx;

        DSU() = default;
        DSU(int n) : n(n), p(n, 0) {
                std::iota(p.begin(), p.end(), 0);
                mx = p;
        }

        int get(int u) {
                while (u != p[u]) u = p[u] = p[p[u]];
                return u;
        }

        void unite(int u, int v) {
                mx[get(u)] = std::max(mx[get(u)], mx[get(v)]);
                p[get(v)] = get(u);
        }

        int max(int u) {
                return mx[get(u)];
        }
};

struct segtree {
        int n;
        std::vector<std::pair<int, int>> tr;

        segtree() = default;
        segtree(int n) : n(n), tr(2 * n, {1e9, 1e9}) {}
        segtree(std::vector<std::pair<int, int>> a) {
                n = a.size();
                tr.resize(2 * n);
                for (int i = 0; i < n; ++i) {
                        tr[i + n] = a[i];
                }
                for (int i = n - 1; i; --i) {
                        tr[i] = std::min(tr[i << 1], tr[i << 1 | 1]);
                }
        }

        std::pair<int, int> get(int l, int r) {
                std::pair<int, int> res = {1e9, 1e9};
                for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
                        if (l & 1) res = std::min(res, tr[l++]);
                        if (~r & 1) res = std::min(res, tr[r--]);
                }
                return res;
        }
};

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

        int n;
        std::cin >> n;
        std::vector<int> p(n);
        for (auto& i : p) std::cin >> i, --i;
        std::vector<std::vector<int>> g(n);
        for (int i = 0; i < n - 1; ++i) {
                int u, v;
                std::cin >> u >> v;
                --u, --v;
                u = p[u];
                v = p[v];
                g[u].push_back(v);
                g[v].push_back(u);
        }
        std::vector<int> depth(n), pos(n);
        std::vector<std::pair<int, int>> ss;
        auto dfs = [&](auto dfs, int u, int p) -> void {
                ss.emplace_back(depth[u], u);
                pos[u] = ss.size() - 1;
                for (auto to : g[u]) {
                        if (to != p) {
                                depth[to] = depth[u] + 1;
                                dfs(dfs, to, u);
                                ss.emplace_back(depth[u], u);
                        }
                }
        };
        dfs(dfs, 0, -1);
        segtree tr(ss);
        auto lca = [&](int u, int v) -> int {
                u = pos[u];
                v = pos[v];
                if (u > v) std::swap(u, v);
                return tr.get(u, v).second;
        };
        auto dist = [&](int u, int v) -> int {
                return depth[u] + depth[v] - 2 * depth[lca(u, v)];
        };
        std::vector<long long> dp(n);
        DSU dsu(n);
        for (int i = 0; i < n; ++i) {
                for (auto j : g[i]) {
                        if (j > i) continue;
                        dp[i] = std::max(dp[i], dp[dsu.max(j)] + dist(i, dsu.max(j)));
                        dsu.unite(i, j);
                }
        }
        std::cout << dp[n - 1];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 452 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1624 KB Output is correct
20 Correct 2 ms 1624 KB Output is correct
21 Correct 3 ms 1492 KB Output is correct
22 Correct 3 ms 1628 KB Output is correct
23 Correct 2 ms 1368 KB Output is correct
24 Correct 2 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 452 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1624 KB Output is correct
20 Correct 2 ms 1624 KB Output is correct
21 Correct 3 ms 1492 KB Output is correct
22 Correct 3 ms 1628 KB Output is correct
23 Correct 2 ms 1368 KB Output is correct
24 Correct 2 ms 1628 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 2 ms 1372 KB Output is correct
27 Correct 2 ms 1372 KB Output is correct
28 Correct 3 ms 1628 KB Output is correct
29 Correct 2 ms 1372 KB Output is correct
30 Correct 3 ms 1116 KB Output is correct
31 Correct 2 ms 1188 KB Output is correct
32 Correct 3 ms 1112 KB Output is correct
33 Correct 3 ms 1220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 452 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1624 KB Output is correct
20 Correct 2 ms 1624 KB Output is correct
21 Correct 3 ms 1492 KB Output is correct
22 Correct 3 ms 1628 KB Output is correct
23 Correct 2 ms 1368 KB Output is correct
24 Correct 2 ms 1628 KB Output is correct
25 Correct 90 ms 39464 KB Output is correct
26 Correct 86 ms 48764 KB Output is correct
27 Correct 86 ms 49076 KB Output is correct
28 Correct 107 ms 47808 KB Output is correct
29 Correct 112 ms 39616 KB Output is correct
30 Correct 103 ms 45544 KB Output is correct
31 Correct 116 ms 44420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 115 ms 29864 KB Output is correct
4 Correct 108 ms 29888 KB Output is correct
5 Correct 105 ms 29884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 452 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1624 KB Output is correct
20 Correct 2 ms 1624 KB Output is correct
21 Correct 3 ms 1492 KB Output is correct
22 Correct 3 ms 1628 KB Output is correct
23 Correct 2 ms 1368 KB Output is correct
24 Correct 2 ms 1628 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 2 ms 1372 KB Output is correct
27 Correct 2 ms 1372 KB Output is correct
28 Correct 3 ms 1628 KB Output is correct
29 Correct 2 ms 1372 KB Output is correct
30 Correct 3 ms 1116 KB Output is correct
31 Correct 2 ms 1188 KB Output is correct
32 Correct 3 ms 1112 KB Output is correct
33 Correct 3 ms 1220 KB Output is correct
34 Correct 90 ms 39464 KB Output is correct
35 Correct 86 ms 48764 KB Output is correct
36 Correct 86 ms 49076 KB Output is correct
37 Correct 107 ms 47808 KB Output is correct
38 Correct 112 ms 39616 KB Output is correct
39 Correct 103 ms 45544 KB Output is correct
40 Correct 116 ms 44420 KB Output is correct
41 Correct 0 ms 600 KB Output is correct
42 Correct 1 ms 460 KB Output is correct
43 Correct 115 ms 29864 KB Output is correct
44 Correct 108 ms 29888 KB Output is correct
45 Correct 105 ms 29884 KB Output is correct
46 Correct 109 ms 43452 KB Output is correct
47 Correct 103 ms 43264 KB Output is correct
48 Correct 110 ms 43972 KB Output is correct
49 Correct 97 ms 43200 KB Output is correct
50 Correct 138 ms 30348 KB Output is correct
51 Correct 133 ms 30456 KB Output is correct
52 Correct 157 ms 30660 KB Output is correct
53 Correct 129 ms 30820 KB Output is correct