#include<bits/stdc++.h>
using namespace std;
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]);
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
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]];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10584 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10584 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
4 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11352 KB |
Output is correct |
20 |
Correct |
3 ms |
11356 KB |
Output is correct |
21 |
Correct |
4 ms |
11356 KB |
Output is correct |
22 |
Correct |
7 ms |
11356 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
3 ms |
11352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10584 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
4 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11352 KB |
Output is correct |
20 |
Correct |
3 ms |
11356 KB |
Output is correct |
21 |
Correct |
4 ms |
11356 KB |
Output is correct |
22 |
Correct |
7 ms |
11356 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
3 ms |
11352 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
4 ms |
11100 KB |
Output is correct |
27 |
Correct |
5 ms |
11100 KB |
Output is correct |
28 |
Correct |
4 ms |
11100 KB |
Output is correct |
29 |
Correct |
4 ms |
11100 KB |
Output is correct |
30 |
Correct |
4 ms |
10844 KB |
Output is correct |
31 |
Correct |
4 ms |
10844 KB |
Output is correct |
32 |
Correct |
5 ms |
10840 KB |
Output is correct |
33 |
Correct |
4 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10584 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
4 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11352 KB |
Output is correct |
20 |
Correct |
3 ms |
11356 KB |
Output is correct |
21 |
Correct |
4 ms |
11356 KB |
Output is correct |
22 |
Correct |
7 ms |
11356 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
3 ms |
11352 KB |
Output is correct |
25 |
Correct |
87 ms |
34644 KB |
Output is correct |
26 |
Correct |
83 ms |
38628 KB |
Output is correct |
27 |
Correct |
86 ms |
38548 KB |
Output is correct |
28 |
Correct |
117 ms |
38660 KB |
Output is correct |
29 |
Correct |
114 ms |
38616 KB |
Output is correct |
30 |
Correct |
113 ms |
38692 KB |
Output is correct |
31 |
Correct |
120 ms |
38480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
106 ms |
19044 KB |
Output is correct |
4 |
Correct |
118 ms |
19052 KB |
Output is correct |
5 |
Correct |
124 ms |
19012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10584 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
4 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11352 KB |
Output is correct |
20 |
Correct |
3 ms |
11356 KB |
Output is correct |
21 |
Correct |
4 ms |
11356 KB |
Output is correct |
22 |
Correct |
7 ms |
11356 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
3 ms |
11352 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
4 ms |
11100 KB |
Output is correct |
27 |
Correct |
5 ms |
11100 KB |
Output is correct |
28 |
Correct |
4 ms |
11100 KB |
Output is correct |
29 |
Correct |
4 ms |
11100 KB |
Output is correct |
30 |
Correct |
4 ms |
10844 KB |
Output is correct |
31 |
Correct |
4 ms |
10844 KB |
Output is correct |
32 |
Correct |
5 ms |
10840 KB |
Output is correct |
33 |
Correct |
4 ms |
10844 KB |
Output is correct |
34 |
Correct |
87 ms |
34644 KB |
Output is correct |
35 |
Correct |
83 ms |
38628 KB |
Output is correct |
36 |
Correct |
86 ms |
38548 KB |
Output is correct |
37 |
Correct |
117 ms |
38660 KB |
Output is correct |
38 |
Correct |
114 ms |
38616 KB |
Output is correct |
39 |
Correct |
113 ms |
38692 KB |
Output is correct |
40 |
Correct |
120 ms |
38480 KB |
Output is correct |
41 |
Correct |
2 ms |
10588 KB |
Output is correct |
42 |
Correct |
2 ms |
10588 KB |
Output is correct |
43 |
Correct |
106 ms |
19044 KB |
Output is correct |
44 |
Correct |
118 ms |
19052 KB |
Output is correct |
45 |
Correct |
124 ms |
19012 KB |
Output is correct |
46 |
Correct |
124 ms |
31416 KB |
Output is correct |
47 |
Correct |
137 ms |
30040 KB |
Output is correct |
48 |
Correct |
133 ms |
32284 KB |
Output is correct |
49 |
Correct |
141 ms |
30168 KB |
Output is correct |
50 |
Correct |
172 ms |
23020 KB |
Output is correct |
51 |
Correct |
140 ms |
23020 KB |
Output is correct |
52 |
Correct |
141 ms |
23112 KB |
Output is correct |
53 |
Correct |
174 ms |
23120 KB |
Output is correct |