Submission #1042879

# Submission time Handle Problem Language Result Execution time Memory
1042879 2024-08-03T13:53:57 Z SoulKnight Cat Exercise (JOI23_ho_t4) C++17
100 / 100
184 ms 43932 KB
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define ln '\n'

const int N = 2e5 + 5;
const int LG = 20;

int n, a[N], par[N], dep[N], up[LG][N];
ll dp[N];
vector<int> adj[N];

void dfs(int u, int p){
    dep[u] = 1 + dep[p];

    up[0][u] = p;
    for (int i = 1; i < LG; i++) up[i][u] = up[i-1][up[i-1][u]];

    for (auto v: adj[u]){
        if (v == p) continue;
        dfs(v, u);
    }
}

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

    for (int i = LG-1; i >= 0; i--) if (dep[up[i][u]] >= dep[v]) u = up[i][u];
    if (u == v) return u;

    for (int i = LG-1; i >= 0; i--){
        if (up[i][u] != up[i][v]) u = up[i][u], v = up[i][v];
    }
    return up[0][u];
}

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

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






void solve(){
    cin >> n;
    iota(par, par+n+1, 0);
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 0; i < n-1; i++){
        int u, v; cin >> u >> v;
        u = a[u]; v = a[v];
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(n, n);

    for (int i = 1; i <= n; i++){
        for (auto v: adj[i]){
            v = find(v);
            if (v < i) {
                par[v] = i;
                dp[i] = max(dp[i], dist(i, v) + dp[v]);
            }
        }
    }
    cout << dp[n] << ln;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // int TT; cin >> TT;
    // while (TT--) {solve();}

    solve();

}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5216 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5216 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 3 ms 5136 KB Output is correct
15 Correct 2 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5216 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 3 ms 5136 KB Output is correct
15 Correct 2 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5164 KB Output is correct
18 Correct 7 ms 6236 KB Output is correct
19 Correct 4 ms 6232 KB Output is correct
20 Correct 6 ms 6488 KB Output is correct
21 Correct 4 ms 5976 KB Output is correct
22 Correct 6 ms 6128 KB Output is correct
23 Correct 5 ms 5980 KB Output is correct
24 Correct 4 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5216 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 3 ms 5136 KB Output is correct
15 Correct 2 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5164 KB Output is correct
18 Correct 7 ms 6236 KB Output is correct
19 Correct 4 ms 6232 KB Output is correct
20 Correct 6 ms 6488 KB Output is correct
21 Correct 4 ms 5976 KB Output is correct
22 Correct 6 ms 6128 KB Output is correct
23 Correct 5 ms 5980 KB Output is correct
24 Correct 4 ms 5980 KB Output is correct
25 Correct 2 ms 5208 KB Output is correct
26 Correct 4 ms 5980 KB Output is correct
27 Correct 5 ms 5980 KB Output is correct
28 Correct 4 ms 5980 KB Output is correct
29 Correct 4 ms 5980 KB Output is correct
30 Correct 4 ms 5944 KB Output is correct
31 Correct 4 ms 5944 KB Output is correct
32 Correct 4 ms 5980 KB Output is correct
33 Correct 4 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5216 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 3 ms 5136 KB Output is correct
15 Correct 2 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5164 KB Output is correct
18 Correct 7 ms 6236 KB Output is correct
19 Correct 4 ms 6232 KB Output is correct
20 Correct 6 ms 6488 KB Output is correct
21 Correct 4 ms 5976 KB Output is correct
22 Correct 6 ms 6128 KB Output is correct
23 Correct 5 ms 5980 KB Output is correct
24 Correct 4 ms 5980 KB Output is correct
25 Correct 74 ms 43896 KB Output is correct
26 Correct 89 ms 43932 KB Output is correct
27 Correct 88 ms 43860 KB Output is correct
28 Correct 149 ms 41560 KB Output is correct
29 Correct 184 ms 42836 KB Output is correct
30 Correct 153 ms 42560 KB Output is correct
31 Correct 160 ms 43088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 134 ms 34640 KB Output is correct
4 Correct 136 ms 34640 KB Output is correct
5 Correct 146 ms 34620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5216 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 3 ms 5136 KB Output is correct
15 Correct 2 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5164 KB Output is correct
18 Correct 7 ms 6236 KB Output is correct
19 Correct 4 ms 6232 KB Output is correct
20 Correct 6 ms 6488 KB Output is correct
21 Correct 4 ms 5976 KB Output is correct
22 Correct 6 ms 6128 KB Output is correct
23 Correct 5 ms 5980 KB Output is correct
24 Correct 4 ms 5980 KB Output is correct
25 Correct 2 ms 5208 KB Output is correct
26 Correct 4 ms 5980 KB Output is correct
27 Correct 5 ms 5980 KB Output is correct
28 Correct 4 ms 5980 KB Output is correct
29 Correct 4 ms 5980 KB Output is correct
30 Correct 4 ms 5944 KB Output is correct
31 Correct 4 ms 5944 KB Output is correct
32 Correct 4 ms 5980 KB Output is correct
33 Correct 4 ms 5980 KB Output is correct
34 Correct 74 ms 43896 KB Output is correct
35 Correct 89 ms 43932 KB Output is correct
36 Correct 88 ms 43860 KB Output is correct
37 Correct 149 ms 41560 KB Output is correct
38 Correct 184 ms 42836 KB Output is correct
39 Correct 153 ms 42560 KB Output is correct
40 Correct 160 ms 43088 KB Output is correct
41 Correct 2 ms 5208 KB Output is correct
42 Correct 2 ms 5212 KB Output is correct
43 Correct 134 ms 34640 KB Output is correct
44 Correct 136 ms 34640 KB Output is correct
45 Correct 146 ms 34620 KB Output is correct
46 Correct 98 ms 41296 KB Output is correct
47 Correct 103 ms 41280 KB Output is correct
48 Correct 96 ms 42068 KB Output is correct
49 Correct 98 ms 41224 KB Output is correct
50 Correct 168 ms 34624 KB Output is correct
51 Correct 170 ms 34816 KB Output is correct
52 Correct 167 ms 34644 KB Output is correct
53 Correct 155 ms 34644 KB Output is correct