답안 #861727

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
861727 2023-10-16T19:31:51 Z arbuzick Cat Exercise (JOI23_ho_t4) C++17
100 / 100
217 ms 63300 KB
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2")
#include <bits/stdc++.h>

using namespace std;

struct DSU {
    int n;
    vector<int> sz, p;
    vector<pair<int, int>> mx;

    DSU(int _n, vector<int> vals) {
        n = _n;
        sz.assign(n, 1);
        p.resize(n);
        iota(p.begin(), p.end(), 0);
        mx.resize(n);
        for (int i = 0; i < n; ++i) {
            mx[i] = {vals[i], i};
        }
    }

    int pr(int v) {
        if (p[v] == v) {
            return v;
        }
        return p[v] = pr(p[v]);
    }

    void unite(int a, int b) {
        a = pr(a), b = pr(b);
        if (a == b) {
            return;
        }
        if (sz[a] < sz[b]) {
            swap(a, b);
        }
        p[b] = a;
        sz[a] += sz[b];
        mx[a] = max(mx[a], mx[b]);
    }
};

constexpr int maxn = 2e5 + 5, lg = 20;

vector<int> g[maxn];

int go[lg][maxn];

int tin[maxn], tout[maxn];

int t = 0;

int d[maxn];

void dfs(int v) {
    tin[v] = t;
    t++;
    for (auto u : g[v]) {
        if (u != go[0][v]) {
            go[0][u] = v;
            d[u] = d[v] + 1;
            dfs(u);
        }
    }
    tout[v] = t;
}

int lca(int a, int b) {
    if (tin[a] <= tin[b] && tout[b] <= tout[a]) {
        return a;
    }
    if (tin[b] <= tin[a] && tout[a] <= tout[b]) {
        return b;
    }
    int v = a;
    for (int i = lg - 1; i >= 0; --i) {
        if (!(tin[go[i][v]] <= tin[b] && tout[b] <= tout[go[i][v]])) {
            v = go[i][v];
        }
    }
    return go[0][v];
}

int get_dist(int a, int b) {
    return d[a] + d[b] - d[lca(a, b)] * 2;
}

