답안 #832879

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
832879 2023-08-21T16:13:26 Z vjudge1 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
433 ms 407996 KB
#include <bits/stdc++.h>
using namespace std;

/// 123

static const int N = 1e6 + 10;
class lca_t {
       public:
        int rmq[20][N * 2];
        int pos[N], d[N];
        vector<int> adj[N];
        int timer;
        int n;

        lca_t(int n = 0) : n(n) {}

        void add_edge(int u, int v) {
                adj[u].emplace_back(v);
                adj[v].emplace_back(u);
        }

        void dfs(int u, int p) {
                rmq[0][timer++] = u;
                for (int v : adj[u]) {
                        if (v == p) continue;
                        d[v] = d[u] + 1;
                        dfs(v, u);
                        rmq[0][timer++] = u;
                }
        }

        void build(int root) {
                timer = 0;
                d[root] = 0;
                dfs(root, -1);
                for (int i = 0; i < timer; i++) pos[rmq[0][i]] = i;
                assert(__lg(timer) < 20);
                for (int i = 1; i <= __lg(timer); i++) {
                        for (int j = 0; j + (1 << i) <= timer; j++) {
                                rmq[i][j] = d[rmq[i - 1][j]] < d[rmq[i - 1][j + (1 << (i - 1))]]
                                                ? rmq[i - 1][j]
                                                : rmq[i - 1][j + (1 << (i - 1))];
                        }
                }
        }

        int get(int u, int v) {
                int l = pos[u], r = pos[v];
                if (l > r) swap(l, r);
                int i = __lg(r - l + 1);
                r -= (1 << i) - 1;
                int x = d[rmq[i][l]] < d[rmq[i][r]] ? rmq[i][l] : rmq[i][r];
                return d[u] + d[v] - d[x] * 2;
        }

} lca;

int32_t main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) cin >> a[i];
        vector<vector<int>> adj(n);
        lca = lca_t(n);
        for (int i = 0; i < n - 1; i++) {
                int u, v;
                cin >> u >> v;
                u--, v--;
                if (a[u] < a[v]) swap(u, v);
                adj[u].emplace_back(v);
                lca.add_edge(u, v);
        }
        vector<int> ord(n);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](int i, int j) {
                return a[i] < a[j];
        });
        lca.build(0);
        vector<int> par(n, -1), m(n, -1);
        vector<int64_t> f(n, 0);
        for (int i = 0; i < n; i++) m[i] = i;
        function<int(int)> root = [&](int u) { return par[u] < 0 ? u : (par[u] = root(par[u])); };
        function<void(int, int)> unite = [&](int u, int v) {
                u = root(u), v = root(v);
                if (a[m[u]] < a[m[v]]) swap(u, v);
                int64_t ff = max(f[u], f[v] + lca.get(m[u], m[v]));
                if (par[u] > par[v]) swap(u, v);
                if (a[m[u]] < a[m[v]]) m[u] = m[v];
                par[u] += par[v];
                par[v] = u;
                f[u] = ff;
        };
        for (int i : ord) {
                for (int j : adj[i]) unite(i, j);
        }
        cout << f[root(0)];
}

Compilation message

