Submission #945440

# Submission time Handle Problem Language Result Execution time Memory
945440 2024-03-13T19:26:21 Z vladilius Cat Exercise (JOI23_ho_t4) C++17
100 / 100
433 ms 88956 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll

struct dsu{
    vector<int> sz, p, m, a;
    int n;
    dsu(int ns, vector<int>& as){
        a = as;
        n = ns;
        p.resize(n + 1);
        sz.resize(n + 1);
        m.resize(n + 1);
        for (int i = 1; i <= n; i++){
            p[i] = m[i] = i;
            sz[i] = 1;
        }
    }
    int get(int v){
        if (p[v] != v){
            p[v] = get(p[v]);
        }
        return p[v];
    }
    void unite(int x, int y){
        x = get(x); y = get(y);
        if (x == y) return;
        if (sz[x] > sz[y]){
            swap(x, y);
        }
        p[x] = y;
        sz[y] += sz[x];
        if (a[m[x]] > a[m[y]]){
            m[y] = m[x];
        }
    }
};

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n; cin>>n;
    vector<int> a(n + 1), p(n + 1);
    int s = 0;
    for (int i = 1; i <= n; i++){
        cin>>a[i];
        if (a[i] == n){
            s = i;
        }
        p[a[i]] = i;
    }
    vector<int> g[n + 1];
    for (int i = 1; i < n; i++){
        int u, v; cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dsu T(n + 1, a);
    
    vector<int> tin(n + 1), tout(n + 1), pw[n + 1], d(n + 1);
    int lg = ceil(log2(n)), timer = 0;
    for (int i = 1; i <= n; i++){
        pw[i].resize(lg);
    }
    function<void(int, int)> fill = [&](int v, int pr){
        tin[v] = timer++;
        pw[v][0] = pr;
        for (int i = 1; i < lg; i++){
            pw[v][i] = pw[pw[v][i - 1]][i - 1];
        }
        for (int i: g[v]){
            if (i == pr) continue;
            d[i] = d[v] + 1;
            fill(i, v);
        }
        tout[v] = timer++;
    };
    fill(1, 1);
    auto check = [&](int x, int y){
        return (tin[x] <= tin[y] && tout[x] >= tout[y]);
    };
    auto lca = [&](int x, int y){
        if (check(x, y)) return x;
        if (check(y, x)) return y;
        for (int i = lg - 1; i >= 0; i--){
            if (!check(pw[x][i], y)){
                x = pw[x][i];
            }
        }
        return pw[x][0];
    };
    auto dist = [&](int x, int y){
        return d[x] + d[y] - 2 * d[lca(x, y)];
    };
    vector<int> f(n + 1);
    for (int k = 1; k < n; k++){
        int i = p[k];
        for (int j: g[i]){
            if (a[j] > a[i]) continue;
            int v = T.m[T.get(j)];
            f[i] = max(f[i], f[v] + dist(i, v));
            T.unite(i, j);
        }
    }
    int out = 0;
    for (int i = 1; i <= n; i++){
        out = max(out, f[i] + dist(i, s));
    }
    cout<<out<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 3 ms 2140 KB Output is correct
19 Correct 3 ms 2140 KB Output is correct
20 Correct 3 ms 2140 KB Output is correct
21 Correct 3 ms 2140 KB Output is correct
22 Correct 3 ms 2140 KB Output is correct
23 Correct 3 ms 2136 KB Output is correct
24 Correct 3 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 3 ms 2140 KB Output is correct
19 Correct 3 ms 2140 KB Output is correct
20 Correct 3 ms 2140 KB Output is correct
21 Correct 3 ms 2140 KB Output is correct
22 Correct 3 ms 2140 KB Output is correct
23 Correct 3 ms 2136 KB Output is correct
24 Correct 3 ms 2140 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 4 ms 1884 KB Output is correct
27 Correct 4 ms 2140 KB Output is correct
28 Correct 3 ms 1884 KB Output is correct
29 Correct 3 ms 2140 KB Output is correct
30 Correct 3 ms 1628 KB Output is correct
31 Correct 3 ms 1628 KB Output is correct
32 Correct 3 ms 1628 KB Output is correct
33 Correct 3 ms 1820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 3 ms 2140 KB Output is correct
19 Correct 3 ms 2140 KB Output is correct
20 Correct 3 ms 2140 KB Output is correct
21 Correct 3 ms 2140 KB Output is correct
22 Correct 3 ms 2140 KB Output is correct
23 Correct 3 ms 2136 KB Output is correct
24 Correct 3 ms 2140 KB Output is correct
25 Correct 129 ms 84820 KB Output is correct
26 Correct 132 ms 88956 KB Output is correct
27 Correct 126 ms 88656 KB Output is correct
28 Correct 164 ms 88912 KB Output is correct
29 Correct 158 ms 88656 KB Output is correct
30 Correct 162 ms 88656 KB Output is correct
31 Correct 164 ms 88688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 185 ms 64752 KB Output is correct
4 Correct 173 ms 64596 KB Output is correct
5 Correct 174 ms 64608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 3 ms 2140 KB Output is correct
19 Correct 3 ms 2140 KB Output is correct
20 Correct 3 ms 2140 KB Output is correct
21 Correct 3 ms 2140 KB Output is correct
22 Correct 3 ms 2140 KB Output is correct
23 Correct 3 ms 2136 KB Output is correct
24 Correct 3 ms 2140 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 4 ms 1884 KB Output is correct
27 Correct 4 ms 2140 KB Output is correct
28 Correct 3 ms 1884 KB Output is correct
29 Correct 3 ms 2140 KB Output is correct
30 Correct 3 ms 1628 KB Output is correct
31 Correct 3 ms 1628 KB Output is correct
32 Correct 3 ms 1628 KB Output is correct
33 Correct 3 ms 1820 KB Output is correct
34 Correct 129 ms 84820 KB Output is correct
35 Correct 132 ms 88956 KB Output is correct
36 Correct 126 ms 88656 KB Output is correct
37 Correct 164 ms 88912 KB Output is correct
38 Correct 158 ms 88656 KB Output is correct
39 Correct 162 ms 88656 KB Output is correct
40 Correct 164 ms 88688 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 185 ms 64752 KB Output is correct
44 Correct 173 ms 64596 KB Output is correct
45 Correct 174 ms 64608 KB Output is correct
46 Correct 423 ms 79664 KB Output is correct
47 Correct 412 ms 77268 KB Output is correct
48 Correct 394 ms 80380 KB Output is correct
49 Correct 433 ms 77380 KB Output is correct
50 Correct 305 ms 67936 KB Output is correct
51 Correct 326 ms 67924 KB Output is correct
52 Correct 308 ms 67744 KB Output is correct
53 Correct 303 ms 67924 KB Output is correct