Submission #1022517

# Submission time Handle Problem Language Result Execution time Memory
1022517 2024-07-13T16:07:37 Z Aiperiii Cat Exercise (JOI23_ho_t4) C++14
100 / 100
312 ms 79192 KB
#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N=2e5+5;
int a[N],d[N],p[N],dp[N];
vector <int> g[N],g2[N];
int jmp[20][N];

void dfs(int v,int p){
    jmp[0][v]=p;
    for(auto to : g[v]){
        if(to!=p){
            d[to]=d[v]+1;
            dfs(to,v);
        }
    }
}

int lca(int v,int u){
    if(d[v]<d[u])swap(u,v);
    for(int k=19;k>=0;k--){
        if(d[jmp[k][v]]>=d[u]){
            v=jmp[k][v];
        }
    }
    if(u==v)return v;
    for(int k=19;k>=0;k--){
        if(jmp[k][v]!=jmp[k][u]){
            v=jmp[k][v];
            u=jmp[k][u];
        }
    }
    return jmp[0][v];
}
int find_set(int v){
    if(p[v]==v)return v;
    return p[v]=find_set(p[v]);
}
void union_set(int v,int u){
    u=find_set(u);
    p[u]=v;
    g2[v].pb(u);
}

int dist(int v,int u){
    int l=lca(u,v);
    return d[u]+d[v]-2*d[l];
}
void dfs2(int v,int p){
    for(auto to : g2[v]){
        if(to!=p){
            dfs2(to,v);
            dp[v]=max(dp[to]+dist(to,v),dp[v]);
        }
    }
}
signed main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        p[i]=i;
    }
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        g[a[u]].pb(a[v]);
        g[a[v]].pb(a[u]);
    }
    
    dfs(n,n);
    
    for(int k=1;k<20;k++){
        for(int i=1;i<=n;i++){
            jmp[k][i]=jmp[k-1][jmp[k-1][i]];
        }
    }
    
    for(int i=2;i<=n;i++){
        for(auto to : g[i]){
            if(to<i){
                union_set(i,to);
            }
        }
    }
    
    dfs2(n,0);
    cout<<dp[n]<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 6 ms 9820 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 3 ms 9992 KB Output is correct
