답안 #861721

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
861721 2023-10-16T18:17:31 Z arbuzick Cat Exercise (JOI23_ho_t4) C++17
31 / 100
2000 ms 51672 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;

    DSU(int _n) {
        n = _n;
        sz.assign(n, 1);
        p.resize(n);
        iota(p.begin(), p.end(), 0);
    }

    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];
    }
};

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];
        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);
    long long ans = 0;
    for (int i = n - 1; i >= 0; --i) {
        DSU d(n);
        for (auto j : g[ord[i].second]) {
            d.unite(ord[i].second, j);
        }
        for (int j = 0; j < i; ++j) {
            for (auto k : g[ord[j].second]) {
                if (p[k] < ord[j].first) {
                    d.unite(ord[j].second, k);
                }
            }
            if (d.pr(ord[i].second) == d.pr(ord[j].second)) {
                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 18780 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18776 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 18904 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18900 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18860 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 3 ms 18776 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 18904 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18900 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18860 KB Output is correct
11 Correct 4 ms 18852 KB Output is correct
12 Correct 4 ms 18852 KB Output is correct
13 Correct 4 ms 18780 KB Output is correct
14 Correct 4 ms 18944 KB Output is correct
15 Correct 4 ms 18860 KB Output is correct
16 Correct 4 ms 18780 KB Output is correct
17 Correct 4 ms 18776 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 3 ms 18776 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 18904 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18900 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18860 KB Output is correct
11 Correct 4 ms 18852 KB Output is correct
12 Correct 4 ms 18852 KB Output is correct
13 Correct 4 ms 18780 KB Output is correct
14 Correct 4 ms 18944 KB Output is correct
15 Correct 4 ms 18860 KB Output is correct
16 Correct 4 ms 18780 KB Output is correct
17 Correct 4 ms 18776 KB Output is correct
18 Correct 130 ms 19676 KB Output is correct
19 Correct 149 ms 19548 KB Output is correct
20 Correct 146 ms 19544 KB Output is correct
21 Correct 204 ms 19792 KB Output is correct
22 Correct 197 ms 19676 KB Output is correct
23 Correct 201 ms 19676 KB Output is correct
24 Correct 202 ms 19920 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 3 ms 18776 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 18904 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18900 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18860 KB Output is correct
11 Correct 4 ms 18852 KB Output is correct
12 Correct 4 ms 18852 KB Output is correct
13 Correct 4 ms 18780 KB Output is correct
14 Correct 4 ms 18944 KB Output is correct
15 Correct 4 ms 18860 KB Output is correct
16 Correct 4 ms 18780 KB Output is correct
17 Correct 4 ms 18776 KB Output is correct
18 Correct 130 ms 19676 KB Output is correct
19 Correct 149 ms 19548 KB Output is correct
20 Correct 146 ms 19544 KB Output is correct
21 Correct 204 ms 19792 KB Output is correct
22 Correct 197 ms 19676 KB Output is correct
23 Correct 201 ms 19676 KB Output is correct
24 Correct 202 ms 19920 KB Output is correct
25 Correct 3 ms 18776 KB Output is correct
26 Correct 163 ms 19548 KB Output is correct
27 Correct 168 ms 19548 KB Output is correct
28 Correct 181 ms 19512 KB Output is correct
29 Correct 166 ms 19548 KB Output is correct
30 Correct 303 ms 19544 KB Output is correct
31 Correct 302 ms 19372 KB Output is correct
32 Correct 289 ms 19368 KB Output is correct
33 Correct 307 ms 19372 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 3 ms 18776 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 18904 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18900 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18860 KB Output is correct
11 Correct 4 ms 18852 KB Output is correct
12 Correct 4 ms 18852 KB Output is correct
13 Correct 4 ms 18780 KB Output is correct
14 Correct 4 ms 18944 KB Output is correct
15 Correct 4 ms 18860 KB Output is correct
16 Correct 4 ms 18780 KB Output is correct
17 Correct 4 ms 18776 KB Output is correct
18 Correct 130 ms 19676 KB Output is correct
19 Correct 149 ms 19548 KB Output is correct
20 Correct 146 ms 19544 KB Output is correct
21 Correct 204 ms 19792 KB Output is correct
22 Correct 197 ms 19676 KB Output is correct
23 Correct 201 ms 19676 KB Output is correct
24 Correct 202 ms 19920 KB Output is correct
25 Execution timed out 2101 ms 51672 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18776 KB Output is correct
2 Correct 4 ms 18780 KB Output is correct
3 Execution timed out 2080 ms 38768 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18780 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18776 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 18904 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18900 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18860 KB Output is correct
11 Correct 4 ms 18852 KB Output is correct
12 Correct 4 ms 18852 KB Output is correct
13 Correct 4 ms 18780 KB Output is correct
14 Correct 4 ms 18944 KB Output is correct
15 Correct 4 ms 18860 KB Output is correct
16 Correct 4 ms 18780 KB Output is correct
17 Correct 4 ms 18776 KB Output is correct
18 Correct 130 ms 19676 KB Output is correct
19 Correct 149 ms 19548 KB Output is correct
20 Correct 146 ms 19544 KB Output is correct
21 Correct 204 ms 19792 KB Output is correct
22 Correct 197 ms 19676 KB Output is correct
23 Correct 201 ms 19676 KB Output is correct
24 Correct 202 ms 19920 KB Output is correct
25 Correct 3 ms 18776 KB Output is correct
26 Correct 163 ms 19548 KB Output is correct
27 Correct 168 ms 19548 KB Output is correct
28 Correct 181 ms 19512 KB Output is correct
29 Correct 166 ms 19548 KB Output is correct
30 Correct 303 ms 19544 KB Output is correct
31 Correct 302 ms 19372 KB Output is correct
32 Correct 289 ms 19368 KB Output is correct
33 Correct 307 ms 19372 KB Output is correct
34 Execution timed out 2101 ms 51672 KB Time limit exceeded
35 Halted 0 ms 0 KB -