Submission #1092676

# Submission time Handle Problem Language Result Execution time Memory
1092676 2024-09-24T18:02:37 Z Trisanu_Das Cat Exercise (JOI23_ho_t4) C++17
100 / 100
127 ms 41736 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;

int n;
int a[N], pa[N], sz[N], hv[N], lv[N], hd[N], ord[N], fa[N];
vector<int> adj[N];
long long dp[N];
 
void dfs(int u,int p = 0){
    pa[u] = p;
    sz[u] = 1;
    lv[u] = lv[p] + 1;
    for(auto v : adj[u]) 
        if(v != p){
            dfs(v,u);
            sz[u]+=sz[v];
            if(sz[v]>sz[hv[u]])hv[u]=v;
        }
}
 
void hld(int u,int p=0){
    if(!hd[u])hd[u]=u;
    if(hv[u])hd[hv[u]]=hd[u],hld(hv[u],u);
    for(auto v:adj[u])if(v!=p&&v!=hv[u])hld(v,u);
}
 
int lca(int u,int v){
    while(hd[u]!=hd[v]){
        if(lv[hd[u]]<lv[hd[v]])swap(u,v);
        u=pa[hd[u]];
    }
    return lv[u]<lv[v]?u:v;
}
 
int dist(int u,int v){
    return lv[u]+lv[v]-2*lv[lca(u,v)];
}
 
int fp(int u){
    return fa[u]=fa[u]==u?u:fp(fa[u]);
}
 
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    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;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }
    dfs(1);
    hld(1);
    iota(fa+1,fa+n+1,1);
    iota(ord+1,ord+n+1,1);
    sort(ord+1,ord+n+1,[&](int i,int j){
        return a[i]<a[j];
    });
    for(int i=1;i<=n;i++){
        int u=ord[i];
        for(auto v:adj[u])if(a[u]>a[v]){
            v=fp(v);
            dp[u]=max(dp[u],dp[v]+dist(u,v));
            fa[v]=u;
        }
    }
    cout << dp[ord[n]];
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 4996 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 3 ms 5172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 4996 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 3 ms 5172 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5220 KB Output is correct
13 Correct 4 ms 5216 KB Output is correct
14 Correct 2 ms 5164 KB Output is correct
15 Correct 2 ms 5216 KB Output is correct
16 Correct 2 ms 5168 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 4996 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 3 ms 5172 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5220 KB Output is correct
13 Correct 4 ms 5216 KB Output is correct
14 Correct 2 ms 5164 KB Output is correct
15 Correct 2 ms 5216 KB Output is correct
16 Correct 2 ms 5168 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 3 ms 5980 KB Output is correct
19 Correct 4 ms 5984 KB Output is correct
20 Correct 3 ms 5984 KB Output is correct
21 Correct 4 ms 5984 KB Output is correct
22 Correct 4 ms 5984 KB Output is correct
23 Correct 4 ms 5984 KB Output is correct
24 Correct 4 ms 6040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 4996 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 3 ms 5172 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5220 KB Output is correct
13 Correct 4 ms 5216 KB Output is correct
14 Correct 2 ms 5164 KB Output is correct
15 Correct 2 ms 5216 KB Output is correct
16 Correct 2 ms 5168 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 3 ms 5980 KB Output is correct
19 Correct 4 ms 5984 KB Output is correct
20 Correct 3 ms 5984 KB Output is correct
21 Correct 4 ms 5984 KB Output is correct
22 Correct 4 ms 5984 KB Output is correct
23 Correct 4 ms 5984 KB Output is correct
24 Correct 4 ms 6040 KB Output is correct
25 Correct 2 ms 5216 KB Output is correct
26 Correct 4 ms 5728 KB Output is correct
27 Correct 4 ms 5984 KB Output is correct
28 Correct 5 ms 5732 KB Output is correct
29 Correct 4 ms 5980 KB Output is correct
30 Correct 4 ms 5700 KB Output is correct
31 Correct 4 ms 5732 KB Output is correct
32 Correct 4 ms 5736 KB Output is correct
33 Correct 4 ms 5736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 4996 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 3 ms 5172 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5220 KB Output is correct
13 Correct 4 ms 5216 KB Output is correct
14 Correct 2 ms 5164 KB Output is correct
15 Correct 2 ms 5216 KB Output is correct
16 Correct 2 ms 5168 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 3 ms 5980 KB Output is correct
19 Correct 4 ms 5984 KB Output is correct
20 Correct 3 ms 5984 KB Output is correct
21 Correct 4 ms 5984 KB Output is correct
22 Correct 4 ms 5984 KB Output is correct
23 Correct 4 ms 5984 KB Output is correct
24 Correct 4 ms 6040 KB Output is correct
25 Correct 76 ms 41556 KB Output is correct
26 Correct 76 ms 41568 KB Output is correct
27 Correct 79 ms 41568 KB Output is correct
28 Correct 87 ms 41736 KB Output is correct
29 Correct 88 ms 41576 KB Output is correct
30 Correct 88 ms 41560 KB Output is correct
31 Correct 90 ms 41552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 91 ms 29780 KB Output is correct
4 Correct 91 ms 29780 KB Output is correct
5 Correct 97 ms 29820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 4996 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 3 ms 5172 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5220 KB Output is correct
13 Correct 4 ms 5216 KB Output is correct
14 Correct 2 ms 5164 KB Output is correct
15 Correct 2 ms 5216 KB Output is correct
16 Correct 2 ms 5168 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 3 ms 5980 KB Output is correct
19 Correct 4 ms 5984 KB Output is correct
20 Correct 3 ms 5984 KB Output is correct
21 Correct 4 ms 5984 KB Output is correct
22 Correct 4 ms 5984 KB Output is correct
23 Correct 4 ms 5984 KB Output is correct
24 Correct 4 ms 6040 KB Output is correct
25 Correct 2 ms 5216 KB Output is correct
26 Correct 4 ms 5728 KB Output is correct
27 Correct 4 ms 5984 KB Output is correct
28 Correct 5 ms 5732 KB Output is correct
29 Correct 4 ms 5980 KB Output is correct
30 Correct 4 ms 5700 KB Output is correct
31 Correct 4 ms 5732 KB Output is correct
32 Correct 4 ms 5736 KB Output is correct
33 Correct 4 ms 5736 KB Output is correct
34 Correct 76 ms 41556 KB Output is correct
35 Correct 76 ms 41568 KB Output is correct
36 Correct 79 ms 41568 KB Output is correct
37 Correct 87 ms 41736 KB Output is correct
38 Correct 88 ms 41576 KB Output is correct
39 Correct 88 ms 41560 KB Output is correct
40 Correct 90 ms 41552 KB Output is correct
41 Correct 2 ms 5212 KB Output is correct
42 Correct 2 ms 5212 KB Output is correct
43 Correct 91 ms 29780 KB Output is correct
44 Correct 91 ms 29780 KB Output is correct
45 Correct 97 ms 29820 KB Output is correct
46 Correct 101 ms 36428 KB Output is correct
47 Correct 117 ms 35408 KB Output is correct
48 Correct 103 ms 37204 KB Output is correct
49 Correct 101 ms 35408 KB Output is correct
50 Correct 126 ms 30292 KB Output is correct
51 Correct 126 ms 30264 KB Output is correct
52 Correct 127 ms 30320 KB Output is correct
53 Correct 123 ms 30296 KB Output is correct