5 Correct 5 ms 9836 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 3 ms 9820 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 6 ms 9820 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 3 ms 9992 KB Output is correct
5 Correct 5 ms 9836 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 3 ms 9820 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 10076 KB Output is correct
12 Correct 4 ms 10076 KB Output is correct
13 Correct 4 ms 10076 KB Output is correct
14 Correct 3 ms 10076 KB Output is correct
15 Correct 4 ms 9844 KB Output is correct
16 Correct 4 ms 10076 KB Output is correct
17 Correct 4 ms 10076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 6 ms 9820 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 3 ms 9992 KB Output is correct
5 Correct 5 ms 9836 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 3 ms 9820 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 10076 KB Output is correct
12 Correct 4 ms 10076 KB Output is correct
13 Correct 4 ms 10076 KB Output is correct
14 Correct 3 ms 10076 KB Output is correct
15 Correct 4 ms 9844 KB Output is correct
16 Correct 4 ms 10076 KB Output is correct
17 Correct 4 ms 10076 KB Output is correct
18 Correct 7 ms 11608 KB Output is correct
19 Correct 7 ms 11612 KB Output is correct
20 Correct 8 ms 11612 KB Output is correct
21 Correct 8 ms 11356 KB Output is correct
22 Correct 7 ms 11352 KB Output is correct
23 Correct 8 ms 11356 KB Output is correct
24 Correct 7 ms 11272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 6 ms 9820 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 3 ms 9992 KB Output is correct
5 Correct 5 ms 9836 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 3 ms 9820 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 10076 KB Output is correct
12 Correct 4 ms 10076 KB Output is correct
13 Correct 4 ms 10076 KB Output is correct
14 Correct 3 ms 10076 KB Output is correct
15 Correct 4 ms 9844 KB Output is correct
16 Correct 4 ms 10076 KB Output is correct
17 Correct 4 ms 10076 KB Output is correct
18 Correct 7 ms 11608 KB Output is correct
19 Correct 7 ms 11612 KB Output is correct
20 Correct 8 ms 11612 KB Output is correct
21 Correct 8 ms 11356 KB Output is correct
22 Correct 7 ms 11352 KB Output is correct
23 Correct 8 ms 11356 KB Output is correct
24 Correct 7 ms 11272 KB Output is correct
25 Correct 5 ms 9820 KB Output is correct
26 Correct 8 ms 11584 KB Output is correct
27 Correct 8 ms 11388 KB Output is correct
28 Correct 8 ms 11384 KB Output is correct
29 Correct 7 ms 11440 KB Output is correct
30 Correct 8 ms 11280 KB Output is correct
31 Correct 8 ms 11100 KB Output is correct
32 Correct 8 ms 11100 KB Output is correct
33 Correct 10 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 6 ms 9820 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 3 ms 9992 KB Output is correct
5 Correct 5 ms 9836 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 3 ms 9820 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 10076 KB Output is correct
12 Correct 4 ms 10076 KB Output is correct
13 Correct 4 ms 10076 KB Output is correct
14 Correct 3 ms 10076 KB Output is correct
15 Correct 4 ms 9844 KB Output is correct
16 Correct 4 ms 10076 KB Output is correct
17 Correct 4 ms 10076 KB Output is correct
18 Correct 7 ms 11608 KB Output is correct
19 Correct 7 ms 11612 KB Output is correct
20 Correct 8 ms 11612 KB Output is correct
21 Correct 8 ms 11356 KB Output is correct
22 Correct 7 ms 11352 KB Output is correct
23 Correct 8 ms 11356 KB Output is correct
24 Correct 7 ms 11272 KB Output is correct
25 Correct 184 ms 79192 KB Output is correct
26 Correct 191 ms 77016 KB Output is correct
27 Correct 193 ms 77140 KB Output is correct
28 Correct 300 ms 68628 KB Output is correct
29 Correct 312 ms 69720 KB Output is correct
30 Correct 285 ms 69456 KB Output is correct
31 Correct 311 ms 69972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 4 ms 9820 KB Output is correct
3 Correct 268 ms 64284 KB Output is correct
4 Correct 275 ms 64340 KB Output is correct
5 Correct 283 ms 64096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 6 ms 9820 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 3 ms 9992 KB Output is correct
5 Correct 5 ms 9836 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 3 ms 9820 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 10076 KB Output is correct
12 Correct 4 ms 10076 KB Output is correct
13 Correct 4 ms 10076 KB Output is correct
14 Correct 3 ms 10076 KB Output is correct
15 Correct 4 ms 9844 KB Output is correct
16 Correct 4 ms 10076 KB Output is correct
17 Correct 4 ms 10076 KB Output is correct
18 Correct 7 ms 11608 KB Output is correct
19 Correct 7 ms 11612 KB Output is correct
20 Correct 8 ms 11612 KB Output is correct
21 Correct 8 ms 11356 KB Output is correct
22 Correct 7 ms 11352 KB Output is correct
23 Correct 8 ms 11356 KB Output is correct
24 Correct 7 ms 11272 KB Output is correct
25 Correct 5 ms 9820 KB Output is correct
26 Correct 8 ms 11584 KB Output is correct
27 Correct 8 ms 11388 KB Output is correct
28 Correct 8 ms 11384 KB Output is correct
29 Correct 7 ms 11440 KB Output is correct
30 Correct 8 ms 11280 KB Output is correct
31 Correct 8 ms 11100 KB Output is correct
32 Correct 8 ms 11100 KB Output is correct
33 Correct 10 ms 11100 KB Output is correct
34 Correct 184 ms 79192 KB Output is correct
35 Correct 191 ms 77016 KB Output is correct
36 Correct 193 ms 77140 KB Output is correct
37 Correct 300 ms 68628 KB Output is correct
38 Correct 312 ms 69720 KB Output is correct
39 Correct 285 ms 69456 KB Output is correct
40 Correct 311 ms 69972 KB Output is correct
41 Correct 4 ms 9816 KB Output is correct
42 Correct 4 ms 9820 KB Output is correct
43 Correct 268 ms 64284 KB Output is correct
44 Correct 275 ms 64340 KB Output is correct
45 Correct 283 ms 64096 KB Output is correct
46 Correct 249 ms 74064 KB Output is correct
47 Correct 213 ms 74068 KB Output is correct
48 Correct 211 ms 74068 KB Output is correct
49 Correct 221 ms 74064 KB Output is correct
50 Correct 295 ms 62680 KB Output is correct
51 Correct 287 ms 62944 KB Output is correct
52 Correct 312 ms 63056 KB Output is correct
53 Correct 309 ms 63160 KB Output is correct