답안 #963748

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
963748 2024-04-15T15:23:01 Z EJIC_B_KEDAX Cat Exercise (JOI23_ho_t4) C++17
100 / 100
201 ms 69968 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 6 ms 45664 KB Output is correct
2 Correct 7 ms 45796 KB Output is correct
3 Correct 6 ms 45656 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 7 ms 45660 KB Output is correct
7 Correct 7 ms 45656 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 7 ms 45912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 45664 KB Output is correct
2 Correct 7 ms 45796 KB Output is correct
3 Correct 6 ms 45656 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 7 ms 45660 KB Output is correct
7 Correct 7 ms 45656 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 7 ms 45912 KB Output is correct
11 Correct 7 ms 45708 KB Output is correct
12 Correct 6 ms 45660 KB Output is correct
13 Correct 6 ms 45660 KB Output is correct
14 Correct 6 ms 43728 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 6 ms 45664 KB Output is correct
2 Correct 7 ms 45796 KB Output is correct
3 Correct 6 ms 45656 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 7 ms 45660 KB Output is correct
7 Correct 7 ms 45656 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 7 ms 45912 KB Output is correct
11 Correct 7 ms 45708 KB Output is correct
12 Correct 6 ms 45660 KB Output is correct
13 Correct 6 ms 45660 KB Output is correct
14 Correct 6 ms 43728 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 44180 KB Output is correct
19 Correct 9 ms 46168 KB Output is correct
20 Correct 8 ms 46172 KB Output is correct
21 Correct 9 ms 44124 KB Output is correct
22 Correct 8 ms 44124 KB Output is correct
23 Correct 8 ms 44148 KB Output is correct
24 Correct 8 ms 46216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 45664 KB Output is correct
2 Correct 7 ms 45796 KB Output is correct
3 Correct 6 ms 45656 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 7 ms 45660 KB Output is correct
7 Correct 7 ms 45656 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 7 ms 45912 KB Output is correct
11 Correct 7 ms 45708 KB Output is correct
12 Correct 6 ms 45660 KB Output is correct
13 Correct 6 ms 45660 KB Output is correct
14 Correct 6 ms 43728 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 44180 KB Output is correct
19 Correct 9 ms 46168 KB Output is correct
20 Correct 8 ms 46172 KB Output is correct
21 Correct 9 ms 44124 KB Output is correct
22 Correct 8 ms 44124 KB Output is correct
23 Correct 8 ms 44148 KB Output is correct
24 Correct 8 ms 46216 KB Output is correct
25 Correct 6 ms 43608 KB Output is correct
26 Correct 8 ms 46172 KB Output is correct
27 Correct 10 ms 46168 KB Output is correct
28 Correct 8 ms 44124 KB Output is correct
29 Correct 9 ms 46240 KB Output is correct
30 Correct 9 ms 43864 KB Output is correct
31 Correct 8 ms 43868 KB Output is correct
32 Correct 9 ms 45916 KB Output is correct
33 Correct 8 ms 46072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 45664 KB Output is correct
2 Correct 7 ms 45796 KB Output is correct
3 Correct 6 ms 45656 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 7 ms 45660 KB Output is correct
7 Correct 7 ms 45656 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 7 ms 45912 KB Output is correct
11 Correct 7 ms 45708 KB Output is correct
12 Correct 6 ms 45660 KB Output is correct
13 Correct 6 ms 45660 KB Output is correct
14 Correct 6 ms 43728 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 44180 KB Output is correct
19 Correct 9 ms 46168 KB Output is correct
20 Correct 8 ms 46172 KB Output is correct
21 Correct 9 ms 44124 KB Output is correct
22 Correct 8 ms 44124 KB Output is correct
23 Correct 8 ms 44148 KB Output is correct
24 Correct 8 ms 46216 KB Output is correct
25 Correct 99 ms 63548 KB Output is correct
26 Correct 108 ms 69212 KB Output is correct
27 Correct 103 ms 69968 KB Output is correct
28 Correct 192 ms 68624 KB Output is correct
29 Correct 171 ms 63572 KB Output is correct
30 Correct 177 ms 67628 KB Output is correct
31 Correct 198 ms 65924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 45708 KB Output is correct
2 Correct 6 ms 43664 KB Output is correct
3 Correct 157 ms 56660 KB Output is correct
4 Correct 173 ms 56584 KB Output is correct
5 Correct 176 ms 56800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 45664 KB Output is correct
2 Correct 7 ms 45796 KB Output is correct
3 Correct 6 ms 45656 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 6 ms 45660 KB Output is correct
6 Correct 7 ms 45660 KB Output is correct
7 Correct 7 ms 45656 KB Output is correct
8 Correct 6 ms 45660 KB Output is correct
9 Correct 6 ms 45660 KB Output is correct
10 Correct 7 ms 45912 KB Output is correct
11 Correct 7 ms 45708 KB Output is correct
12 Correct 6 ms 45660 KB Output is correct
13 Correct 6 ms 45660 KB Output is correct
14 Correct 6 ms 43728 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 44180 KB Output is correct
19 Correct 9 ms 46168 KB Output is correct
20 Correct 8 ms 46172 KB Output is correct
21 Correct 9 ms 44124 KB Output is correct
22 Correct 8 ms 44124 KB Output is correct
23 Correct 8 ms 44148 KB Output is correct
24 Correct 8 ms 46216 KB Output is correct
25 Correct 6 ms 43608 KB Output is correct
26 Correct 8 ms 46172 KB Output is correct
27 Correct 10 ms 46168 KB Output is correct
28 Correct 8 ms 44124 KB Output is correct
29 Correct 9 ms 46240 KB Output is correct
30 Correct 9 ms 43864 KB Output is correct
31 Correct 8 ms 43868 KB Output is correct
32 Correct 9 ms 45916 KB Output is correct
33 Correct 8 ms 46072 KB Output is correct
34 Correct 99 ms 63548 KB Output is correct
35 Correct 108 ms 69212 KB Output is correct
36 Correct 103 ms 69968 KB Output is correct
37 Correct 192 ms 68624 KB Output is correct
38 Correct 171 ms 63572 KB Output is correct
39 Correct 177 ms 67628 KB Output is correct
40 Correct 198 ms 65924 KB Output is correct
41 Correct 6 ms 45708 KB Output is correct
42 Correct 6 ms 43664 KB Output is correct
43 Correct 157 ms 56660 KB Output is correct
44 Correct 173 ms 56584 KB Output is correct
45 Correct 176 ms 56800 KB Output is correct
46 Correct 129 ms 66532 KB Output is correct
47 Correct 133 ms 66440 KB Output is correct
48 Correct 135 ms 66308 KB Output is correct
49 Correct 134 ms 66388 KB Output is correct
50 Correct 181 ms 57880 KB Output is correct
51 Correct 201 ms 57780 KB Output is correct
52 Correct 174 ms 57404 KB Output is correct
53 Correct 196 ms 56912 KB Output is correct