Submission #963745

# Submission time Handle Problem Language Result Execution time Memory
963745 2024-04-15T15:20:30 Z EJIC_B_KEDAX Cat Exercise (JOI23_ho_t4) C++17
100 / 100
218 ms 67416 KB
#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>

#ifndef LOCAL
    // #pragma GCC optimize("O3")
    // #pragma GCC optimize("Ofast")
    // #pragma GCC optimize("unroll-loops")
    // #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

mt19937 mt(123);

void solve();
void init();

int32_t main() {
#ifndef LOCAL
    cin.tie(nullptr)->sync_with_stdio(false);
#endif
    cout << fixed << setprecision(30);
    init();
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}

void init() {}

const int LG = 18, N = 1 << LG;
int p[N], dsu[N], tin[N], tout[N], timer;
ll dp[N];
pair<int, int> binup[LG][N];
vector<int> g[N];

int get(int x) {
    return x == dsu[x] ? x : dsu[x] = get(dsu[x]);
}

void merge(int x, int y) {
    x = get(x);
    y = get(y);
    if (x < y) {
        swap(x, y);
    }
    dsu[y] = x;
}

void dfs(int s, int p = -1) {
    tin[s] = timer++;
    if (p == -1) {
        binup[0][s] = {0, s};
    } else {
        binup[0][s] = {1, p};
    }
    for (int i : g[s]) {
        if (i != p) {
            dfs(i, s);
        }
    }
    tout[s] = timer++;
}

bool check_pr(int i, int p) {
    return tin[p] <= tin[i] && tin[i] <= tout[p];
}

int get_dist(int u, int v) {
    int nw = u, res = 0;
    for (int l = LG - 1; l >= 0; l--) {
        if (!check_pr(v, binup[l][nw].y)) {
            res += binup[l][nw].x;
            nw = binup[l][nw].y;
        }
    }
    if (!check_pr(v, nw)) {
        res++;
    }
    nw = v;
    for (int l = LG - 1; l >= 0; l--) {
        if (!check_pr(u, binup[l][nw].y)) {
            res += binup[l][nw].x;
            nw = binup[l][nw].y;
        }
    }
    if (!check_pr(u, nw)) {
        res++;
    }
    return res;
}

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> p[i]; p[i]--;
        dsu[i] = i;
        dp[i] = 0;
    }
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v; u--; v--;
        u = p[u];
        v = p[v];
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(0);
    for (int l = 1; l < LG; l++) {
        for (int i = 0; i < n; i++) {
            binup[l][i] = {binup[l - 1][i].x + binup[l - 1][binup[l - 1][i].y].x, binup[l - 1][binup[l - 1][i].y].y};
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j : g[i]) {
            if (j < i) {
                dp[i] = max(dp[i], dp[get(j)] + get_dist(i, get(j)));
            }
        }
        for (int j : g[i]) {
            if (j < i) {
                merge(i, j);
            }
        }
    }
    cout << dp[n - 1] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 45660 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 6 ms 45776 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45800 KB Output is correct
