답안 #917991

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
917991 2024-01-29T09:54:55 Z thangdz2k7 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
254 ms 63108 KB
#include <bits/stdc++.h>
#define pb push_back
#define ll long long

using namespace std;

const int N = 2e5 + 5;

int n, p[N], par[N];
vector <int> adj[N], nw[N];

int find(int u){
    if (u == par[u]) return u;
    return par[u] = find(par[u]);
}

int depth[N], to[N][20];

void dfs(int u, int pa){
    for (int v : adj[u]){
        if (v == pa) continue;
        to[v][0] = u;
        depth[v] = depth[u] + 1;
        for (int j = 1; j <= log2(depth[v] - 1); ++ j) to[v][j] = to[to[v][j - 1]][j - 1];
        dfs(v, u);
    }
}

int jump(int u, int le){
    for (int i = log2(le); i >= 0; -- i){
        if ((le >> i) & 1) u = to[u][i];
    }

    return u;
}

int lca(int u, int v){
    if (depth[u] > depth[v]) swap(u, v);
    v = jump(v, depth[v] - depth[u]);
    if (u == v) return u;
    for (int i = log2(depth[u] - 1); i >= 0; -- i){
        if (to[u][i] != to[v][i]){
            u = to[u][i];
            v = to[v][i];
        }
    }

    return to[u][0];
}

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

ll calc(int u){
    ll ans = 0;
    for (int v : nw[u]){
        ans = max(ans, calc(v) + dist(u, v));
    }

    return ans;
}

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

    for (int i = 1; i <= n - 1; ++ i){
        int u, v;
        cin >> u >> v;
        adj[p[u]].pb(p[v]);
        adj[p[v]].pb(p[u]);
    }

    depth[n] = 1;
    dfs(n, 0);

    for (int i = 1; i <= n; ++ i){
        par[i] = i;
        for (int j : adj[i]){
            if (j < i){
                int u = find(j);
                nw[i].pb(u);
                par[u] = i;
            }
        }
    }

    //cout << depth[5] << endl;

    cout << calc(n);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 3 ms 12632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 3 ms 12632 KB Output is correct
11 Correct 3 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 4 ms 12636 KB Output is correct
15 Correct 3 ms 12776 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 3 ms 12772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 3 ms 12632 KB Output is correct
11 Correct 3 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 4 ms 12636 KB Output is correct
15 Correct 3 ms 12776 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 3 ms 12772 KB Output is correct
18 Correct 6 ms 15704 KB Output is correct
19 Correct 5 ms 15708 KB Output is correct
20 Correct 6 ms 15708 KB Output is correct
21 Correct 7 ms 15708 KB Output is correct
22 Correct 6 ms 15452 KB Output is correct
23 Correct 6 ms 15452 KB Output is correct
24 Correct 5 ms 15448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 3 ms 12632 KB Output is correct
11 Correct 3 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 4 ms 12636 KB Output is correct
15 Correct 3 ms 12776 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 3 ms 12772 KB Output is correct
18 Correct 6 ms 15704 KB Output is correct
19 Correct 5 ms 15708 KB Output is correct
20 Correct 6 ms 15708 KB Output is correct
21 Correct 7 ms 15708 KB Output is correct
22 Correct 6 ms 15452 KB Output is correct
23 Correct 6 ms 15452 KB Output is correct
24 Correct 5 ms 15448 KB Output is correct
25 Correct 4 ms 12640 KB Output is correct
26 Correct 6 ms 15456 KB Output is correct
27 Correct 6 ms 15448 KB Output is correct
28 Correct 8 ms 15708 KB Output is correct
29 Correct 6 ms 15716 KB Output is correct
30 Correct 5 ms 15200 KB Output is correct
31 Correct 5 ms 15196 KB Output is correct
32 Correct 6 ms 15236 KB Output is correct
33 Correct 6 ms 15196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 3 ms 12632 KB Output is correct
11 Correct 3 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 4 ms 12636 KB Output is correct
15 Correct 3 ms 12776 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 3 ms 12772 KB Output is correct
18 Correct 6 ms 15704 KB Output is correct
19 Correct 5 ms 15708 KB Output is correct
20 Correct 6 ms 15708 KB Output is correct
21 Correct 7 ms 15708 KB Output is correct
22 Correct 6 ms 15452 KB Output is correct
23 Correct 6 ms 15452 KB Output is correct
24 Correct 5 ms 15448 KB Output is correct
25 Correct 146 ms 63108 KB Output is correct
26 Correct 134 ms 62168 KB Output is correct
27 Correct 132 ms 62160 KB Output is correct
28 Correct 220 ms 55912 KB Output is correct
29 Correct 214 ms 58692 KB Output is correct
30 Correct 217 ms 57940 KB Output is correct
31 Correct 235 ms 58852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 172 ms 42860 KB Output is correct
4 Correct 184 ms 43092 KB Output is correct
5 Correct 167 ms 42832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 3 ms 12632 KB Output is correct
11 Correct 3 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 4 ms 12636 KB Output is correct
15 Correct 3 ms 12776 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 3 ms 12772 KB Output is correct
18 Correct 6 ms 15704 KB Output is correct
19 Correct 5 ms 15708 KB Output is correct
20 Correct 6 ms 15708 KB Output is correct
21 Correct 7 ms 15708 KB Output is correct
22 Correct 6 ms 15452 KB Output is correct
23 Correct 6 ms 15452 KB Output is correct
24 Correct 5 ms 15448 KB Output is correct
25 Correct 4 ms 12640 KB Output is correct
26 Correct 6 ms 15456 KB Output is correct
27 Correct 6 ms 15448 KB Output is correct
28 Correct 8 ms 15708 KB Output is correct
29 Correct 6 ms 15716 KB Output is correct
30 Correct 5 ms 15200 KB Output is correct
31 Correct 5 ms 15196 KB Output is correct
32 Correct 6 ms 15236 KB Output is correct
33 Correct 6 ms 15196 KB Output is correct
34 Correct 146 ms 63108 KB Output is correct
35 Correct 134 ms 62168 KB Output is correct
36 Correct 132 ms 62160 KB Output is correct
37 Correct 220 ms 55912 KB Output is correct
38 Correct 214 ms 58692 KB Output is correct
39 Correct 217 ms 57940 KB Output is correct
40 Correct 235 ms 58852 KB Output is correct
41 Correct 3 ms 12636 KB Output is correct
42 Correct 3 ms 12636 KB Output is correct
43 Correct 172 ms 42860 KB Output is correct
44 Correct 184 ms 43092 KB Output is correct
45 Correct 167 ms 42832 KB Output is correct
46 Correct 203 ms 56256 KB Output is correct
47 Correct 155 ms 56328 KB Output is correct
48 Correct 160 ms 58172 KB Output is correct
49 Correct 178 ms 56220 KB Output is correct
50 Correct 212 ms 41980 KB Output is correct
51 Correct 254 ms 42064 KB Output is correct
52 Correct 222 ms 42052 KB Output is correct
53 Correct 245 ms 42324 KB Output is correct