답안 #895150

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895150 2023-12-29T13:39:02 Z hmm789 Cat Exercise (JOI23_ho_t4) C++14
100 / 100
218 ms 58968 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1000000000000000000
#define MOD 998244353

vector<int> adj[200000];
int p[200000][18], depth[200000], dp[200000], pa[200000];

int root(int x) {
    if(pa[x] == x) return x;
    else return pa[x] = root(pa[x]);
}

void merge(int x, int y) {
    x = root(x); y = root(y);
    if(x == y) return;
    if(x > y) swap(x, y);
    pa[x] = y;
}

void dfs(int x, int par, int d) {
    depth[x] = d;
    p[x][0] = par;
    for(int i = 1; i < 18; i++) {
        if(p[x][i-1] == -1) break;
        p[x][i] = p[p[x][i-1]][i-1];
    }
    for(int i : adj[x]) if(i != par) dfs(i, x, d+1);
}

int lca(int x, int y) {
    if(depth[x] < depth[y]) swap(x, y);
    int h = depth[x]-depth[y];
    for(int i = 0; i < 18; i++) {
        if(h&(1<<i)) x = p[x][i];
    }
    if(x == y) return x;
    for(int i = 17; i >= 0; i--) {
        if(p[x][i] != p[y][i]) {
            x = p[x][i]; y = p[y][i];
        }
    }
    return p[x][0];
}

