제출 #1352442

#제출 시각아이디문제언어결과실행 시간메모리
1352442chithanhnguyenCat Exercise (JOI23_ho_t4)C++20
100 / 100
169 ms57464 KiB
/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;

/* START OF TEMPALTE */

// #define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) (1ll << (x))
#define SZ(a) ((int32_t)a.size())

#define debug(a, l, r) {for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';}

template<class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x > y) {
        x = y;
        return true;
    } else return false;
}

template<class X, class Y>
bool maximize(X &x, const Y &y) {
    if (x < y) {
        x = y;
        return true;
    } else return false;
}

/* END OF TEMPALTE */

struct DSU {
    int n;
    vector<int> par;

    DSU(int _n) : n(_n) {
        par.resize(n + 5);
        iota(all(par), 0);
    }

    int findSet(int u) {
        while (u != par[u])
            u = par[u] = par[par[u]];
        return u;
    }

    // x is parent of y
    bool unite(int x, int y) {
        x = findSet(x);
        y = findSet(y);

        if (x == y) return 0;

        par[y] = x;

        return 1;
    }
};

const int MAXN = 2e5 + 5;
const int LG   = 18;
int n, h[MAXN], pos[MAXN], depth[MAXN], up[LG + 1][MAXN];
ll dp[MAXN];
vector<int> adj[MAXN], g[MAXN];

void init() {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> h[i];
        pos[h[i]] = i;
    }

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

void dfsLCA(int u, int par = 0) {
    up[0][u] = par;
    for (int j = 1; j <= LG; ++j)
        up[j][u] = up[j - 1][up[j - 1][u]];

    for (int v : adj[u]) {
        if (v == par) continue;
        depth[v] = depth[u] + 1;
        dfsLCA(v, u);
    }
}

int lift(int u, int k) {
    for (int j = 0; j <= LG; ++j)
        if (BIT(k, j)) u = up[j][u];
    return u;
}

int lca(int u, int v) {
    if (depth[u] > depth[v]) swap(u, v);
    int diff = depth[v] - depth[u];
    v = lift(v, diff);

    if (u == v) return u;

    for (int j = LG; j >= 0; --j) {
        if (up[j][u] != up[j][v]) {
            u = up[j][u];
            v = up[j][v];
        }
    }

    return up[0][u];
}

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

void dfsDP(int u, int par = 0) {
    for (int v : g[u]) {
        if (v == par) continue;
        dfsDP(v, u);
        maximize(dp[u], dp[v] + dist(u, v));
    }
}

void solve() {
    dfsLCA(1);

    DSU dsu(n);
    for (int i = 1; i <= n; ++i) {
        int u = pos[i];
        for (int v : adj[u]) {
            if (h[v] > h[u]) continue;
            int to = dsu.findSet(v);
            g[u].push_back(to);
            dsu.unite(u, to);
        }
    }

    dfsDP(pos[n]);
    cout << dp[pos[n]];
}

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    init();
    solve();

    return 0;
}
#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...