답안 #768564

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
768564 2023-06-28T09:07:49 Z green_gold_dog Cat Exercise (JOI23_ho_t4) C++17
31 / 100
2000 ms 85508 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][1];
        }
        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 dfs2(v, v, u, 0);
                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);
        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();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 216 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 216 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 1 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 216 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 1 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 76 ms 2424 KB Output is correct
19 Correct 50 ms 2388 KB Output is correct
20 Correct 50 ms 2444 KB Output is correct
21 Correct 42 ms 2392 KB Output is correct
22 Correct 38 ms 2388 KB Output is correct
23 Correct 40 ms 2388 KB Output is correct
24 Correct 39 ms 2428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 216 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 1 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 76 ms 2424 KB Output is correct
19 Correct 50 ms 2388 KB Output is correct
20 Correct 50 ms 2444 KB Output is correct
21 Correct 42 ms 2392 KB Output is correct
22 Correct 38 ms 2388 KB Output is correct
23 Correct 40 ms 2388 KB Output is correct
24 Correct 39 ms 2428 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 106 ms 2396 KB Output is correct
27 Correct 112 ms 2396 KB Output is correct
28 Correct 102 ms 2376 KB Output is correct
29 Correct 101 ms 2476 KB Output is correct
30 Correct 213 ms 2368 KB Output is correct
31 Correct 175 ms 2388 KB Output is correct
32 Correct 187 ms 2368 KB Output is correct
33 Correct 173 ms 2368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 216 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 1 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 76 ms 2424 KB Output is correct
19 Correct 50 ms 2388 KB Output is correct
20 Correct 50 ms 2444 KB Output is correct
21 Correct 42 ms 2392 KB Output is correct
22 Correct 38 ms 2388 KB Output is correct
23 Correct 40 ms 2388 KB Output is correct
24 Correct 39 ms 2428 KB Output is correct
25 Execution timed out 2079 ms 85508 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Execution timed out 2074 ms 83840 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 216 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 1 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 76 ms 2424 KB Output is correct
19 Correct 50 ms 2388 KB Output is correct
20 Correct 50 ms 2444 KB Output is correct
21 Correct 42 ms 2392 KB Output is correct
22 Correct 38 ms 2388 KB Output is correct
23 Correct 40 ms 2388 KB Output is correct
24 Correct 39 ms 2428 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 106 ms 2396 KB Output is correct
27 Correct 112 ms 2396 KB Output is correct
28 Correct 102 ms 2376 KB Output is correct
29 Correct 101 ms 2476 KB Output is correct
30 Correct 213 ms 2368 KB Output is correct
31 Correct 175 ms 2388 KB Output is correct
32 Correct 187 ms 2368 KB Output is correct
33 Correct 173 ms 2368 KB Output is correct
34 Execution timed out 2079 ms 85508 KB Time limit exceeded
35 Halted 0 ms 0 KB -