Submission #990968

# Submission time Handle Problem Language Result Execution time Memory
990968 2024-05-31T23:28:07 Z tincamatei Cat Exercise (JOI23_ho_t4) C++14
100 / 100
171 ms 45728 KB
#include <bits/stdc++.h>

struct DSU {
    std::vector<long long> height;
    std::vector<int> boss;

    explicit DSU(int N) {
        boss.resize(1 + N);
        height.resize(1 + N);
        for (int i = 1; i <= N; i++)
            boss[i] = i;
    }

    int find(int x) {
        if (x == boss[x])
            return x;

        boss[x] = find(boss[x]);
        return boss[x];
    }

    void merge(int a, int b) {
        int sa = find(a);
        int sb = find(b);

        if (sa > sb)
            boss[sb] = sa;
        else
            boss[sa] = sb;
    }
};

struct DistanceCalculator {
    int lg = 0;
    std::vector<std::vector<int>>& graph;
    std::vector<std::vector<int>> parent;

    std::vector<int> dep;

    DistanceCalculator(std::vector<std::vector<int>>& _graph): graph(_graph) {
        int N = (int)graph.size() - 1;
        while ((1 << (lg + 1)) <= N)
            lg++;

        parent.resize(1 + N, std::vector<int>(1 + lg));
        dep.resize(1 + N);

        predfs();
    }

    void predfs(int node = 1, int father = 0) {
        parent[node][0] = father;
        for (int l = 1; l <= lg; l++)
            parent[node][l] = parent[parent[node][l - 1]][l - 1];

        for (auto it: graph[node]) {
            if (it != father) {
                dep[it] = dep[node] + 1;
                predfs(it, node);
            }
        }
    }

    int goup(int node, int x) {
        for (int l = lg; l >= 0; --l)
            if ((1 << l) & x)
                node = parent[node][l];
        return node;
    }

    int lca(int a, int b) {
        if (dep[a] > dep[b])
            a = goup(a, dep[a] - dep[b]);
        else
            b = goup(b, dep[b] - dep[a]);

        if (a == b) return a;

        for (int l = lg; l >= 0; --l)
            if (parent[a][l] != parent[b][l]) {
                a = parent[a][l];
                b = parent[b][l];
            }
        return parent[a][0];
    }

    int dist(int a, int b) {
        int l = lca(a, b);
        return dep[a] + dep[b] - 2 * dep[l];
    }
};

int main() {
    std::cin.tie(NULL);
    std::iostream::sync_with_stdio(false);

    int N;
    std::cin >> N;

    std::vector<int> perm(1 + N);
    
    for (int i = 1; i <= N; i++)
        std::cin >> perm[i];

    std::vector<std::vector<int>> graph(1 + N);

    for (int i = 0; i < N - 1; i++) {
        int a, b;
        std::cin >> a >> b;
        a = perm[a];
        b = perm[b];

        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    DSU dsu(N);
    DistanceCalculator dc(graph);

    //std::cerr << dc.dist(2, 3) << "\n";

    for (int node = 1; node <= N; node++) {
        long long height = 0;
        for (auto it: graph[node]) {
            if (node > it) {
                height = std::max(height, dsu.height[dsu.find(it)] + dc.dist(node, dsu.find(it)));
                dsu.merge(node, it);
            }
        }

        dsu.height[dsu.find(node)] = height;
        //std::cerr << node << " has height " << height << "\n";
    }

    std::cout << dsu.height[dsu.find(N)];

    return 0;
}

# Verdict Execution time Memory 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 0 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
# Verdict Execution time Memory 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 0 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 0 ms 348 KB Output is correct
12 Correct 0 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 0 ms 348 KB Output is correct
16 Correct 0 ms 456 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory 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 0 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 0 ms 348 KB Output is correct
12 Correct 0 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 0 ms 348 KB Output is correct
16 Correct 0 ms 456 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1372 KB Output is correct
21 Correct 2 ms 1624 KB Output is correct
22 Correct 2 ms 1316 KB Output is correct
23 Correct 2 ms 1372 KB Output is correct
24 Correct 2 ms 1232 KB Output is correct
# Verdict Execution time Memory 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 0 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 0 ms 348 KB Output is correct
12 Correct 0 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 0 ms 348 KB Output is correct
16 Correct 0 ms 456 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1372 KB Output is correct
21 Correct 2 ms 1624 KB Output is correct
22 Correct 2 ms 1316 KB Output is correct
23 Correct 2 ms 1372 KB Output is correct
24 Correct 2 ms 1232 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 3 ms 1372 KB Output is correct
27 Correct 2 ms 1316 KB Output is correct
28 Correct 2 ms 1320 KB Output is correct
29 Correct 2 ms 1372 KB Output is correct
30 Correct 2 ms 1116 KB Output is correct
31 Correct 3 ms 1116 KB Output is correct
32 Correct 2 ms 1116 KB Output is correct
33 Correct 2 ms 1112 KB Output is correct
# Verdict Execution time Memory 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 0 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 0 ms 348 KB Output is correct
12 Correct 0 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 0 ms 348 KB Output is correct
16 Correct 0 ms 456 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1372 KB Output is correct
21 Correct 2 ms 1624 KB Output is correct
22 Correct 2 ms 1316 KB Output is correct
23 Correct 2 ms 1372 KB Output is correct
24 Correct 2 ms 1232 KB Output is correct
25 Correct 94 ms 42580 KB Output is correct
26 Correct 102 ms 45652 KB Output is correct
27 Correct 96 ms 45728 KB Output is correct
28 Correct 171 ms 45248 KB Output is correct
29 Correct 165 ms 42576 KB Output is correct
30 Correct 169 ms 44456 KB Output is correct
31 Correct 156 ms 43856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 144 ms 39324 KB Output is correct
4 Correct 147 ms 39248 KB Output is correct
5 Correct 134 ms 39248 KB Output is correct
# Verdict Execution time Memory 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 0 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 0 ms 348 KB Output is correct
12 Correct 0 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 0 ms 348 KB Output is correct
16 Correct 0 ms 456 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1372 KB Output is correct
21 Correct 2 ms 1624 KB Output is correct
22 Correct 2 ms 1316 KB Output is correct
23 Correct 2 ms 1372 KB Output is correct
24 Correct 2 ms 1232 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 3 ms 1372 KB Output is correct
27 Correct 2 ms 1316 KB Output is correct
28 Correct 2 ms 1320 KB Output is correct
29 Correct 2 ms 1372 KB Output is correct
30 Correct 2 ms 1116 KB Output is correct
31 Correct 3 ms 1116 KB Output is correct
32 Correct 2 ms 1116 KB Output is correct
33 Correct 2 ms 1112 KB Output is correct
34 Correct 94 ms 42580 KB Output is correct
35 Correct 102 ms 45652 KB Output is correct
36 Correct 96 ms 45728 KB Output is correct
37 Correct 171 ms 45248 KB Output is correct
38 Correct 165 ms 42576 KB Output is correct
39 Correct 169 ms 44456 KB Output is correct
40 Correct 156 ms 43856 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 144 ms 39324 KB Output is correct
44 Correct 147 ms 39248 KB Output is correct
45 Correct 134 ms 39248 KB Output is correct
46 Correct 118 ms 43740 KB Output is correct
47 Correct 132 ms 43856 KB Output is correct
48 Correct 112 ms 43724 KB Output is correct
49 Correct 112 ms 43732 KB Output is correct
50 Correct 146 ms 39428 KB Output is correct
51 Correct 146 ms 39504 KB Output is correct
52 Correct 140 ms 39504 KB Output is correct
53 Correct 166 ms 39504 KB Output is correct