답안 #963747

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
963747 2024-04-15T15:22:35 Z EJIC_B_KEDAX Cat Exercise (JOI23_ho_t4) C++17
100 / 100
237 ms 64788 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';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 45672 KB Output is correct
2 Correct 6 ms 45672 KB Output is correct
3 Correct 7 ms 45660 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 6 ms 45676 KB Output is correct
7 Correct 7 ms 45756 KB Output is correct
8 Correct 6 ms 45656 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 6 ms 45656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 45672 KB Output is correct
2 Correct 6 ms 45672 KB Output is correct
3 Correct 7 ms 45660 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 6 ms 45676 KB Output is correct
7 Correct 7 ms 45756 KB Output is correct
8 Correct 6 ms 45656 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 6 ms 45656 KB Output is correct
11 Correct 7 ms 45656 KB Output is correct
12 Correct 6 ms 45764 KB Output is correct
13 Correct 6 ms 45660 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 7 ms 45660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 45672 KB Output is correct
2 Correct 6 ms 45672 KB Output is correct
3 Correct 7 ms 45660 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 6 ms 45676 KB Output is correct
7 Correct 7 ms 45756 KB Output is correct
8 Correct 6 ms 45656 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 6 ms 45656 KB Output is correct
11 Correct 7 ms 45656 KB Output is correct
12 Correct 6 ms 45764 KB Output is correct
13 Correct 6 ms 45660 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 7 ms 45660 KB Output is correct
18 Correct 8 ms 46156 KB Output is correct
19 Correct 10 ms 46172 KB Output is correct
20 Correct 8 ms 46172 KB Output is correct
21 Correct 8 ms 45988 KB Output is correct
22 Correct 9 ms 46172 KB Output is correct
23 Correct 10 ms 46172 KB Output is correct
24 Correct 8 ms 46168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 45672 KB Output is correct
2 Correct 6 ms 45672 KB Output is correct
3 Correct 7 ms 45660 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 6 ms 45676 KB Output is correct
7 Correct 7 ms 45756 KB Output is correct
8 Correct 6 ms 45656 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 6 ms 45656 KB Output is correct
11 Correct 7 ms 45656 KB Output is correct
12 Correct 6 ms 45764 KB Output is correct
13 Correct 6 ms 45660 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 7 ms 45660 KB Output is correct
18 Correct 8 ms 46156 KB Output is correct
19 Correct 10 ms 46172 KB Output is correct
20 Correct 8 ms 46172 KB Output is correct
21 Correct 8 ms 45988 KB Output is correct
22 Correct 9 ms 46172 KB Output is correct
23 Correct 10 ms 46172 KB Output is correct
24 Correct 8 ms 46168 KB Output is correct
25 Correct 6 ms 45656 KB Output is correct
26 Correct 8 ms 46172 KB Output is correct
27 Correct 9 ms 46168 KB Output is correct
28 Correct 8 ms 46172 KB Output is correct
29 Correct 8 ms 46176 KB Output is correct
30 Correct 9 ms 45928 KB Output is correct
31 Correct 9 ms 45920 KB Output is correct
32 Correct 9 ms 45916 KB Output is correct
33 Correct 8 ms 45916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 45672 KB Output is correct
2 Correct 6 ms 45672 KB Output is correct
3 Correct 7 ms 45660 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 6 ms 45676 KB Output is correct
7 Correct 7 ms 45756 KB Output is correct
8 Correct 6 ms 45656 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 6 ms 45656 KB Output is correct
11 Correct 7 ms 45656 KB Output is correct
12 Correct 6 ms 45764 KB Output is correct
13 Correct 6 ms 45660 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 7 ms 45660 KB Output is correct
18 Correct 8 ms 46156 KB Output is correct
19 Correct 10 ms 46172 KB Output is correct
20 Correct 8 ms 46172 KB Output is correct
21 Correct 8 ms 45988 KB Output is correct
22 Correct 9 ms 46172 KB Output is correct
23 Correct 10 ms 46172 KB Output is correct
24 Correct 8 ms 46168 KB Output is correct
25 Correct 103 ms 61352 KB Output is correct
26 Correct 110 ms 64108 KB Output is correct
27 Correct 108 ms 64104 KB Output is correct
28 Correct 183 ms 64788 KB Output is correct
29 Correct 232 ms 61492 KB Output is correct
30 Correct 178 ms 63808 KB Output is correct
31 Correct 175 ms 63132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 45656 KB Output is correct
2 Correct 7 ms 45784 KB Output is correct
3 Correct 160 ms 57556 KB Output is correct
4 Correct 237 ms 57704 KB Output is correct
5 Correct 171 ms 57616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 45672 KB Output is correct
2 Correct 6 ms 45672 KB Output is correct
3 Correct 7 ms 45660 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 6 ms 45676 KB Output is correct
7 Correct 7 ms 45756 KB Output is correct
8 Correct 6 ms 45656 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 6 ms 45656 KB Output is correct
11 Correct 7 ms 45656 KB Output is correct
12 Correct 6 ms 45764 KB Output is correct
13 Correct 6 ms 45660 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 7 ms 45660 KB Output is correct
18 Correct 8 ms 46156 KB Output is correct
19 Correct 10 ms 46172 KB Output is correct
20 Correct 8 ms 46172 KB Output is correct
21 Correct 8 ms 45988 KB Output is correct
22 Correct 9 ms 46172 KB Output is correct
23 Correct 10 ms 46172 KB Output is correct
24 Correct 8 ms 46168 KB Output is correct
25 Correct 6 ms 45656 KB Output is correct
26 Correct 8 ms 46172 KB Output is correct
27 Correct 9 ms 46168 KB Output is correct
28 Correct 8 ms 46172 KB Output is correct
29 Correct 8 ms 46176 KB Output is correct
30 Correct 9 ms 45928 KB Output is correct
31 Correct 9 ms 45920 KB Output is correct
32 Correct 9 ms 45916 KB Output is correct
33 Correct 8 ms 45916 KB Output is correct
34 Correct 103 ms 61352 KB Output is correct
35 Correct 110 ms 64108 KB Output is correct
36 Correct 108 ms 64104 KB Output is correct
37 Correct 183 ms 64788 KB Output is correct
38 Correct 232 ms 61492 KB Output is correct
39 Correct 178 ms 63808 KB Output is correct
40 Correct 175 ms 63132 KB Output is correct
41 Correct 9 ms 45656 KB Output is correct
42 Correct 7 ms 45784 KB Output is correct
43 Correct 160 ms 57556 KB Output is correct
44 Correct 237 ms 57704 KB Output is correct
45 Correct 171 ms 57616 KB Output is correct
46 Correct 121 ms 62860 KB Output is correct
47 Correct 132 ms 61908 KB Output is correct
48 Correct 144 ms 61916 KB Output is correct
49 Correct 123 ms 61776 KB Output is correct
50 Correct 186 ms 57168 KB Output is correct
51 Correct 177 ms 56780 KB Output is correct
52 Correct 169 ms 56912 KB Output is correct
53 Correct 185 ms 57624 KB Output is correct