Submission #917988

# Submission time Handle Problem Language Result Execution time Memory
917988 2024-01-29T09:50:14 Z thangdz2k7 Cat Exercise (JOI23_ho_t4) C++17
0 / 100
4 ms 12636 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];
    }
}

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 << calc(n);
}

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

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12636 KB Output is correct
2 Incorrect 3 ms 12632 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12636 KB Output is correct
2 Incorrect 3 ms 12632 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12636 KB Output is correct
2 Incorrect 3 ms 12632 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12636 KB Output is correct
2 Incorrect 3 ms 12632 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12636 KB Output is correct
2 Incorrect 3 ms 12632 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 12632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12636 KB Output is correct
2 Incorrect 3 ms 12632 KB Output isn't correct
3 Halted 0 ms 0 KB -