Submission #1076803

# Submission time Handle Problem Language Result Execution time Memory
1076803 2024-08-26T16:45:49 Z dwuy Cat Exercise (JOI23_ho_t4) C++14
100 / 100
128 ms 41720 KB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int MX = 200005;
int n;
int a[MX];
int e[MX];
int mx[MX];
int res[MX];
vector<int> G[MX];

void nhap(){
    cin >> n;
    for(int i=1; i<=n; i++) cin >> a[i];
    for(int i=1; i<n; i++){
        int u, v;
        cin >> u >> v;
        G[a[u]].push_back(a[v]);
        G[a[v]].push_back(a[u]);
    }
}

namespace HLD{
    int h[MX];
    int p[MX];
    int numC[MX];
    int heavy[MX];
    int head[MX];

    void dfs(int u){
        numC[u] = 1;
        h[u] = h[p[u]] + 1;
        for(int v: G[u]) if(v != p[u]){
            p[v] = u;
            dfs(v);
            numC[u] += numC[v];
            if(numC[v] > numC[heavy[u]]) heavy[u] = v;
        }
    }

    void decompose(int u, int he){
        head[u] = he;
        if(heavy[u]) decompose(heavy[u], he);
        for(int v: G[u]) if(v != heavy[u] && v != p[u]){
            decompose(v, v);
        }
    }

    int dis(int u, int v){
        int res = 0;
        while(head[u] != head[v]){
            if(h[head[u]] < h[head[v]]) swap(u, v);
            res += h[u] - h[head[u]] + 1;
            u = p[head[u]];
        }
        res += abs(h[u] - h[v]);
        return res;
    }

    void build(){
        dfs(1);
        decompose(1, 1);
    }
}

int fp(int u){
    return e[u] < 0? u : e[u] = fp(e[u]);
}

void solve(){
    HLD::build();

    memset(e, -1, sizeof e);
    memset(res, 0, sizeof res);
    for(int i=1; i<=n; i++) mx[i] = i;

    for(int u=1; u<=n; u++){
        int best = 0;
        for(int x: G[u]) if(x < u){
            int v = fp(x);
            best = max(best, res[v] + HLD::dis(u, mx[v]));
            int t = fp(u);
            if(e[t] > e[v]) swap(t, v);
            e[v] += e[t];
            e[t] = v;
        }
        int t = fp(u);
        res[t] = best;
        mx[t] = u;
    }
    cout << res[fp(n)];
}

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

    nhap();
    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 3 ms 8240 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8196 KB Output is correct
