Submission #768578

# Submission time Handle Problem Language Result Execution time Memory
768578 2023-06-28T09:20:07 Z green_gold_dog Cat Exercise (JOI23_ho_t4) C++17
100 / 100
476 ms 86988 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll LOG = 20;

template<typename T>
bool assign_min(T& a, T b) {
        if (a > b) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
bool assign_max(T& a, T b) {
        if (a < b) {
                a = b;
                return true;
        }
        return false;
}

struct DSU {
        vector<ll> p;
        DSU(ll n) {
                p.resize(n, 0);
                for (ll i = 0; i < n; i++) {
                        p[i] = i;
                }
        }
        ll get(ll x) {
                return (p[x] == x ? x : p[x] = get(p[x]));
        }
        void unite(ll a, ll b) {
                p[b] = a;
        }
};

struct LCA {
        vector<vector<ll>> tree;
        vector<ll> h;
        vector<vector<ll>> binup;
        LCA(vector<vector<ll>> tree): tree(tree) {
                ll n = tree.size();
                h.resize(n, -1);
                binup.resize(n, vector<ll>(LOG, 0));
                dfs(0, 0);
        }
        void dfs(ll v, ll p) {
                h[v] = h[p] + 1;
                binup[v][0] = p;
                for (ll i = 1; i < LOG; i++) {
                        binup[v][i] = binup[binup[v][i - 1]][i - 1];
                }
                for (auto i : tree[v]) {
                        if (i != p) {
                                dfs(i, v);
                        }
                }
        }
        ll la(ll v, ll x) {
                for (ll i = 0; i < LOG; i++) {
                        if ((x >> i) & 1) {
                                v = binup[v][i];
                        }
                }
                return v;
        }
        ll lca(ll v, ll u) {
                if (h[v] < h[u]) {
                        swap(v, u);
                }
                v = la(v, h[v] - h[u]);
                if (v == u) {
                        return v;
                }
                for (ll i = LOG - 1; i >= 0; i--) {
                        if (binup[v][i] != binup[u][i]) {
                                v = binup[v][i];
                                u = binup[u][i];
                        }
                }
                return binup[v][0];
        }
        ll dfs2(ll v, ll p, ll u, ll x) {
                if (v == u) {
                        return x;
                }
                for (auto i : tree[v]) {
                        if (i != p) {
                                ll a = dfs2(i, v, u, x + 1);
                                if (a != -1) {
                                        return a;
                                }
                        }
                }
                return -1;
        }
        ll dist(ll v, ll u) {
                return h[v] + h[u] - h[lca(v, u)] * 2;
        }
};

void solve() {
        ll n;
        cin >> n;
        vector<pair<ll, ll>> arr(n);
        for (ll i = 0; i < n; i++) {
                cin >> arr[i].first;
                arr[i].second = i;
        }
        sort(arr.begin(), arr.end());
        vector<vector<ll>> tree(n);
        for (ll i = 1; i < n; i++) {
                ll a, b;
                cin >> a >> b;
                a--;
                b--;
                tree[a].push_back(b);
                tree[b].push_back(a);
        }
        DSU d(n);
        LCA l(tree);
        l.dist(3, 4);
        vector<ll> dp(n, -1);
        ll ans = 0;
        for (auto[_, i] : arr) {
                dp[i] = 0;
                for (auto j : tree[i]) {
                        if (dp[j] >= 0) {
                                ll x = d.get(j);
                                assign_max(dp[i], dp[x] + l.dist(i, x));
                                d.unite(i, x);
                        }
                }
                ans = dp[i];
        }
        cout << ans << '\n';
}

int main() {
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 5 ms 2388 KB Output is correct
19 Correct 4 ms 2388 KB Output is correct
20 Correct 4 ms 2388 KB Output is correct
21 Correct 4 ms 2388 KB Output is correct
22 Correct 4 ms 2388 KB Output is correct
23 Correct 4 ms 2388 KB Output is correct
24 Correct 4 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 5 ms 2388 KB Output is correct
19 Correct 4 ms 2388 KB Output is correct
20 Correct 4 ms 2388 KB Output is correct
21 Correct 4 ms 2388 KB Output is correct
22 Correct 4 ms 2388 KB Output is correct
23 Correct 4 ms 2388 KB Output is correct
24 Correct 4 ms 2388 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 4 ms 2260 KB Output is correct
27 Correct 4 ms 2396 KB Output is correct
28 Correct 4 ms 2392 KB Output is correct
29 Correct 4 ms 2384 KB Output is correct
30 Correct 4 ms 2388 KB Output is correct
31 Correct 5 ms 2352 KB Output is correct
32 Correct 4 ms 2260 KB Output is correct
33 Correct 4 ms 2264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 5 ms 2388 KB Output is correct
19 Correct 4 ms 2388 KB Output is correct
20 Correct 4 ms 2388 KB Output is correct
21 Correct 4 ms 2388 KB Output is correct
22 Correct 4 ms 2388 KB Output is correct
23 Correct 4 ms 2388 KB Output is correct
24 Correct 4 ms 2388 KB Output is correct
25 Correct 175 ms 85504 KB Output is correct
26 Correct 171 ms 85460 KB Output is correct
27 Correct 164 ms 85516 KB Output is correct
28 Correct 210 ms 85552 KB Output is correct
29 Correct 233 ms 85456 KB Output is correct
30 Correct 205 ms 85500 KB Output is correct
31 Correct 218 ms 85452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 228 ms 83732 KB Output is correct
4 Correct 228 ms 83832 KB Output is correct
5 Correct 267 ms 83768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 5 ms 2388 KB Output is correct
19 Correct 4 ms 2388 KB Output is correct
20 Correct 4 ms 2388 KB Output is correct
21 Correct 4 ms 2388 KB Output is correct
22 Correct 4 ms 2388 KB Output is correct
23 Correct 4 ms 2388 KB Output is correct
24 Correct 4 ms 2388 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 4 ms 2260 KB Output is correct
27 Correct 4 ms 2396 KB Output is correct
28 Correct 4 ms 2392 KB Output is correct
29 Correct 4 ms 2384 KB Output is correct
30 Correct 4 ms 2388 KB Output is correct
31 Correct 5 ms 2352 KB Output is correct
32 Correct 4 ms 2260 KB Output is correct
33 Correct 4 ms 2264 KB Output is correct
34 Correct 175 ms 85504 KB Output is correct
35 Correct 171 ms 85460 KB Output is correct
36 Correct 164 ms 85516 KB Output is correct
37 Correct 210 ms 85552 KB Output is correct
38 Correct 233 ms 85456 KB Output is correct
39 Correct 205 ms 85500 KB Output is correct
40 Correct 218 ms 85452 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 228 ms 83732 KB Output is correct
44 Correct 228 ms 83832 KB Output is correct
45 Correct 267 ms 83768 KB Output is correct
46 Correct 396 ms 86768 KB Output is correct
47 Correct 410 ms 86188 KB Output is correct
48 Correct 399 ms 86988 KB Output is correct
49 Correct 476 ms 86124 KB Output is correct
50 Correct 353 ms 83952 KB Output is correct
51 Correct 349 ms 84072 KB Output is correct
52 Correct 365 ms 84080 KB Output is correct
53 Correct 345 ms 83968 KB Output is correct