답안 #1107435

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1107435 2024-11-01T08:37:26 Z Zero_OP Cat Exercise (JOI23_ho_t4) C++14
100 / 100
170 ms 54468 KB
#include <bits/stdc++.h>

using namespace std;

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

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

#ifdef LOCAL
    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);
#endif // LOCAL

    struct DisjointSet{
        vector<int> lab, mx;
        DisjointSet(int n) : lab(n, -1), mx(n) {
            for(int i = 0; i < n; ++i) mx[i] = i;
        }

        int root(int u){
            return lab[u] < 0 ? u : (lab[u] = root(lab[u]));
        }

        bool unite(int u, int v){
            u = root(u);
            v = root(v);
            if(u == v) return false;
            if(lab[u] > lab[v]) swap(u, v);
            lab[u] += lab[v];
            lab[v] = u;
            maximize(mx[u], mx[v]);
            return true;
        }

        int maxLabel(int u){
            return mx[root(u)];
        }
    };

    int N;
    cin >> N;
    vector<int> P(N);
    for(int i = 0; i < N; ++i) cin >> P[i], --P[i];

    vector<vector<int>> adj(N);
    for(int i = 1; i < N; ++i){
        int u, v;
        cin >> u >> v;
        --u, --v;
        u = P[u];
        v = P[v];
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    int timerdfs = 0;
    vector<int> depth(N);
    vector<int> tin(N), tout(N);
    vector<vector<int>> lift(32 - __builtin_clz(N), vector<int>(N));

    function<void(int, int)> dfs = [&](int u, int pre){
        tin[u] = ++timerdfs;
        for(int v : adj[u]) if(v != pre){
            depth[v] = depth[u] + 1;
            lift[0][v] = u;
            for(int i = 1; (1 << i) <= depth[v]; ++i)
                lift[i][v] = lift[i - 1][lift[i - 1][v]];
            dfs(v, u);
        }
        tout[u] = timerdfs;
    };

    auto inside = [&](int u, int v) -> bool{
        return tin[u] <= tin[v] && tout[v] <= tout[u];
    };

    auto getLCA = [&](int u, int v) -> int{
        if(inside(u, v)) return u;
        if(inside(v, u)) return v;
        for(int i = 31 - __builtin_clz(depth[u]); i >= 0; --i){
            if(depth[u] >= (1 << i) && !inside(lift[i][u], v)){
                u = lift[i][u];
            }
        }
        return lift[0][u];
    };

    auto distance = [&](int u, int v) -> int{
        return depth[u] + depth[v] - 2 * depth[getLCA(u, v)];
    };


    dfs(N - 1, -1);
    DisjointSet dsu(N);

    vector<long long> dp(N, 0);

    for(int i = 1; i < N; ++i){
        for(int j : adj[i]) if(j < i){
            int mx = dsu.maxLabel(j);
            maximize(dp[i], dp[mx] + distance(mx, i));
            dsu.unite(i, j);
        }
    }

    cout << dp[N - 1] << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 508 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 508 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 3 ms 1616 KB Output is correct
19 Correct 3 ms 1616 KB Output is correct
20 Correct 3 ms 1616 KB Output is correct
21 Correct 3 ms 1360 KB Output is correct
22 Correct 3 ms 1360 KB Output is correct
23 Correct 3 ms 1360 KB Output is correct
24 Correct 3 ms 1360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 508 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 3 ms 1616 KB Output is correct
19 Correct 3 ms 1616 KB Output is correct
20 Correct 3 ms 1616 KB Output is correct
21 Correct 3 ms 1360 KB Output is correct
22 Correct 3 ms 1360 KB Output is correct
23 Correct 3 ms 1360 KB Output is correct
24 Correct 3 ms 1360 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 3 ms 1360 KB Output is correct
27 Correct 3 ms 1360 KB Output is correct
28 Correct 3 ms 1360 KB Output is correct
29 Correct 3 ms 1360 KB Output is correct
30 Correct 3 ms 1104 KB Output is correct
31 Correct 4 ms 992 KB Output is correct
32 Correct 3 ms 1104 KB Output is correct
33 Correct 2 ms 1104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 508 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 3 ms 1616 KB Output is correct
19 Correct 3 ms 1616 KB Output is correct
20 Correct 3 ms 1616 KB Output is correct
21 Correct 3 ms 1360 KB Output is correct
22 Correct 3 ms 1360 KB Output is correct
23 Correct 3 ms 1360 KB Output is correct
24 Correct 3 ms 1360 KB Output is correct
25 Correct 115 ms 50488 KB Output is correct
26 Correct 130 ms 54468 KB Output is correct
27 Correct 104 ms 54292 KB Output is correct
28 Correct 137 ms 49628 KB Output is correct
29 Correct 155 ms 52264 KB Output is correct
30 Correct 133 ms 51388 KB Output is correct
31 Correct 159 ms 52280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 552 KB Output is correct
3 Correct 113 ms 31800 KB Output is correct
4 Correct 101 ms 31644 KB Output is correct
5 Correct 120 ms 31800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 508 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 3 ms 1616 KB Output is correct
19 Correct 3 ms 1616 KB Output is correct
20 Correct 3 ms 1616 KB Output is correct
21 Correct 3 ms 1360 KB Output is correct
22 Correct 3 ms 1360 KB Output is correct
23 Correct 3 ms 1360 KB Output is correct
24 Correct 3 ms 1360 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 3 ms 1360 KB Output is correct
27 Correct 3 ms 1360 KB Output is correct
28 Correct 3 ms 1360 KB Output is correct
29 Correct 3 ms 1360 KB Output is correct
30 Correct 3 ms 1104 KB Output is correct
31 Correct 4 ms 992 KB Output is correct
32 Correct 3 ms 1104 KB Output is correct
33 Correct 2 ms 1104 KB Output is correct
34 Correct 115 ms 50488 KB Output is correct
35 Correct 130 ms 54468 KB Output is correct
36 Correct 104 ms 54292 KB Output is correct
37 Correct 137 ms 49628 KB Output is correct
38 Correct 155 ms 52264 KB Output is correct
39 Correct 133 ms 51388 KB Output is correct
40 Correct 159 ms 52280 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 552 KB Output is correct
43 Correct 113 ms 31800 KB Output is correct
44 Correct 101 ms 31644 KB Output is correct
45 Correct 120 ms 31800 KB Output is correct
46 Correct 123 ms 48660 KB Output is correct
47 Correct 132 ms 48548 KB Output is correct
48 Correct 134 ms 50404 KB Output is correct
49 Correct 131 ms 48696 KB Output is correct
50 Correct 157 ms 35636 KB Output is correct
51 Correct 142 ms 35508 KB Output is correct
52 Correct 159 ms 35496 KB Output is correct
53 Correct 170 ms 35676 KB Output is correct