Submission #884546

# Submission time Handle Problem Language Result Execution time Memory
884546 2023-12-07T16:20:14 Z wksp Cat Exercise (JOI23_ho_t4) C++14
100 / 100
207 ms 64460 KB
#include<bits/stdc++.h>

using namespace std;
const int n_max = 2 * 1e5 + 7;
const int max_log = 20;
int jump[max_log][n_max];

vector<int> graph[n_max];
int graph_up[n_max];
vector<int> graph_down[n_max];
long long deep[n_max];
int open[n_max];
int leader[n_max];
long long ans[n_max];
void init(int n){
    for(int i = 0; i    <= n; i++){
        leader[i] = i;
    }
}
void init_DFS(int v, int p = 0, int vallue = 1){
    graph_up[v] = p;
    deep[v] = vallue;
    for(auto u: graph[v]){
        if(u != p){
            init_DFS(u, v, vallue + 1);
            graph_down[v].push_back(u);
        }
    }
}
void init_jumps(int n){
    for(int i = 1; i <= n; i++){
        jump[0][i] = graph_up[i];
    }
    for(int i = 1; i < max_log; i++){
        for(int j = 1; j <= n; j++){
            jump[i][j] = jump[i-1][jump[i-1][j]];
        }
    }
}

int find(int v){
    if(v != leader[v]){
        leader[v] = find(leader[v]);
    }
    return leader[v];
}
long long unite_without_lca(int v, int u){
    u = find(u);
    leader[u] = v;
    return (deep[u] - deep[v]) + ans[u];
}
long long get_lca(int v, int u){
    if(deep[v] > deep[u]){
        int tmp = v;
        v = u;
        u = tmp;
    }
    long long number = (1<<19);
    for(int i = 19; i > -1; i--){
        if(deep[u] - deep[v] >= number){
            u = jump[i][u];
        }
        number /= 2;
    }
    if(u == v){
        return v;
    }
    for(int i = 19; i > -1; i--){
        if(jump[i][u] != jump[i][v]){
            u = jump[i][u];
            v = jump[i][v];
        }
    }
    return jump[0][v];
}
long long unite_with_lca(int v, int u){
    u = find(u);
    int lca = get_lca(v, u);
    leader[u] = v;
    return (deep[u] + deep[v] - 2 * deep[lca] + ans[u]);
}

