Submission #963744

# Submission time Handle Problem Language Result Execution time Memory
963744 2024-04-15T15:17:32 Z EJIC_B_KEDAX Cat Exercise (JOI23_ho_t4) C++17
100 / 100
223 ms 111076 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 = 19, 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 46 ms 58072 KB Output is correct
2 Correct 9 ms 58196 KB Output is correct
3 Correct 9 ms 58012 KB Output is correct
4 Correct 8 ms 57948 KB Output is correct
5 Correct 8 ms 57944 KB Output is correct
6 Correct 8 ms 57948 KB Output is correct
7 Correct 9 ms 58008 KB Output is correct
8 Correct 9 ms 57944 KB Output is correct
9 Correct 9 ms 57948 KB Output is correct
10 Correct 9 ms 57944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 58072 KB Output is correct
2 Correct 9 ms 58196 KB Output is correct
3 Correct 9 ms 58012 KB Output is correct
4 Correct 8 ms 57948 KB Output is correct
5 Correct 8 ms 57944 KB Output is correct
6 Correct 8 ms 57948 KB Output is correct
7 Correct 9 ms 58008 KB Output is correct
8 Correct 9 ms 57944 KB Output is correct
9 Correct 9 ms 57948 KB Output is correct
10 Correct 9 ms 57944 KB Output is correct
11 Correct 8 ms 58008 KB Output is correct
12 Correct 8 ms 57948 KB Output is correct
13 Correct 8 ms 57948 KB Output is correct
14 Correct 9 ms 57968 KB Output is correct
15 Correct 9 ms 57948 KB Output is correct
16 Correct 8 ms 57948 KB Output is correct
17 Correct 8 ms 58016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 58072 KB Output is correct
2 Correct 9 ms 58196 KB Output is correct
3 Correct 9 ms 58012 KB Output is correct
4 Correct 8 ms 57948 KB Output is correct
5 Correct 8 ms 57944 KB Output is correct
6 Correct 8 ms 57948 KB Output is correct
7 Correct 9 ms 58008 KB Output is correct
8 Correct 9 ms 57944 KB Output is correct
9 Correct 9 ms 57948 KB Output is correct
10 Correct 9 ms 57944 KB Output is correct
11 Correct 8 ms 58008 KB Output is correct
12 Correct 8 ms 57948 KB Output is correct
13 Correct 8 ms 57948 KB Output is correct
14 Correct 9 ms 57968 KB Output is correct
15 Correct 9 ms 57948 KB Output is correct
16 Correct 8 ms 57948 KB Output is correct
17 Correct 8 ms 58016 KB Output is correct
18 Correct 11 ms 58428 KB Output is correct
19 Correct 10 ms 58352 KB Output is correct
20 Correct 10 ms 58348 KB Output is correct
21 Correct 11 ms 58460 KB Output is correct
22 Correct 10 ms 58460 KB Output is correct
23 Correct 10 ms 58456 KB Output is correct
24 Correct 11 ms 58352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 58072 KB Output is correct
2 Correct 9 ms 58196 KB Output is correct
3 Correct 9 ms 58012 KB Output is correct
4 Correct 8 ms 57948 KB Output is correct
5 Correct 8 ms 57944 KB Output is correct
6 Correct 8 ms 57948 KB Output is correct
7 Correct 9 ms 58008 KB Output is correct
8 Correct 9 ms 57944 KB Output is correct
9 Correct 9 ms 57948 KB Output is correct
10 Correct 9 ms 57944 KB Output is correct
11 Correct 8 ms 58008 KB Output is correct
12 Correct 8 ms 57948 KB Output is correct
13 Correct 8 ms 57948 KB Output is correct
14 Correct 9 ms 57968 KB Output is correct
15 Correct 9 ms 57948 KB Output is correct
16 Correct 8 ms 57948 KB Output is correct
17 Correct 8 ms 58016 KB Output is correct
18 Correct 11 ms 58428 KB Output is correct
19 Correct 10 ms 58352 KB Output is correct
20 Correct 10 ms 58348 KB Output is correct
21 Correct 11 ms 58460 KB Output is correct
22 Correct 10 ms 58460 KB Output is correct
23 Correct 10 ms 58456 KB Output is correct
24 Correct 11 ms 58352 KB Output is correct
25 Correct 8 ms 57944 KB Output is correct
26 Correct 10 ms 58616 KB Output is correct
27 Correct 10 ms 58460 KB Output is correct
28 Correct 13 ms 58476 KB Output is correct
29 Correct 12 ms 58460 KB Output is correct
30 Correct 14 ms 58204 KB Output is correct
31 Correct 11 ms 58204 KB Output is correct
32 Correct 12 ms 58200 KB Output is correct
33 Correct 11 ms 58320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 58072 KB Output is correct
2 Correct 9 ms 58196 KB Output is correct
3 Correct 9 ms 58012 KB Output is correct
4 Correct 8 ms 57948 KB Output is correct
5 Correct 8 ms 57944 KB Output is correct
6 Correct 8 ms 57948 KB Output is correct
7 Correct 9 ms 58008 KB Output is correct
8 Correct 9 ms 57944 KB Output is correct
9 Correct 9 ms 57948 KB Output is correct
10 Correct 9 ms 57944 KB Output is correct
11 Correct 8 ms 58008 KB Output is correct
12 Correct 8 ms 57948 KB Output is correct
13 Correct 8 ms 57948 KB Output is correct
14 Correct 9 ms 57968 KB Output is correct
15 Correct 9 ms 57948 KB Output is correct
16 Correct 8 ms 57948 KB Output is correct
17 Correct 8 ms 58016 KB Output is correct
18 Correct 11 ms 58428 KB Output is correct
19 Correct 10 ms 58352 KB Output is correct
20 Correct 10 ms 58348 KB Output is correct
21 Correct 11 ms 58460 KB Output is correct
22 Correct 10 ms 58460 KB Output is correct
23 Correct 10 ms 58456 KB Output is correct
24 Correct 11 ms 58352 KB Output is correct
25 Correct 125 ms 106232 KB Output is correct
26 Correct 109 ms 110832 KB Output is correct
27 Correct 111 ms 111076 KB Output is correct
28 Correct 211 ms 110560 KB Output is correct
29 Correct 204 ms 106268 KB Output is correct
30 Correct 205 ms 109440 KB Output is correct
31 Correct 213 ms 108700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 57948 KB Output is correct
2 Correct 8 ms 57948 KB Output is correct
3 Correct 180 ms 101528 KB Output is correct
4 Correct 167 ms 101444 KB Output is correct
5 Correct 181 ms 101560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 58072 KB Output is correct
2 Correct 9 ms 58196 KB Output is correct
3 Correct 9 ms 58012 KB Output is correct
4 Correct 8 ms 57948 KB Output is correct
5 Correct 8 ms 57944 KB Output is correct
6 Correct 8 ms 57948 KB Output is correct
7 Correct 9 ms 58008 KB Output is correct
8 Correct 9 ms 57944 KB Output is correct
9 Correct 9 ms 57948 KB Output is correct
10 Correct 9 ms 57944 KB Output is correct
11 Correct 8 ms 58008 KB Output is correct
12 Correct 8 ms 57948 KB Output is correct
13 Correct 8 ms 57948 KB Output is correct
14 Correct 9 ms 57968 KB Output is correct
15 Correct 9 ms 57948 KB Output is correct
16 Correct 8 ms 57948 KB Output is correct
17 Correct 8 ms 58016 KB Output is correct
18 Correct 11 ms 58428 KB Output is correct
19 Correct 10 ms 58352 KB Output is correct
20 Correct 10 ms 58348 KB Output is correct
21 Correct 11 ms 58460 KB Output is correct
22 Correct 10 ms 58460 KB Output is correct
23 Correct 10 ms 58456 KB Output is correct
24 Correct 11 ms 58352 KB Output is correct
25 Correct 8 ms 57944 KB Output is correct
26 Correct 10 ms 58616 KB Output is correct
27 Correct 10 ms 58460 KB Output is correct
28 Correct 13 ms 58476 KB Output is correct
29 Correct 12 ms 58460 KB Output is correct
30 Correct 14 ms 58204 KB Output is correct
31 Correct 11 ms 58204 KB Output is correct
32 Correct 12 ms 58200 KB Output is correct
33 Correct 11 ms 58320 KB Output is correct
34 Correct 125 ms 106232 KB Output is correct
35 Correct 109 ms 110832 KB Output is correct
36 Correct 111 ms 111076 KB Output is correct
37 Correct 211 ms 110560 KB Output is correct
38 Correct 204 ms 106268 KB Output is correct
39 Correct 205 ms 109440 KB Output is correct
40 Correct 213 ms 108700 KB Output is correct
41 Correct 8 ms 57948 KB Output is correct
42 Correct 8 ms 57948 KB Output is correct
43 Correct 180 ms 101528 KB Output is correct
44 Correct 167 ms 101444 KB Output is correct
45 Correct 181 ms 101560 KB Output is correct
46 Correct 128 ms 108260 KB Output is correct
47 Correct 125 ms 108680 KB Output is correct
48 Correct 131 ms 108740 KB Output is correct
49 Correct 131 ms 108216 KB Output is correct
50 Correct 223 ms 102456 KB Output is correct
51 Correct 201 ms 101876 KB Output is correct
52 Correct 213 ms 102416 KB Output is correct
53 Correct 210 ms 101788 KB Output is correct