Submission #987653

# Submission time Handle Problem Language Result Execution time Memory
987653 2024-05-23T09:37:02 Z Otalp Cat Exercise (JOI23_ho_t4) C++14
41 / 100
305 ms 59780 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(i + (1 << k) - 1 > 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 6 ms 14424 KB Output is correct
2 Correct 4 ms 14428 KB Output is correct
3 Correct 4 ms 14428 KB Output is correct
4 Correct 4 ms 14428 KB Output is correct
5 Correct 4 ms 14428 KB Output is correct
6 Correct 3 ms 14424 KB Output is correct
7 Correct 3 ms 14424 KB Output is correct
8 Correct 5 ms 14428 KB Output is correct
9 Correct 4 ms 14428 KB Output is correct
10 Correct 4 ms 14292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 4 ms 14428 KB Output is correct
3 Correct 4 ms 14428 KB Output is correct
4 Correct 4 ms 14428 KB Output is correct
5 Correct 4 ms 14428 KB Output is correct
6 Correct 3 ms 14424 KB Output is correct
7 Correct 3 ms 14424 KB Output is correct
8 Correct 5 ms 14428 KB Output is correct
9 Correct 4 ms 14428 KB Output is correct
10 Correct 4 ms 14292 KB Output is correct
11 Correct 5 ms 14428 KB Output is correct
12 Correct 4 ms 14428 KB Output is correct
13 Correct 4 ms 14480 KB Output is correct
14 Correct 4 ms 14428 KB Output is correct
15 Correct 5 ms 14520 KB Output is correct
16 Correct 4 ms 14428 KB Output is correct
17 Correct 5 ms 14428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 4 ms 14428 KB Output is correct
3 Correct 4 ms 14428 KB Output is correct
4 Correct 4 ms 14428 KB Output is correct
5 Correct 4 ms 14428 KB Output is correct
6 Correct 3 ms 14424 KB Output is correct
7 Correct 3 ms 14424 KB Output is correct
8 Correct 5 ms 14428 KB Output is correct
9 Correct 4 ms 14428 KB Output is correct
10 Correct 4 ms 14292 KB Output is correct
11 Correct 5 ms 14428 KB Output is correct
12 Correct 4 ms 14428 KB Output is correct
13 Correct 4 ms 14480 KB Output is correct
14 Correct 4 ms 14428 KB Output is correct
15 Correct 5 ms 14520 KB Output is correct
16 Correct 4 ms 14428 KB Output is correct
17 Correct 5 ms 14428 KB Output is correct
18 Correct 8 ms 16988 KB Output is correct
19 Correct 8 ms 16908 KB Output is correct
20 Correct 8 ms 16984 KB Output is correct
21 Correct 9 ms 16988 KB Output is correct
22 Correct 8 ms 16988 KB Output is correct
23 Correct 7 ms 16988 KB Output is correct
24 Correct 7 ms 16988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 4 ms 14428 KB Output is correct
3 Correct 4 ms 14428 KB Output is correct
4 Correct 4 ms 14428 KB Output is correct
5 Correct 4 ms 14428 KB Output is correct
6 Correct 3 ms 14424 KB Output is correct
7 Correct 3 ms 14424 KB Output is correct
8 Correct 5 ms 14428 KB Output is correct
9 Correct 4 ms 14428 KB Output is correct
10 Correct 4 ms 14292 KB Output is correct
11 Correct 5 ms 14428 KB Output is correct
12 Correct 4 ms 14428 KB Output is correct
13 Correct 4 ms 14480 KB Output is correct
14 Correct 4 ms 14428 KB Output is correct
15 Correct 5 ms 14520 KB Output is correct
16 Correct 4 ms 14428 KB Output is correct
17 Correct 5 ms 14428 KB Output is correct
18 Correct 8 ms 16988 KB Output is correct
19 Correct 8 ms 16908 KB Output is correct
20 Correct 8 ms 16984 KB Output is correct
21 Correct 9 ms 16988 KB Output is correct
22 Correct 8 ms 16988 KB Output is correct
23 Correct 7 ms 16988 KB Output is correct
24 Correct 7 ms 16988 KB Output is correct
25 Correct 4 ms 14424 KB Output is correct
26 Incorrect 9 ms 16728 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 4 ms 14428 KB Output is correct
3 Correct 4 ms 14428 KB Output is correct
4 Correct 4 ms 14428 KB Output is correct
5 Correct 4 ms 14428 KB Output is correct
6 Correct 3 ms 14424 KB Output is correct
7 Correct 3 ms 14424 KB Output is correct
8 Correct 5 ms 14428 KB Output is correct
9 Correct 4 ms 14428 KB Output is correct
10 Correct 4 ms 14292 KB Output is correct
11 Correct 5 ms 14428 KB Output is correct
12 Correct 4 ms 14428 KB Output is correct
13 Correct 4 ms 14480 KB Output is correct
14 Correct 4 ms 14428 KB Output is correct
15 Correct 5 ms 14520 KB Output is correct
16 Correct 4 ms 14428 KB Output is correct
17 Correct 5 ms 14428 KB Output is correct
18 Correct 8 ms 16988 KB Output is correct
19 Correct 8 ms 16908 KB Output is correct
20 Correct 8 ms 16984 KB Output is correct
21 Correct 9 ms 16988 KB Output is correct
22 Correct 8 ms 16988 KB Output is correct
23 Correct 7 ms 16988 KB Output is correct
24 Correct 7 ms 16988 KB Output is correct
25 Correct 203 ms 59728 KB Output is correct
26 Correct 213 ms 59560 KB Output is correct
27 Correct 217 ms 59684 KB Output is correct
28 Correct 232 ms 59688 KB Output is correct
29 Correct 305 ms 59776 KB Output is correct
30 Correct 241 ms 59656 KB Output is correct
31 Correct 239 ms 59780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14428 KB Output is correct
2 Correct 4 ms 14428 KB Output is correct
3 Incorrect 299 ms 47152 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 4 ms 14428 KB Output is correct
3 Correct 4 ms 14428 KB Output is correct
4 Correct 4 ms 14428 KB Output is correct
5 Correct 4 ms 14428 KB Output is correct
6 Correct 3 ms 14424 KB Output is correct
7 Correct 3 ms 14424 KB Output is correct
8 Correct 5 ms 14428 KB Output is correct
9 Correct 4 ms 14428 KB Output is correct
10 Correct 4 ms 14292 KB Output is correct
11 Correct 5 ms 14428 KB Output is correct
12 Correct 4 ms 14428 KB Output is correct
13 Correct 4 ms 14480 KB Output is correct
14 Correct 4 ms 14428 KB Output is correct
15 Correct 5 ms 14520 KB Output is correct
16 Correct 4 ms 14428 KB Output is correct
17 Correct 5 ms 14428 KB Output is correct
18 Correct 8 ms 16988 KB Output is correct
19 Correct 8 ms 16908 KB Output is correct
20 Correct 8 ms 16984 KB Output is correct
21 Correct 9 ms 16988 KB Output is correct
22 Correct 8 ms 16988 KB Output is correct
23 Correct 7 ms 16988 KB Output is correct
24 Correct 7 ms 16988 KB Output is correct
25 Correct 4 ms 14424 KB Output is correct
26 Incorrect 9 ms 16728 KB Output isn't correct
27 Halted 0 ms 0 KB -