6 Correct 3 ms 8280 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 4 ms 8240 KB Output is correct
10 Correct 4 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 3 ms 8240 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8196 KB Output is correct
6 Correct 3 ms 8280 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 4 ms 8240 KB Output is correct
10 Correct 4 ms 8284 KB Output is correct
11 Correct 3 ms 8280 KB Output is correct
12 Correct 4 ms 8284 KB Output is correct
13 Correct 4 ms 8284 KB Output is correct
14 Correct 3 ms 8284 KB Output is correct
15 Correct 4 ms 8284 KB Output is correct
16 Correct 4 ms 8244 KB Output is correct
17 Correct 3 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 3 ms 8240 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8196 KB Output is correct
6 Correct 3 ms 8280 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 4 ms 8240 KB Output is correct
10 Correct 4 ms 8284 KB Output is correct
11 Correct 3 ms 8280 KB Output is correct
12 Correct 4 ms 8284 KB Output is correct
13 Correct 4 ms 8284 KB Output is correct
14 Correct 3 ms 8284 KB Output is correct
15 Correct 4 ms 8284 KB Output is correct
16 Correct 4 ms 8244 KB Output is correct
17 Correct 3 ms 8284 KB Output is correct
18 Correct 5 ms 8796 KB Output is correct
19 Correct 5 ms 9028 KB Output is correct
20 Correct 7 ms 9052 KB Output is correct
21 Correct 6 ms 8948 KB Output is correct
22 Correct 5 ms 9052 KB Output is correct
23 Correct 5 ms 8796 KB Output is correct
24 Correct 5 ms 9092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 3 ms 8240 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8196 KB Output is correct
6 Correct 3 ms 8280 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 4 ms 8240 KB Output is correct
10 Correct 4 ms 8284 KB Output is correct
11 Correct 3 ms 8280 KB Output is correct
12 Correct 4 ms 8284 KB Output is correct
13 Correct 4 ms 8284 KB Output is correct
14 Correct 3 ms 8284 KB Output is correct
15 Correct 4 ms 8284 KB Output is correct
16 Correct 4 ms 8244 KB Output is correct
17 Correct 3 ms 8284 KB Output is correct
18 Correct 5 ms 8796 KB Output is correct
19 Correct 5 ms 9028 KB Output is correct
20 Correct 7 ms 9052 KB Output is correct
21 Correct 6 ms 8948 KB Output is correct
22 Correct 5 ms 9052 KB Output is correct
23 Correct 5 ms 8796 KB Output is correct
24 Correct 5 ms 9092 KB Output is correct
25 Correct 4 ms 8284 KB Output is correct
26 Correct 5 ms 8992 KB Output is correct
27 Correct 5 ms 9052 KB Output is correct
28 Correct 5 ms 9052 KB Output is correct
29 Correct 5 ms 9052 KB Output is correct
30 Correct 6 ms 8796 KB Output is correct
31 Correct 6 ms 8792 KB Output is correct
32 Correct 6 ms 8752 KB Output is correct
33 Correct 6 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 3 ms 8240 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8196 KB Output is correct
6 Correct 3 ms 8280 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 4 ms 8240 KB Output is correct
10 Correct 4 ms 8284 KB Output is correct
11 Correct 3 ms 8280 KB Output is correct
12 Correct 4 ms 8284 KB Output is correct
13 Correct 4 ms 8284 KB Output is correct
14 Correct 3 ms 8284 KB Output is correct
15 Correct 4 ms 8284 KB Output is correct
16 Correct 4 ms 8244 KB Output is correct
17 Correct 3 ms 8284 KB Output is correct
18 Correct 5 ms 8796 KB Output is correct
19 Correct 5 ms 9028 KB Output is correct
20 Correct 7 ms 9052 KB Output is correct
21 Correct 6 ms 8948 KB Output is correct
22 Correct 5 ms 9052 KB Output is correct
23 Correct 5 ms 8796 KB Output is correct
24 Correct 5 ms 9092 KB Output is correct
25 Correct 67 ms 35408 KB Output is correct
26 Correct 78 ms 41552 KB Output is correct
27 Correct 65 ms 41720 KB Output is correct
28 Correct 99 ms 41036 KB Output is correct
29 Correct 85 ms 35416 KB Output is correct
30 Correct 84 ms 39504 KB Output is correct
31 Correct 87 ms 38228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 4 ms 8284 KB Output is correct
3 Correct 102 ms 30600 KB Output is correct
4 Correct 106 ms 30692 KB Output is correct
5 Correct 101 ms 30544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 3 ms 8240 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8196 KB Output is correct
6 Correct 3 ms 8280 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 4 ms 8240 KB Output is correct
10 Correct 4 ms 8284 KB Output is correct
11 Correct 3 ms 8280 KB Output is correct
12 Correct 4 ms 8284 KB Output is correct
13 Correct 4 ms 8284 KB Output is correct
14 Correct 3 ms 8284 KB Output is correct
15 Correct 4 ms 8284 KB Output is correct
16 Correct 4 ms 8244 KB Output is correct
17 Correct 3 ms 8284 KB Output is correct
18 Correct 5 ms 8796 KB Output is correct
19 Correct 5 ms 9028 KB Output is correct
20 Correct 7 ms 9052 KB Output is correct
21 Correct 6 ms 8948 KB Output is correct
22 Correct 5 ms 9052 KB Output is correct
23 Correct 5 ms 8796 KB Output is correct
24 Correct 5 ms 9092 KB Output is correct
25 Correct 4 ms 8284 KB Output is correct
26 Correct 5 ms 8992 KB Output is correct
27 Correct 5 ms 9052 KB Output is correct
28 Correct 5 ms 9052 KB Output is correct
29 Correct 5 ms 9052 KB Output is correct
30 Correct 6 ms 8796 KB Output is correct
31 Correct 6 ms 8792 KB Output is correct
32 Correct 6 ms 8752 KB Output is correct
33 Correct 6 ms 8796 KB Output is correct
34 Correct 67 ms 35408 KB Output is correct
35 Correct 78 ms 41552 KB Output is correct
36 Correct 65 ms 41720 KB Output is correct
37 Correct 99 ms 41036 KB Output is correct
38 Correct 85 ms 35416 KB Output is correct
39 Correct 84 ms 39504 KB Output is correct
40 Correct 87 ms 38228 KB Output is correct
41 Correct 3 ms 8284 KB Output is correct
42 Correct 4 ms 8284 KB Output is correct
43 Correct 102 ms 30600 KB Output is correct
44 Correct 106 ms 30692 KB Output is correct
45 Correct 101 ms 30544 KB Output is correct
46 Correct 81 ms 38508 KB Output is correct
47 Correct 81 ms 38480 KB Output is correct
48 Correct 78 ms 38460 KB Output is correct
49 Correct 83 ms 38520 KB Output is correct
50 Correct 112 ms 30292 KB Output is correct
51 Correct 128 ms 30296 KB Output is correct
52 Correct 112 ms 30288 KB Output is correct
53 Correct 112 ms 30288 KB Output is correct