답안 #890979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
890979 2023-12-22T05:47:27 Z juliany2 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
207 ms 43120 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()

const int N = 2e5 + 7, L = 19;
int n;
int p[N];
vector<int> adj[N];
int lift[N][L], dep[N], e[N];
ll dp[N];

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

void dfs(int v = 1, int p = 0, int d = 0) {
    dep[v] = d;
    lift[v][0] = p;
    for (int i = 1; i < L; i++)
        lift[v][i] = lift[lift[v][i - 1]][i - 1];

    for (int u : adj[v])
        if (u != p)
            dfs(u, v, d + 1);
}

int lca(int u, int v) {
    if (dep[u] > dep[v])
        swap(u, v);

    for (int i = L - 1; ~i; --i)
        if (dep[v] - (1 << i) >= dep[u])
            v = lift[v][i];

    if (u == v)
        return u;

    for (int i = L - 1; ~i; --i)
        if (lift[v][i] != lift[u][i])
            v = lift[v][i], u = lift[u][i];
    return lift[u][0];
}

int dist(int u, int v) {
    return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}

int main() {
    cin.tie(0)->sync_with_stdio(false);

    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> p[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;

        u = p[u], v = p[v];
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs();

    iota(e + 1, e + n + 1, 1);

    for (int i = 1; i <= n; i++) {
        for (int u : adj[i]) {
            u = get(u);
            if (u < i) {
                dp[i] = max(dp[i], dp[u] + dist(i, u));
                e[u] = i;
            }
        }
    }

    cout << dp[n] << '\n';

    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10720 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10456 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10720 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10456 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10680 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10584 KB Output is correct
15 Correct 2 ms 10720 KB Output is correct
16 Correct 2 ms 10720 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10720 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10456 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10680 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10584 KB Output is correct
15 Correct 2 ms 10720 KB Output is correct
16 Correct 2 ms 10720 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 4 ms 11100 KB Output is correct
19 Correct 6 ms 11100 KB Output is correct
20 Correct 4 ms 11100 KB Output is correct
21 Correct 4 ms 11100 KB Output is correct
22 Correct 5 ms 11052 KB Output is correct
23 Correct 4 ms 11096 KB Output is correct
24 Correct 4 ms 11100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10720 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10456 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10680 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10584 KB Output is correct
15 Correct 2 ms 10720 KB Output is correct
16 Correct 2 ms 10720 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 4 ms 11100 KB Output is correct
19 Correct 6 ms 11100 KB Output is correct
20 Correct 4 ms 11100 KB Output is correct
21 Correct 4 ms 11100 KB Output is correct
22 Correct 5 ms 11052 KB Output is correct
23 Correct 4 ms 11096 KB Output is correct
24 Correct 4 ms 11100 KB Output is correct
25 Correct 2 ms 10584 KB Output is correct
26 Correct 5 ms 10988 KB Output is correct
27 Correct 4 ms 11352 KB Output is correct
28 Correct 4 ms 11100 KB Output is correct
29 Correct 4 ms 11100 KB Output is correct
30 Correct 4 ms 10788 KB Output is correct
31 Correct 4 ms 10844 KB Output is correct
32 Correct 6 ms 10844 KB Output is correct
33 Correct 4 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10720 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10456 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10680 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10584 KB Output is correct
15 Correct 2 ms 10720 KB Output is correct
16 Correct 2 ms 10720 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 4 ms 11100 KB Output is correct
19 Correct 6 ms 11100 KB Output is correct
20 Correct 4 ms 11100 KB Output is correct
21 Correct 4 ms 11100 KB Output is correct
22 Correct 5 ms 11052 KB Output is correct
23 Correct 4 ms 11096 KB Output is correct
24 Correct 4 ms 11100 KB Output is correct
25 Correct 91 ms 36844 KB Output is correct
26 Correct 109 ms 43120 KB Output is correct
27 Correct 103 ms 43052 KB Output is correct
28 Correct 185 ms 42760 KB Output is correct
29 Correct 165 ms 38740 KB Output is correct
30 Correct 174 ms 41556 KB Output is correct
31 Correct 174 ms 40792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 146 ms 32036 KB Output is correct
4 Correct 162 ms 32212 KB Output is correct
5 Correct 134 ms 32048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10720 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10456 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10680 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10584 KB Output is correct
15 Correct 2 ms 10720 KB Output is correct
16 Correct 2 ms 10720 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 4 ms 11100 KB Output is correct
19 Correct 6 ms 11100 KB Output is correct
20 Correct 4 ms 11100 KB Output is correct
21 Correct 4 ms 11100 KB Output is correct
22 Correct 5 ms 11052 KB Output is correct
23 Correct 4 ms 11096 KB Output is correct
24 Correct 4 ms 11100 KB Output is correct
25 Correct 2 ms 10584 KB Output is correct
26 Correct 5 ms 10988 KB Output is correct
27 Correct 4 ms 11352 KB Output is correct
28 Correct 4 ms 11100 KB Output is correct
29 Correct 4 ms 11100 KB Output is correct
30 Correct 4 ms 10788 KB Output is correct
31 Correct 4 ms 10844 KB Output is correct
32 Correct 6 ms 10844 KB Output is correct
33 Correct 4 ms 10844 KB Output is correct
34 Correct 91 ms 36844 KB Output is correct
35 Correct 109 ms 43120 KB Output is correct
36 Correct 103 ms 43052 KB Output is correct
37 Correct 185 ms 42760 KB Output is correct
38 Correct 165 ms 38740 KB Output is correct
39 Correct 174 ms 41556 KB Output is correct
40 Correct 174 ms 40792 KB Output is correct
41 Correct 2 ms 10588 KB Output is correct
42 Correct 2 ms 10584 KB Output is correct
43 Correct 146 ms 32036 KB Output is correct
44 Correct 162 ms 32212 KB Output is correct
45 Correct 134 ms 32048 KB Output is correct
46 Correct 118 ms 40536 KB Output is correct
47 Correct 117 ms 40304 KB Output is correct
48 Correct 119 ms 40356 KB Output is correct
49 Correct 126 ms 40644 KB Output is correct
50 Correct 198 ms 33872 KB Output is correct
51 Correct 207 ms 33896 KB Output is correct
52 Correct 197 ms 34128 KB Output is correct
53 Correct 195 ms 34024 KB Output is correct