제출 #1038457

#제출 시각아이디문제언어결과실행 시간메모리
1038457SoulKnightCat Exercise (JOI23_ho_t4)C++17
21 / 100
91 ms67156 KiB
#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, glit = 1, p[N], mx[LG][N], up[LG][N], where[N], sz[N];
vector<int> adj[N];
bool used[N];

int dfs(int u, int par){
    up[0][u] = par;
    mx[0][glit] = u;

    where[u] = glit++;

    int tot = 1;
    for (auto v: adj[u]){
        if (v == par) continue;
        tot += dfs(v, u);
    }
    sz[where[u]] = tot;
    return tot;
}

int answer(int l, int r){
    if (l > r) return 0;
    int lg = log2(r-l+1);
    return p[mx[lg][l]] > p[mx[lg][r-(1<<lg)+1]]? mx[lg][l]: mx[lg][r-(1<<lg)+1];
}

bool is_ancestor(int u, int v){
    return where[u] <= where[v] && where[v] <= where[u] + sz[where[u]] - 1;
}

int lca(int u, int v){
    if (is_ancestor(u, v)) return u;
    if (is_ancestor(v, u)) return v;

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

int dist(int u, int v){
    int ances = lca(u, v);
    int ans = 0;
    for (int i = LG-1; i >= 0; i--){
        if (!is_ancestor(up[i][u], ances)) {ans += (1 << i); u = up[i][u];}
        if (!is_ancestor(up[i][v], ances)) {ans += (1 << i); v = up[i][v];}
    }

    return ans + (u != ances) + (v != ances);
}

int f(int u, int tp, int btm){
    used[u] = 1;

    // cout << u << ' ' << tp << ' ' << btm << ln;

    int res = 0;
    for (auto v: adj[u]){
        if (v == up[0][u]) continue;
        if (used[v]) continue;
        // cout << "checking " << v << ln;

        int nxt;
        if (btm == -1) nxt = answer(where[v],
                                    where[v] + sz[where[v]] - 1);

        else nxt = max(answer(where[v], where[btm]-1),
                       answer(where[btm] + sz[where[btm]], where[v] + sz[where[v]]-1),
                       [&](int x, int y){return p[x] < p[y];});
        // cout << "into " << nxt << ln;
        if (!used[nxt] && nxt) res = max(res, dist(u, nxt) + f(nxt, v, btm));
    }

    int par = up[0][u];
    if (par == u) return res;

    int nxt;
    nxt = max(answer(where[tp], where[u] - 1),
              answer(where[u] + sz[where[u]], where[tp] + sz[where[tp]] - 1),
              [&](int x, int y){return p[x] < p[y];});
    if (!used[nxt] && nxt) res = max(res, dist(u, nxt) + f(nxt, tp, u));

    used[u] = 0;
    return res;
}


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


    // for (int i = 1; i <= n; i++) cout << where[i] << " ";
    // cout << ln;
    // for (int i = 1; i <= n; i++) cout << sz[where[i]] << " ";

    for (int i = 1; i < LG; i++){
        for (int j = 1; j <= n; j++) up[i][j] = up[i-1][up[i-1][j]];
        for (int j = 1; j + (1 << (i-1)) <= n; j++){
            mx[i][j] = (p[mx[i-1][j]] > p[mx[i-1][j + (1 << (i-1))]])? mx[i-1][j]: mx[i-1][j + (1 << (i-1))];
        }
    }

    cout << f(root, root, -1) << ln;
    // cout << where[1] << ' ' << where[1] + sz[where[1]] - 1 << ln;
    // cout << answer(where[1], where[1] + sz[1] -1) << ln;
    // for (int i = 1; i <= n; i++) cout << mx[0][i] << ' ';

}

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

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

    solve();

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...