int dist(int x, int y) {
    return depth[x]+depth[y]-2*depth[lca(x, y)];
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, x, y;
    cin >> n;
    int p[n];
    for(int i = 0; i < n; i++) {
        cin >> p[i];
        p[i]--;
    }
    for(int i = 0; i < n-1; i++) {
        cin >> x >> y;
        x--; y--;
        adj[p[x]].push_back(p[y]);
        adj[p[y]].push_back(p[x]);
    }
    memset(p, -1, sizeof(p));
    for(int i = 0; i < n; i++) pa[i] = i;
    dfs(0, -1, 0);
    for(int i = 0; i < n; i++) {
        for(int j : adj[i]) if(j < i) {
            int rt = root(j);
            dp[i] = max(dp[i], dp[rt]+dist(rt, i));
            merge(i, j);
        }
    }
    cout << dp[n-1];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 2 ms 11172 KB Output is correct
5 Correct 2 ms 11172 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 2 ms 11172 KB Output is correct
5 Correct 2 ms 11172 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11164 KB Output is correct
11 Correct 3 ms 11096 KB Output is correct
12 Correct 2 ms 11100 KB Output is correct
13 Correct 2 ms 11100 KB Output is correct
14 Correct 2 ms 11096 KB Output is correct
15 Correct 2 ms 11100 KB Output is correct
16 Correct 2 ms 11096 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 2 ms 11172 KB Output is correct
5 Correct 2 ms 11172 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11164 KB Output is correct
11 Correct 3 ms 11096 KB Output is correct
12 Correct 2 ms 11100 KB Output is correct
13 Correct 2 ms 11100 KB Output is correct
14 Correct 2 ms 11096 KB Output is correct
15 Correct 2 ms 11100 KB Output is correct
16 Correct 2 ms 11096 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 4 ms 11612 KB Output is correct
19 Correct 4 ms 11700 KB Output is correct
20 Correct 5 ms 11612 KB Output is correct
21 Correct 4 ms 11612 KB Output is correct
22 Correct 4 ms 11612 KB Output is correct
23 Correct 4 ms 11864 KB Output is correct
24 Correct 4 ms 11612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 2 ms 11172 KB Output is correct
5 Correct 2 ms 11172 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11164 KB Output is correct
11 Correct 3 ms 11096 KB Output is correct
12 Correct 2 ms 11100 KB Output is correct
13 Correct 2 ms 11100 KB Output is correct
14 Correct 2 ms 11096 KB Output is correct
15 Correct 2 ms 11100 KB Output is correct
16 Correct 2 ms 11096 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 4 ms 11612 KB Output is correct
19 Correct 4 ms 11700 KB Output is correct
20 Correct 5 ms 11612 KB Output is correct
21 Correct 4 ms 11612 KB Output is correct
22 Correct 4 ms 11612 KB Output is correct
23 Correct 4 ms 11864 KB Output is correct
24 Correct 4 ms 11612 KB Output is correct
25 Correct 2 ms 11100 KB Output is correct
26 Correct 4 ms 11612 KB Output is correct
27 Correct 4 ms 11696 KB Output is correct
28 Correct 4 ms 11700 KB Output is correct
29 Correct 4 ms 11624 KB Output is correct
30 Correct 4 ms 11368 KB Output is correct
31 Correct 4 ms 11368 KB Output is correct
32 Correct 5 ms 11368 KB Output is correct
33 Correct 4 ms 11368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 2 ms 11172 KB Output is correct
5 Correct 2 ms 11172 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11164 KB Output is correct
11 Correct 3 ms 11096 KB Output is correct
12 Correct 2 ms 11100 KB Output is correct
13 Correct 2 ms 11100 KB Output is correct
14 Correct 2 ms 11096 KB Output is correct
15 Correct 2 ms 11100 KB Output is correct
16 Correct 2 ms 11096 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 4 ms 11612 KB Output is correct
19 Correct 4 ms 11700 KB Output is correct
20 Correct 5 ms 11612 KB Output is correct
21 Correct 4 ms 11612 KB Output is correct
22 Correct 4 ms 11612 KB Output is correct
23 Correct 4 ms 11864 KB Output is correct
24 Correct 4 ms 11612 KB Output is correct
25 Correct 131 ms 54112 KB Output is correct
26 Correct 113 ms 58968 KB Output is correct
27 Correct 99 ms 58924 KB Output is correct
28 Correct 170 ms 58356 KB Output is correct
29 Correct 172 ms 54408 KB Output is correct
30 Correct 167 ms 57168 KB Output is correct
31 Correct 174 ms 56656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 170 ms 51284 KB Output is correct
4 Correct 159 ms 51064 KB Output is correct
5 Correct 162 ms 51024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 2 ms 11172 KB Output is correct
5 Correct 2 ms 11172 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11164 KB Output is correct
11 Correct 3 ms 11096 KB Output is correct
12 Correct 2 ms 11100 KB Output is correct
13 Correct 2 ms 11100 KB Output is correct
14 Correct 2 ms 11096 KB Output is correct
15 Correct 2 ms 11100 KB Output is correct
16 Correct 2 ms 11096 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 4 ms 11612 KB Output is correct
19 Correct 4 ms 11700 KB Output is correct
20 Correct 5 ms 11612 KB Output is correct
21 Correct 4 ms 11612 KB Output is correct
22 Correct 4 ms 11612 KB Output is correct
23 Correct 4 ms 11864 KB Output is correct
24 Correct 4 ms 11612 KB Output is correct
25 Correct 2 ms 11100 KB Output is correct
26 Correct 4 ms 11612 KB Output is correct
27 Correct 4 ms 11696 KB Output is correct
28 Correct 4 ms 11700 KB Output is correct
29 Correct 4 ms 11624 KB Output is correct
30 Correct 4 ms 11368 KB Output is correct
31 Correct 4 ms 11368 KB Output is correct
32 Correct 5 ms 11368 KB Output is correct
33 Correct 4 ms 11368 KB Output is correct
34 Correct 131 ms 54112 KB Output is correct
35 Correct 113 ms 58968 KB Output is correct
36 Correct 99 ms 58924 KB Output is correct
37 Correct 170 ms 58356 KB Output is correct
38 Correct 172 ms 54408 KB Output is correct
39 Correct 167 ms 57168 KB Output is correct
40 Correct 174 ms 56656 KB Output is correct
41 Correct 2 ms 11096 KB Output is correct
42 Correct 2 ms 11100 KB Output is correct
43 Correct 170 ms 51284 KB Output is correct
44 Correct 159 ms 51064 KB Output is correct
45 Correct 162 ms 51024 KB Output is correct
46 Correct 149 ms 56648 KB Output is correct
47 Correct 135 ms 56656 KB Output is correct
48 Correct 133 ms 56660 KB Output is correct
49 Correct 132 ms 56584 KB Output is correct
50 Correct 196 ms 50660 KB Output is correct
51 Correct 218 ms 50436 KB Output is correct
52 Correct 197 ms 50692 KB Output is correct
53 Correct 209 ms 50508 KB Output is correct