void add_point(int v){
    ans[v] = 0;
    open[v] = 1;
    if(open[graph_up[v]] == 1){
        ans[v] = unite_with_lca(v, graph_up[v]); 
    }
    for(auto u: graph_down[v]){
        if(open[u] == 1){
            ans[v] = max(ans[v], unite_without_lca(v, u));
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    vector<pair<int, int>> tower_hight;
    int n;
    cin >> n;
    init(n);
    for(int i = 1; i <= n; i++){
        int p;
        cin >> p;
        tower_hight.push_back({p, i});
    }
    for(int i = 0; i < n - 1; i++){
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    init_DFS(1);
    init_jumps(n);
    sort(tower_hight.begin(), tower_hight.end());
    for(auto [p, id]: tower_hight){
        add_point(id);
    }
    cout << ans[tower_hight[tower_hight.size()- 1].second] << '\n';

    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:117:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  117 |     for(auto [p, id]: tower_hight){
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29020 KB Output is correct
2 Correct 5 ms 29160 KB Output is correct
3 Correct 4 ms 29020 KB Output is correct
4 Correct 4 ms 29116 KB Output is correct
5 Correct 4 ms 29020 KB Output is correct
6 Correct 4 ms 29020 KB Output is correct
7 Correct 5 ms 29428 KB Output is correct
8 Correct 4 ms 29016 KB Output is correct
9 Correct 5 ms 29020 KB Output is correct
10 Correct 6 ms 29028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29020 KB Output is correct
2 Correct 5 ms 29160 KB Output is correct
3 Correct 4 ms 29020 KB Output is correct
4 Correct 4 ms 29116 KB Output is correct
5 Correct 4 ms 29020 KB Output is correct
6 Correct 4 ms 29020 KB Output is correct
7 Correct 5 ms 29428 KB Output is correct
8 Correct 4 ms 29016 KB Output is correct
9 Correct 5 ms 29020 KB Output is correct
10 Correct 6 ms 29028 KB Output is correct
11 Correct 4 ms 29020 KB Output is correct
12 Correct 4 ms 29020 KB Output is correct
13 Correct 5 ms 29020 KB Output is correct
14 Correct 5 ms 29020 KB Output is correct
15 Correct 5 ms 29020 KB Output is correct
16 Correct 5 ms 29020 KB Output is correct
17 Correct 4 ms 29020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29020 KB Output is correct
2 Correct 5 ms 29160 KB Output is correct
3 Correct 4 ms 29020 KB Output is correct
4 Correct 4 ms 29116 KB Output is correct
5 Correct 4 ms 29020 KB Output is correct
6 Correct 4 ms 29020 KB Output is correct
7 Correct 5 ms 29428 KB Output is correct
8 Correct 4 ms 29016 KB Output is correct
9 Correct 5 ms 29020 KB Output is correct
10 Correct 6 ms 29028 KB Output is correct
11 Correct 4 ms 29020 KB Output is correct
12 Correct 4 ms 29020 KB Output is correct
13 Correct 5 ms 29020 KB Output is correct
14 Correct 5 ms 29020 KB Output is correct
15 Correct 5 ms 29020 KB Output is correct
16 Correct 5 ms 29020 KB Output is correct
17 Correct 4 ms 29020 KB Output is correct
18 Correct 7 ms 29784 KB Output is correct
19 Correct 7 ms 29788 KB Output is correct
20 Correct 7 ms 29784 KB Output is correct
21 Correct 6 ms 29864 KB Output is correct
22 Correct 6 ms 29788 KB Output is correct
23 Correct 6 ms 29896 KB Output is correct
24 Correct 6 ms 29788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29020 KB Output is correct
2 Correct 5 ms 29160 KB Output is correct
3 Correct 4 ms 29020 KB Output is correct
4 Correct 4 ms 29116 KB Output is correct
5 Correct 4 ms 29020 KB Output is correct
6 Correct 4 ms 29020 KB Output is correct
7 Correct 5 ms 29428 KB Output is correct
8 Correct 4 ms 29016 KB Output is correct
9 Correct 5 ms 29020 KB Output is correct
10 Correct 6 ms 29028 KB Output is correct
11 Correct 4 ms 29020 KB Output is correct
12 Correct 4 ms 29020 KB Output is correct
13 Correct 5 ms 29020 KB Output is correct
14 Correct 5 ms 29020 KB Output is correct
15 Correct 5 ms 29020 KB Output is correct
16 Correct 5 ms 29020 KB Output is correct
17 Correct 4 ms 29020 KB Output is correct
18 Correct 7 ms 29784 KB Output is correct
19 Correct 7 ms 29788 KB Output is correct
20 Correct 7 ms 29784 KB Output is correct
21 Correct 6 ms 29864 KB Output is correct
22 Correct 6 ms 29788 KB Output is correct
23 Correct 6 ms 29896 KB Output is correct
24 Correct 6 ms 29788 KB Output is correct
25 Correct 4 ms 29016 KB Output is correct
26 Correct 7 ms 29820 KB Output is correct
27 Correct 7 ms 29768 KB Output is correct
28 Correct 7 ms 29788 KB Output is correct
29 Correct 7 ms 29864 KB Output is correct
30 Correct 7 ms 29532 KB Output is correct
31 Correct 7 ms 29532 KB Output is correct
32 Correct 7 ms 29532 KB Output is correct
33 Correct 7 ms 29532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29020 KB Output is correct
2 Correct 5 ms 29160 KB Output is correct
3 Correct 4 ms 29020 KB Output is correct
4 Correct 4 ms 29116 KB Output is correct
5 Correct 4 ms 29020 KB Output is correct
6 Correct 4 ms 29020 KB Output is correct
7 Correct 5 ms 29428 KB Output is correct
8 Correct 4 ms 29016 KB Output is correct
9 Correct 5 ms 29020 KB Output is correct
10 Correct 6 ms 29028 KB Output is correct
11 Correct 4 ms 29020 KB Output is correct
12 Correct 4 ms 29020 KB Output is correct
13 Correct 5 ms 29020 KB Output is correct
14 Correct 5 ms 29020 KB Output is correct
15 Correct 5 ms 29020 KB Output is correct
16 Correct 5 ms 29020 KB Output is correct
17 Correct 4 ms 29020 KB Output is correct
18 Correct 7 ms 29784 KB Output is correct
19 Correct 7 ms 29788 KB Output is correct
20 Correct 7 ms 29784 KB Output is correct
21 Correct 6 ms 29864 KB Output is correct
22 Correct 6 ms 29788 KB Output is correct
23 Correct 6 ms 29896 KB Output is correct
24 Correct 6 ms 29788 KB Output is correct
25 Correct 120 ms 60620 KB Output is correct
26 Correct 120 ms 64404 KB Output is correct
27 Correct 96 ms 64460 KB Output is correct
28 Correct 116 ms 64456 KB Output is correct
29 Correct 124 ms 64424 KB Output is correct
30 Correct 135 ms 64308 KB Output is correct
31 Correct 115 ms 64452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 29020 KB Output is correct
2 Correct 4 ms 29020 KB Output is correct
3 Correct 123 ms 41876 KB Output is correct
4 Correct 121 ms 41700 KB Output is correct
5 Correct 133 ms 41924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29020 KB Output is correct
2 Correct 5 ms 29160 KB Output is correct
3 Correct 4 ms 29020 KB Output is correct
4 Correct 4 ms 29116 KB Output is correct
5 Correct 4 ms 29020 KB Output is correct
6 Correct 4 ms 29020 KB Output is correct
7 Correct 5 ms 29428 KB Output is correct
8 Correct 4 ms 29016 KB Output is correct
9 Correct 5 ms 29020 KB Output is correct
10 Correct 6 ms 29028 KB Output is correct
11 Correct 4 ms 29020 KB Output is correct
12 Correct 4 ms 29020 KB Output is correct
13 Correct 5 ms 29020 KB Output is correct
14 Correct 5 ms 29020 KB Output is correct
15 Correct 5 ms 29020 KB Output is correct
16 Correct 5 ms 29020 KB Output is correct
17 Correct 4 ms 29020 KB Output is correct
18 Correct 7 ms 29784 KB Output is correct
19 Correct 7 ms 29788 KB Output is correct
20 Correct 7 ms 29784 KB Output is correct
21 Correct 6 ms 29864 KB Output is correct
22 Correct 6 ms 29788 KB Output is correct
23 Correct 6 ms 29896 KB Output is correct
24 Correct 6 ms 29788 KB Output is correct
25 Correct 4 ms 29016 KB Output is correct
26 Correct 7 ms 29820 KB Output is correct
27 Correct 7 ms 29768 KB Output is correct
28 Correct 7 ms 29788 KB Output is correct
29 Correct 7 ms 29864 KB Output is correct
30 Correct 7 ms 29532 KB Output is correct
31 Correct 7 ms 29532 KB Output is correct
32 Correct 7 ms 29532 KB Output is correct
33 Correct 7 ms 29532 KB Output is correct
34 Correct 120 ms 60620 KB Output is correct
35 Correct 120 ms 64404 KB Output is correct
36 Correct 96 ms 64460 KB Output is correct
37 Correct 116 ms 64456 KB Output is correct
38 Correct 124 ms 64424 KB Output is correct
39 Correct 135 ms 64308 KB Output is correct
40 Correct 115 ms 64452 KB Output is correct
41 Correct 4 ms 29020 KB Output is correct
42 Correct 4 ms 29020 KB Output is correct
43 Correct 123 ms 41876 KB Output is correct
44 Correct 121 ms 41700 KB Output is correct
45 Correct 133 ms 41924 KB Output is correct
46 Correct 207 ms 56264 KB Output is correct
47 Correct 187 ms 55020 KB Output is correct
48 Correct 196 ms 57028 KB Output is correct
49 Correct 194 ms 54984 KB Output is correct
50 Correct 192 ms 46196 KB Output is correct
51 Correct 163 ms 46280 KB Output is correct
52 Correct 165 ms 46280 KB Output is correct
53 Correct 165 ms 46268 KB Output is correct