Main.cpp: In function 'int32_t main()':
Main.cpp:7:7: warning: '<anonymous>.lca_t::timer' may be used uninitialized in this function [-Wmaybe-uninitialized]
    7 | class lca_t {
      |       ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 375976 KB Output is correct
2 Correct 142 ms 376008 KB Output is correct
3 Correct 146 ms 375964 KB Output is correct
4 Correct 149 ms 376012 KB Output is correct
5 Correct 166 ms 376000 KB Output is correct
6 Correct 142 ms 375936 KB Output is correct
7 Correct 147 ms 375952 KB Output is correct
8 Correct 140 ms 376020 KB Output is correct
9 Correct 144 ms 375964 KB Output is correct
10 Correct 143 ms 375928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 375976 KB Output is correct
2 Correct 142 ms 376008 KB Output is correct
3 Correct 146 ms 375964 KB Output is correct
4 Correct 149 ms 376012 KB Output is correct
5 Correct 166 ms 376000 KB Output is correct
6 Correct 142 ms 375936 KB Output is correct
7 Correct 147 ms 375952 KB Output is correct
8 Correct 140 ms 376020 KB Output is correct
9 Correct 144 ms 375964 KB Output is correct
10 Correct 143 ms 375928 KB Output is correct
11 Correct 142 ms 376024 KB Output is correct
12 Correct 157 ms 376044 KB Output is correct
13 Correct 141 ms 376056 KB Output is correct
14 Correct 140 ms 376020 KB Output is correct
15 Correct 147 ms 376024 KB Output is correct
16 Correct 153 ms 375972 KB Output is correct
17 Correct 140 ms 375996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 375976 KB Output is correct
2 Correct 142 ms 376008 KB Output is correct
3 Correct 146 ms 375964 KB Output is correct
4 Correct 149 ms 376012 KB Output is correct
5 Correct 166 ms 376000 KB Output is correct
6 Correct 142 ms 375936 KB Output is correct
7 Correct 147 ms 375952 KB Output is correct
8 Correct 140 ms 376020 KB Output is correct
9 Correct 144 ms 375964 KB Output is correct
10 Correct 143 ms 375928 KB Output is correct
11 Correct 142 ms 376024 KB Output is correct
12 Correct 157 ms 376044 KB Output is correct
13 Correct 141 ms 376056 KB Output is correct
14 Correct 140 ms 376020 KB Output is correct
15 Correct 147 ms 376024 KB Output is correct
16 Correct 153 ms 375972 KB Output is correct
17 Correct 140 ms 375996 KB Output is correct
18 Correct 151 ms 376800 KB Output is correct
19 Correct 149 ms 376712 KB Output is correct
20 Correct 146 ms 376736 KB Output is correct
21 Correct 144 ms 376720 KB Output is correct
22 Correct 144 ms 376636 KB Output is correct
23 Correct 149 ms 376660 KB Output is correct
24 Correct 150 ms 376748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 375976 KB Output is correct
2 Correct 142 ms 376008 KB Output is correct
3 Correct 146 ms 375964 KB Output is correct
4 Correct 149 ms 376012 KB Output is correct
5 Correct 166 ms 376000 KB Output is correct
6 Correct 142 ms 375936 KB Output is correct
7 Correct 147 ms 375952 KB Output is correct
8 Correct 140 ms 376020 KB Output is correct
9 Correct 144 ms 375964 KB Output is correct
10 Correct 143 ms 375928 KB Output is correct
11 Correct 142 ms 376024 KB Output is correct
12 Correct 157 ms 376044 KB Output is correct
13 Correct 141 ms 376056 KB Output is correct
14 Correct 140 ms 376020 KB Output is correct
15 Correct 147 ms 376024 KB Output is correct
16 Correct 153 ms 375972 KB Output is correct
17 Correct 140 ms 375996 KB Output is correct
18 Correct 151 ms 376800 KB Output is correct
19 Correct 149 ms 376712 KB Output is correct
20 Correct 146 ms 376736 KB Output is correct
21 Correct 144 ms 376720 KB Output is correct
22 Correct 144 ms 376636 KB Output is correct
23 Correct 149 ms 376660 KB Output is correct
24 Correct 150 ms 376748 KB Output is correct
25 Correct 147 ms 375932 KB Output is correct
26 Correct 159 ms 376680 KB Output is correct
27 Correct 156 ms 376708 KB Output is correct
28 Correct 156 ms 376608 KB Output is correct
29 Correct 161 ms 376752 KB Output is correct
30 Correct 149 ms 376500 KB Output is correct
31 Correct 158 ms 376508 KB Output is correct
32 Correct 174 ms 376528 KB Output is correct
33 Correct 176 ms 376572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 375976 KB Output is correct
2 Correct 142 ms 376008 KB Output is correct
3 Correct 146 ms 375964 KB Output is correct
4 Correct 149 ms 376012 KB Output is correct
5 Correct 166 ms 376000 KB Output is correct
6 Correct 142 ms 375936 KB Output is correct
7 Correct 147 ms 375952 KB Output is correct
8 Correct 140 ms 376020 KB Output is correct
9 Correct 144 ms 375964 KB Output is correct
10 Correct 143 ms 375928 KB Output is correct
11 Correct 142 ms 376024 KB Output is correct
12 Correct 157 ms 376044 KB Output is correct
13 Correct 141 ms 376056 KB Output is correct
14 Correct 140 ms 376020 KB Output is correct
15 Correct 147 ms 376024 KB Output is correct
16 Correct 153 ms 375972 KB Output is correct
17 Correct 140 ms 375996 KB Output is correct
18 Correct 151 ms 376800 KB Output is correct
19 Correct 149 ms 376712 KB Output is correct
20 Correct 146 ms 376736 KB Output is correct
21 Correct 144 ms 376720 KB Output is correct
22 Correct 144 ms 376636 KB Output is correct
23 Correct 149 ms 376660 KB Output is correct
24 Correct 150 ms 376748 KB Output is correct
25 Correct 259 ms 407996 KB Output is correct
26 Correct 235 ms 407380 KB Output is correct
27 Correct 231 ms 407436 KB Output is correct
28 Correct 282 ms 406032 KB Output is correct
29 Correct 275 ms 405916 KB Output is correct
30 Correct 277 ms 405840 KB Output is correct
31 Correct 279 ms 405848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 143 ms 375924 KB Output is correct
2 Correct 159 ms 376056 KB Output is correct
3 Correct 314 ms 399292 KB Output is correct
4 Correct 293 ms 399304 KB Output is correct
5 Correct 308 ms 399272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 375976 KB Output is correct
2 Correct 142 ms 376008 KB Output is correct
3 Correct 146 ms 375964 KB Output is correct
4 Correct 149 ms 376012 KB Output is correct
5 Correct 166 ms 376000 KB Output is correct
6 Correct 142 ms 375936 KB Output is correct
7 Correct 147 ms 375952 KB Output is correct
8 Correct 140 ms 376020 KB Output is correct
9 Correct 144 ms 375964 KB Output is correct
10 Correct 143 ms 375928 KB Output is correct
11 Correct 142 ms 376024 KB Output is correct
12 Correct 157 ms 376044 KB Output is correct
13 Correct 141 ms 376056 KB Output is correct
14 Correct 140 ms 376020 KB Output is correct
15 Correct 147 ms 376024 KB Output is correct
16 Correct 153 ms 375972 KB Output is correct
17 Correct 140 ms 375996 KB Output is correct
18 Correct 151 ms 376800 KB Output is correct
19 Correct 149 ms 376712 KB Output is correct
20 Correct 146 ms 376736 KB Output is correct
21 Correct 144 ms 376720 KB Output is correct
22 Correct 144 ms 376636 KB Output is correct
23 Correct 149 ms 376660 KB Output is correct
24 Correct 150 ms 376748 KB Output is correct
25 Correct 147 ms 375932 KB Output is correct
26 Correct 159 ms 376680 KB Output is correct
27 Correct 156 ms 376708 KB Output is correct
28 Correct 156 ms 376608 KB Output is correct
29 Correct 161 ms 376752 KB Output is correct
30 Correct 149 ms 376500 KB Output is correct
31 Correct 158 ms 376508 KB Output is correct
32 Correct 174 ms 376528 KB Output is correct
33 Correct 176 ms 376572 KB Output is correct
34 Correct 259 ms 407996 KB Output is correct
35 Correct 235 ms 407380 KB Output is correct
36 Correct 231 ms 407436 KB Output is correct
37 Correct 282 ms 406032 KB Output is correct
38 Correct 275 ms 405916 KB Output is correct
39 Correct 277 ms 405840 KB Output is correct
40 Correct 279 ms 405848 KB Output is correct
41 Correct 143 ms 375924 KB Output is correct
42 Correct 159 ms 376056 KB Output is correct
43 Correct 314 ms 399292 KB Output is correct
44 Correct 293 ms 399304 KB Output is correct
45 Correct 308 ms 399272 KB Output is correct
46 Correct 433 ms 404316 KB Output is correct
47 Correct 315 ms 403660 KB Output is correct
48 Correct 313 ms 404460 KB Output is correct
49 Correct 395 ms 403664 KB Output is correct
50 Correct 350 ms 399528 KB Output is correct
51 Correct 417 ms 399560 KB Output is correct
52 Correct 378 ms 399560 KB Output is correct
53 Correct 366 ms 399644 KB Output is correct