void solve() {
    int n;
    cin >> n;
    vector<int> p(n);
    vector<pair<int, int>> ord(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        p[i]--;
        ord[i] = {p[i], i};
    }
    sort(ord.begin(), ord.end());
    for (int i = 0; i < n - 1; ++i) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(0);
    for (int i = 1; i < lg; ++i) {
        for (int j = 0; j < n; ++j) {
            go[i][j] = go[i - 1][go[i - 1][j]];
        }
    }
    vector<long long> dp(n);
    vector<vector<int>> updates(n);
    DSU d(n, p);
    for (int i = 0; i < n; ++i) {
        for (auto j : g[ord[i].second]) {
            if (p[j] < ord[i].first) {
                updates[i].push_back(d.mx[d.pr(j)].first);
                d.unite(ord[i].second, j);
            }
        }
    }
    long long ans = 0;
    for (int i = n - 1; i >= 0; --i) {
        for (auto j : updates[i]) {
            dp[j] = max(dp[j], dp[i] + get_dist(ord[i].second, ord[j].second));
            ans = max(ans, dp[j]);
        }
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18884 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18780 KB Output is correct
9 Correct 3 ms 19032 KB Output is correct
10 Correct 3 ms 18776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18884 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18780 KB Output is correct
9 Correct 3 ms 19032 KB Output is correct
10 Correct 3 ms 18776 KB Output is correct
11 Correct 3 ms 18776 KB Output is correct
12 Correct 3 ms 18780 KB Output is correct
13 Correct 3 ms 18780 KB Output is correct
14 Correct 3 ms 18856 KB Output is correct
15 Correct 3 ms 18780 KB Output is correct
16 Correct 3 ms 18776 KB Output is correct
17 Correct 3 ms 18780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18884 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18780 KB Output is correct
9 Correct 3 ms 19032 KB Output is correct
10 Correct 3 ms 18776 KB Output is correct
11 Correct 3 ms 18776 KB Output is correct
12 Correct 3 ms 18780 KB Output is correct
13 Correct 3 ms 18780 KB Output is correct
14 Correct 3 ms 18856 KB Output is correct
15 Correct 3 ms 18780 KB Output is correct
16 Correct 3 ms 18776 KB Output is correct
17 Correct 3 ms 18780 KB Output is correct
18 Correct 5 ms 19804 KB Output is correct
19 Correct 6 ms 19860 KB Output is correct
20 Correct 5 ms 19804 KB Output is correct
21 Correct 5 ms 19804 KB Output is correct
22 Correct 5 ms 19804 KB Output is correct
23 Correct 6 ms 19808 KB Output is correct
24 Correct 5 ms 19804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18884 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18780 KB Output is correct
9 Correct 3 ms 19032 KB Output is correct
10 Correct 3 ms 18776 KB Output is correct
11 Correct 3 ms 18776 KB Output is correct
12 Correct 3 ms 18780 KB Output is correct
13 Correct 3 ms 18780 KB Output is correct
14 Correct 3 ms 18856 KB Output is correct
15 Correct 3 ms 18780 KB Output is correct
16 Correct 3 ms 18776 KB Output is correct
17 Correct 3 ms 18780 KB Output is correct
18 Correct 5 ms 19804 KB Output is correct
19 Correct 6 ms 19860 KB Output is correct
20 Correct 5 ms 19804 KB Output is correct
21 Correct 5 ms 19804 KB Output is correct
22 Correct 5 ms 19804 KB Output is correct
23 Correct 6 ms 19808 KB Output is correct
24 Correct 5 ms 19804 KB Output is correct
25 Correct 3 ms 19032 KB Output is correct
26 Correct 6 ms 19596 KB Output is correct
27 Correct 5 ms 19804 KB Output is correct
28 Correct 6 ms 19824 KB Output is correct
29 Correct 6 ms 19800 KB Output is correct
30 Correct 6 ms 19548 KB Output is correct
31 Correct 6 ms 19544 KB Output is correct
32 Correct 6 ms 19704 KB Output is correct
33 Correct 6 ms 19548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18884 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18780 KB Output is correct
9 Correct 3 ms 19032 KB Output is correct
10 Correct 3 ms 18776 KB Output is correct
11 Correct 3 ms 18776 KB Output is correct
12 Correct 3 ms 18780 KB Output is correct
13 Correct 3 ms 18780 KB Output is correct
14 Correct 3 ms 18856 KB Output is correct
15 Correct 3 ms 18780 KB Output is correct
16 Correct 3 ms 18776 KB Output is correct
17 Correct 3 ms 18780 KB Output is correct
18 Correct 5 ms 19804 KB Output is correct
19 Correct 6 ms 19860 KB Output is correct
20 Correct 5 ms 19804 KB Output is correct
21 Correct 5 ms 19804 KB Output is correct
22 Correct 5 ms 19804 KB Output is correct
23 Correct 6 ms 19808 KB Output is correct
24 Correct 5 ms 19804 KB Output is correct
25 Correct 91 ms 61752 KB Output is correct
26 Correct 93 ms 63020 KB Output is correct
27 Correct 96 ms 63300 KB Output is correct
28 Correct 120 ms 61504 KB Output is correct
29 Correct 121 ms 61392 KB Output is correct
30 Correct 126 ms 61508 KB Output is correct
31 Correct 124 ms 61504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18780 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 180 ms 47104 KB Output is correct
4 Correct 170 ms 48708 KB Output is correct
5 Correct 200 ms 49032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18884 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18780 KB Output is correct
9 Correct 3 ms 19032 KB Output is correct
10 Correct 3 ms 18776 KB Output is correct
11 Correct 3 ms 18776 KB Output is correct
12 Correct 3 ms 18780 KB Output is correct
13 Correct 3 ms 18780 KB Output is correct
14 Correct 3 ms 18856 KB Output is correct
15 Correct 3 ms 18780 KB Output is correct
16 Correct 3 ms 18776 KB Output is correct
17 Correct 3 ms 18780 KB Output is correct
18 Correct 5 ms 19804 KB Output is correct
19 Correct 6 ms 19860 KB Output is correct
20 Correct 5 ms 19804 KB Output is correct
21 Correct 5 ms 19804 KB Output is correct
22 Correct 5 ms 19804 KB Output is correct
23 Correct 6 ms 19808 KB Output is correct
24 Correct 5 ms 19804 KB Output is correct
25 Correct 3 ms 19032 KB Output is correct
26 Correct 6 ms 19596 KB Output is correct
27 Correct 5 ms 19804 KB Output is correct
28 Correct 6 ms 19824 KB Output is correct
29 Correct 6 ms 19800 KB Output is correct
30 Correct 6 ms 19548 KB Output is correct
31 Correct 6 ms 19544 KB Output is correct
32 Correct 6 ms 19704 KB Output is correct
33 Correct 6 ms 19548 KB Output is correct
34 Correct 91 ms 61752 KB Output is correct
35 Correct 93 ms 63020 KB Output is correct
36 Correct 96 ms 63300 KB Output is correct
37 Correct 120 ms 61504 KB Output is correct
38 Correct 121 ms 61392 KB Output is correct
39 Correct 126 ms 61508 KB Output is correct
40 Correct 124 ms 61504 KB Output is correct
41 Correct 3 ms 18780 KB Output is correct
42 Correct 3 ms 18780 KB Output is correct
43 Correct 180 ms 47104 KB Output is correct
44 Correct 170 ms 48708 KB Output is correct
45 Correct 200 ms 49032 KB Output is correct
46 Correct 164 ms 57144 KB Output is correct
47 Correct 158 ms 55864 KB Output is correct
48 Correct 150 ms 57692 KB Output is correct
49 Correct 160 ms 55864 KB Output is correct
50 Correct 217 ms 48960 KB Output is correct
51 Correct 203 ms 48956 KB Output is correct
52 Correct 193 ms 48808 KB Output is correct
53 Correct 202 ms 48964 KB Output is correct