Submission #987666

# Submission time Handle Problem Language Result Execution time Memory
987666 2024-05-23T10:19:39 Z Otalp Cat Exercise (JOI23_ho_t4) C++14
100 / 100
414 ms 60076 KB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
 
 
 
vector<int> q[200100];
int p[200100], sz[200100];
ll mx[200100];
ll dp[200100];
int jmp[200100][30];
int tin[200100], tout[200100], timer = 0;
int h[200100];
int pos[200100];
int a[200100];
 
 
void dfs(int v, int p){
    jmp[v][0] = p;
    tin[v] = ++timer;
    for(int to: q[v]){
        if(to == p) continue;
        h[to] = h[v] + 1;
        dfs(to, v);
    }
    tout[v] = ++timer;
}
 
bool upper(int a, int b){
    if(a == 0) return 1;
    return tin[a] <= tin[b] and tout[a] >= tout[b];
}
 
int lca(int a, int b){
    if(upper(a, b)) return a;
    if(upper(b, a)) return b;
    for(int i=20; i>=0; i--){
        if(upper(jmp[a][i], b)) continue;
        a = jmp[a][i];
    }
    return jmp[a][0];
}
 
int dist(int a, int b){
    return h[a] + h[b] - 2 * h[lca(a, b)];
}
 
int get(int a){
    if(p[a] == a) return a;
    return p[a] = get(p[a]);
}
 
 
void un(int x, int y){
    x = get(x);
    y = get(y);
    if(x == y) return;
    if(sz[x] > sz[y]) swap(x, y);
    p[y] = p[x];
    sz[x] += sz[y];
    if(a[mx[x]] < a[mx[y]]) mx[x] = mx[y];
}
 
void solve(){
    int n;
    cin>>n;
    for(int i=1; i<=n; i++){
        cin>>a[i];
        p[i] = i;
        mx[i] = i;
        pos[a[i]] = i;
        sz[i] = 1;
        dp[i] = 0;
    }
    for(int i=1; i<n; i++){
        int l, r;
        cin>>l>>r;
        q[l].pb(r);
        q[r].pb(l);
    }
    dfs(1, 0);
    for(int k=1; k<=20; k++){
        for(int i=1; i<=n; i++){
            if((1 << k) > n) break;
            jmp[i][k] = jmp[jmp[i][k - 1]][k - 1];
        }
    }
    for(int i=1; i<=n; i++){
        vector<int> d;
        int v = pos[i];
        for(int to: q[v]){
            if(a[to] > i or get(to) == get(v)) continue;
            to = get(to);
            d.pb(mx[to]);
            un(to, v);
        }
        //cout<<"#################\n";
        //cout<<v<<'\n';
        dp[v] = 0;
        for(int h: d){
            //cout<<h<<' ';
            dp[v] = max(dp[v], dp[h] + dist(v, h));
        }
        //cout<<'\n';
        //cout<<dp[v]<<'\n';
    }
    cout<<dp[pos[n]]<<'\n';;
}
 
int main(){
    solve();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 4 ms 5212 KB Output is correct
15 Correct 3 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 4 ms 5212 KB Output is correct
15 Correct 3 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5208 KB Output is correct
18 Correct 7 ms 6296 KB Output is correct
19 Correct 7 ms 6492 KB Output is correct
20 Correct 7 ms 6488 KB Output is correct
21 Correct 7 ms 6492 KB Output is correct
22 Correct 8 ms 6492 KB Output is correct
23 Correct 8 ms 6492 KB Output is correct
24 Correct 8 ms 6488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 4 ms 5212 KB Output is correct
15 Correct 3 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5208 KB Output is correct
18 Correct 7 ms 6296 KB Output is correct
19 Correct 7 ms 6492 KB Output is correct
20 Correct 7 ms 6488 KB Output is correct
21 Correct 7 ms 6492 KB Output is correct
22 Correct 8 ms 6492 KB Output is correct
23 Correct 8 ms 6492 KB Output is correct
24 Correct 8 ms 6488 KB Output is correct
25 Correct 3 ms 4956 KB Output is correct
26 Correct 7 ms 6236 KB Output is correct
27 Correct 7 ms 6380 KB Output is correct
28 Correct 7 ms 6232 KB Output is correct
29 Correct 8 ms 6236 KB Output is correct
30 Correct 8 ms 5976 KB Output is correct
31 Correct 7 ms 5980 KB Output is correct
32 Correct 7 ms 5980 KB Output is correct
33 Correct 7 ms 6232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 4 ms 5212 KB Output is correct
15 Correct 3 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5208 KB Output is correct
18 Correct 7 ms 6296 KB Output is correct
19 Correct 7 ms 6492 KB Output is correct
20 Correct 7 ms 6488 KB Output is correct
21 Correct 7 ms 6492 KB Output is correct
22 Correct 8 ms 6492 KB Output is correct
23 Correct 8 ms 6492 KB Output is correct
24 Correct 8 ms 6488 KB Output is correct
25 Correct 216 ms 59756 KB Output is correct
26 Correct 228 ms 59732 KB Output is correct
27 Correct 224 ms 60076 KB Output is correct
28 Correct 276 ms 59640 KB Output is correct
29 Correct 258 ms 59728 KB Output is correct
30 Correct 256 ms 59732 KB Output is correct
31 Correct 248 ms 59728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 312 ms 47024 KB Output is correct
4 Correct 286 ms 46932 KB Output is correct
5 Correct 286 ms 47488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 4 ms 5212 KB Output is correct
15 Correct 3 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5208 KB Output is correct
18 Correct 7 ms 6296 KB Output is correct
19 Correct 7 ms 6492 KB Output is correct
20 Correct 7 ms 6488 KB Output is correct
21 Correct 7 ms 6492 KB Output is correct
22 Correct 8 ms 6492 KB Output is correct
23 Correct 8 ms 6492 KB Output is correct
24 Correct 8 ms 6488 KB Output is correct
25 Correct 3 ms 4956 KB Output is correct
26 Correct 7 ms 6236 KB Output is correct
27 Correct 7 ms 6380 KB Output is correct
28 Correct 7 ms 6232 KB Output is correct
29 Correct 8 ms 6236 KB Output is correct
30 Correct 8 ms 5976 KB Output is correct
31 Correct 7 ms 5980 KB Output is correct
32 Correct 7 ms 5980 KB Output is correct
33 Correct 7 ms 6232 KB Output is correct
34 Correct 216 ms 59756 KB Output is correct
35 Correct 228 ms 59732 KB Output is correct
36 Correct 224 ms 60076 KB Output is correct
37 Correct 276 ms 59640 KB Output is correct
38 Correct 258 ms 59728 KB Output is correct
39 Correct 256 ms 59732 KB Output is correct
40 Correct 248 ms 59728 KB Output is correct
41 Correct 3 ms 4956 KB Output is correct
42 Correct 3 ms 5212 KB Output is correct
43 Correct 312 ms 47024 KB Output is correct
44 Correct 286 ms 46932 KB Output is correct
45 Correct 286 ms 47488 KB Output is correct
46 Correct 343 ms 54172 KB Output is correct
47 Correct 414 ms 52884 KB Output is correct
48 Correct 346 ms 54540 KB Output is correct
49 Correct 386 ms 53068 KB Output is correct
50 Correct 331 ms 47336 KB Output is correct
51 Correct 363 ms 47360 KB Output is correct
52 Correct 345 ms 47324 KB Output is correct
53 Correct 380 ms 47188 KB Output is correct