Submission #782830

# Submission time Handle Problem Language Result Execution time Memory
782830 2023-07-14T10:06:00 Z thimote75 Cat Exercise (JOI23_ho_t4) C++14
100 / 100
451 ms 72960 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

using idata = vector<int>;
using igrid = vector<idata>;

struct UFD {
    idata parents;

    UFD (int size) {
        parents.resize(size);
        iota(parents.begin(), parents.end(), 0);
    }
    int boss (int x) {
        if (parents[x] != x)
            parents[x] = boss(parents[x]);
        return parents[x];
    }
    void merge (int a, int b) {
        a = boss(a);
        b = boss(b);
        if (a < b) swap (a, b);

        parents[b] = a;
    }
};

const int MAXK = 20;
int N;

idata depth;
idata parents;
igrid parents_2k;

igrid roads;

void dfs (int node, int parent, int _depth) {
    depth     [node]    = _depth;
    parents   [node]    = parent;
    parents_2k[node][0] = parent;

    for (int next : roads[node]) if (next != parent)
        dfs(next, node, _depth + 1);
}
void init_lca () {
    for (int k = 0; k + 1 < MAXK; k ++) {
        for (int i = 0; i < N; i ++) {
            if (parents_2k[i][k] == -1) continue ;

            parents_2k[i][k + 1]
             = parents_2k[parents_2k[i][k]][k];
        }
    }
}
int jump (int node, int k) {
    for (int i = 0; i < MAXK; i ++)
        if ((1 << i) & k)
            node = parents_2k[node][i];
    return node;
}
int lca (int a, int b) {
    if (depth[a] > depth[b]) swap(a, b);

    b = jump(b, depth[b] - depth[a]);

    if (a == b) return a;

    for (int i = MAXK - 1; i >= 0; i --) {
        if (parents_2k[a][i] == parents_2k[b][i]) continue ;

        a = parents_2k[a][i];
        b = parents_2k[b][i];
    }

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

idata height;
idata antiheight;

signed main () {
    cin >> N;

    height.resize(N); antiheight.resize(N);
    for (int i = 0; i < N; i ++) {
        cin >> height[i]; height[i] --;
        antiheight[height[i]] = i;
    }

    roads.resize(N);
    for (int i = 1; i < N; i ++) {
        int a, b;
        cin >> a >> b;
        a --; b --;

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

    depth  .resize(N);
    parents.resize(N);
    parents_2k.resize(N, idata(MAXK, -1));
    dfs(0, -1, 0);

    init_lca();

    idata dp(N);

    UFD ufd(N);

    for (int i = 0; i < N; i ++) {
        int pos = antiheight[i];
        int mdp = 0;

        for (int next : roads[pos]) {
            if (height[next] > i) continue ;

            int local = ufd.boss(height[next]);
            int ldp   = dp [antiheight[local]] + dist(antiheight[local], pos);
            mdp = max(ldp, mdp);
            ufd.merge(i, height[next]);
        }

        dp[pos] = mdp;
    }

    cout << dp[antiheight[N - 1]] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 6 ms 1908 KB Output is correct
19 Correct 5 ms 2004 KB Output is correct
20 Correct 6 ms 2004 KB Output is correct
21 Correct 5 ms 2004 KB Output is correct
22 Correct 5 ms 1912 KB Output is correct
23 Correct 5 ms 2004 KB Output is correct
24 Correct 5 ms 2004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 6 ms 1908 KB Output is correct
19 Correct 5 ms 2004 KB Output is correct
20 Correct 6 ms 2004 KB Output is correct
21 Correct 5 ms 2004 KB Output is correct
22 Correct 5 ms 1912 KB Output is correct
23 Correct 5 ms 2004 KB Output is correct
24 Correct 5 ms 2004 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 5 ms 1876 KB Output is correct
27 Correct 5 ms 1876 KB Output is correct
28 Correct 5 ms 1876 KB Output is correct
29 Correct 5 ms 1876 KB Output is correct
30 Correct 5 ms 1748 KB Output is correct
31 Correct 5 ms 1748 KB Output is correct
32 Correct 5 ms 1748 KB Output is correct
33 Correct 5 ms 1748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 6 ms 1908 KB Output is correct
19 Correct 5 ms 2004 KB Output is correct
20 Correct 6 ms 2004 KB Output is correct
21 Correct 5 ms 2004 KB Output is correct
22 Correct 5 ms 1912 KB Output is correct
23 Correct 5 ms 2004 KB Output is correct
24 Correct 5 ms 2004 KB Output is correct
25 Correct 233 ms 69096 KB Output is correct
26 Correct 274 ms 72904 KB Output is correct
27 Correct 249 ms 72960 KB Output is correct
28 Correct 285 ms 72884 KB Output is correct
29 Correct 312 ms 72956 KB Output is correct
30 Correct 290 ms 72908 KB Output is correct
31 Correct 305 ms 72852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 329 ms 61288 KB Output is correct
4 Correct 350 ms 61232 KB Output is correct
5 Correct 333 ms 61308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 6 ms 1908 KB Output is correct
19 Correct 5 ms 2004 KB Output is correct
20 Correct 6 ms 2004 KB Output is correct
21 Correct 5 ms 2004 KB Output is correct
22 Correct 5 ms 1912 KB Output is correct
23 Correct 5 ms 2004 KB Output is correct
24 Correct 5 ms 2004 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 5 ms 1876 KB Output is correct
27 Correct 5 ms 1876 KB Output is correct
28 Correct 5 ms 1876 KB Output is correct
29 Correct 5 ms 1876 KB Output is correct
30 Correct 5 ms 1748 KB Output is correct
31 Correct 5 ms 1748 KB Output is correct
32 Correct 5 ms 1748 KB Output is correct
33 Correct 5 ms 1748 KB Output is correct
34 Correct 233 ms 69096 KB Output is correct
35 Correct 274 ms 72904 KB Output is correct
36 Correct 249 ms 72960 KB Output is correct
37 Correct 285 ms 72884 KB Output is correct
38 Correct 312 ms 72956 KB Output is correct
39 Correct 290 ms 72908 KB Output is correct
40 Correct 305 ms 72852 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 329 ms 61288 KB Output is correct
44 Correct 350 ms 61232 KB Output is correct
45 Correct 333 ms 61308 KB Output is correct
46 Correct 390 ms 69240 KB Output is correct
47 Correct 451 ms 68508 KB Output is correct
48 Correct 401 ms 69644 KB Output is correct
49 Correct 436 ms 68424 KB Output is correct
50 Correct 384 ms 64608 KB Output is correct
51 Correct 414 ms 64664 KB Output is correct
52 Correct 404 ms 64560 KB Output is correct
53 Correct 419 ms 64612 KB Output is correct