6 Correct 6 ms 45660 KB Output is correct
7 Correct 8 ms 45748 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45784 KB Output is correct
10 Correct 7 ms 45712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 45660 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 6 ms 45776 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45800 KB Output is correct
6 Correct 6 ms 45660 KB Output is correct
7 Correct 8 ms 45748 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45784 KB Output is correct
10 Correct 7 ms 45712 KB Output is correct
11 Correct 7 ms 45660 KB Output is correct
12 Correct 6 ms 45660 KB Output is correct
13 Correct 6 ms 45816 KB Output is correct
14 Correct 7 ms 45656 KB Output is correct
15 Correct 6 ms 45660 KB Output is correct
16 Correct 6 ms 45660 KB Output is correct
17 Correct 6 ms 45660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 45660 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 6 ms 45776 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45800 KB Output is correct
6 Correct 6 ms 45660 KB Output is correct
7 Correct 8 ms 45748 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45784 KB Output is correct
10 Correct 7 ms 45712 KB Output is correct
11 Correct 7 ms 45660 KB Output is correct
12 Correct 6 ms 45660 KB Output is correct
13 Correct 6 ms 45816 KB Output is correct
14 Correct 7 ms 45656 KB Output is correct
15 Correct 6 ms 45660 KB Output is correct
16 Correct 6 ms 45660 KB Output is correct
17 Correct 6 ms 45660 KB Output is correct
18 Correct 8 ms 46168 KB Output is correct
19 Correct 9 ms 46248 KB Output is correct
20 Correct 8 ms 46336 KB Output is correct
21 Correct 8 ms 46172 KB Output is correct
22 Correct 8 ms 46212 KB Output is correct
23 Correct 8 ms 46164 KB Output is correct
24 Correct 9 ms 46172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 45660 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 6 ms 45776 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45800 KB Output is correct
6 Correct 6 ms 45660 KB Output is correct
7 Correct 8 ms 45748 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45784 KB Output is correct
10 Correct 7 ms 45712 KB Output is correct
11 Correct 7 ms 45660 KB Output is correct
12 Correct 6 ms 45660 KB Output is correct
13 Correct 6 ms 45816 KB Output is correct
14 Correct 7 ms 45656 KB Output is correct
15 Correct 6 ms 45660 KB Output is correct
16 Correct 6 ms 45660 KB Output is correct
17 Correct 6 ms 45660 KB Output is correct
18 Correct 8 ms 46168 KB Output is correct
19 Correct 9 ms 46248 KB Output is correct
20 Correct 8 ms 46336 KB Output is correct
21 Correct 8 ms 46172 KB Output is correct
22 Correct 8 ms 46212 KB Output is correct
23 Correct 8 ms 46164 KB Output is correct
24 Correct 9 ms 46172 KB Output is correct
25 Correct 6 ms 45660 KB Output is correct
26 Correct 9 ms 46168 KB Output is correct
27 Correct 8 ms 46172 KB Output is correct
28 Correct 10 ms 46232 KB Output is correct
29 Correct 10 ms 44124 KB Output is correct
30 Correct 9 ms 43868 KB Output is correct
31 Correct 9 ms 46028 KB Output is correct
32 Correct 8 ms 43868 KB Output is correct
33 Correct 10 ms 45916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 45660 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 6 ms 45776 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45800 KB Output is correct
6 Correct 6 ms 45660 KB Output is correct
7 Correct 8 ms 45748 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45784 KB Output is correct
10 Correct 7 ms 45712 KB Output is correct
11 Correct 7 ms 45660 KB Output is correct
12 Correct 6 ms 45660 KB Output is correct
13 Correct 6 ms 45816 KB Output is correct
14 Correct 7 ms 45656 KB Output is correct
15 Correct 6 ms 45660 KB Output is correct
16 Correct 6 ms 45660 KB Output is correct
17 Correct 6 ms 45660 KB Output is correct
18 Correct 8 ms 46168 KB Output is correct
19 Correct 9 ms 46248 KB Output is correct
20 Correct 8 ms 46336 KB Output is correct
21 Correct 8 ms 46172 KB Output is correct
22 Correct 8 ms 46212 KB Output is correct
23 Correct 8 ms 46164 KB Output is correct
24 Correct 9 ms 46172 KB Output is correct
25 Correct 104 ms 62588 KB Output is correct
26 Correct 100 ms 66776 KB Output is correct
27 Correct 98 ms 67416 KB Output is correct
28 Correct 174 ms 66432 KB Output is correct
29 Correct 218 ms 62848 KB Output is correct
30 Correct 166 ms 65620 KB Output is correct
31 Correct 165 ms 64440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 43612 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 171 ms 58056 KB Output is correct
4 Correct 158 ms 57932 KB Output is correct
5 Correct 161 ms 58252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 45660 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 6 ms 45776 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45800 KB Output is correct
6 Correct 6 ms 45660 KB Output is correct
7 Correct 8 ms 45748 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45784 KB Output is correct
10 Correct 7 ms 45712 KB Output is correct
11 Correct 7 ms 45660 KB Output is correct
12 Correct 6 ms 45660 KB Output is correct
13 Correct 6 ms 45816 KB Output is correct
14 Correct 7 ms 45656 KB Output is correct
15 Correct 6 ms 45660 KB Output is correct
16 Correct 6 ms 45660 KB Output is correct
17 Correct 6 ms 45660 KB Output is correct
18 Correct 8 ms 46168 KB Output is correct
19 Correct 9 ms 46248 KB Output is correct
20 Correct 8 ms 46336 KB Output is correct
21 Correct 8 ms 46172 KB Output is correct
22 Correct 8 ms 46212 KB Output is correct
23 Correct 8 ms 46164 KB Output is correct
24 Correct 9 ms 46172 KB Output is correct
25 Correct 6 ms 45660 KB Output is correct
26 Correct 9 ms 46168 KB Output is correct
27 Correct 8 ms 46172 KB Output is correct
28 Correct 10 ms 46232 KB Output is correct
29 Correct 10 ms 44124 KB Output is correct
30 Correct 9 ms 43868 KB Output is correct
31 Correct 9 ms 46028 KB Output is correct
32 Correct 8 ms 43868 KB Output is correct
33 Correct 10 ms 45916 KB Output is correct
34 Correct 104 ms 62588 KB Output is correct
35 Correct 100 ms 66776 KB Output is correct
36 Correct 98 ms 67416 KB Output is correct
37 Correct 174 ms 66432 KB Output is correct
38 Correct 218 ms 62848 KB Output is correct
39 Correct 166 ms 65620 KB Output is correct
40 Correct 165 ms 64440 KB Output is correct
41 Correct 6 ms 43612 KB Output is correct
42 Correct 6 ms 45660 KB Output is correct
43 Correct 171 ms 58056 KB Output is correct
44 Correct 158 ms 57932 KB Output is correct
45 Correct 161 ms 58252 KB Output is correct
46 Correct 145 ms 64144 KB Output is correct
47 Correct 124 ms 64004 KB Output is correct
48 Correct 118 ms 64592 KB Output is correct
49 Correct 138 ms 64076 KB Output is correct
50 Correct 198 ms 58160 KB Output is correct
51 Correct 167 ms 57684 KB Output is correct
52 Correct 188 ms 58068 KB Output is correct
53 Correct 181 ms 58108 KB Output is correct