#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) x.size()
#define all(x) x.begin(),x.end()
#define F first
#define S second
using namespace std;
const int maxn=200005;
int n,a,b,p[maxn][25],h[maxn],dep[maxn],dp[maxn];
vi g[maxn];
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin >> n;
for(int i=0;i<n;i++) cin >> h[i];
for(int i=1;i<n;i++){
cin >> a >> b;
a--,b--;
g[a].emplace_back(b);
g[b].emplace_back(a);
}
function<void(int,int)> dfs=[&](int x,int pre){
dep[x]=dep[pre]+1;
p[x][0]=pre;
for(int i=1;i<25;i++) p[x][i]=p[p[x][i-1]][i-1];
for(int u:g[x]){
if(u==pre) continue;
dfs(u,x);
}
};
dfs(0,0);
function<int(int,int)> lca=[&](int x,int y){
if(dep[x]>dep[y]) swap(x,y);
int tmp=dep[y]-dep[x],i=0;
while(tmp){
if(tmp&1) y=p[y][i];
i++,tmp>>=1;
}
if(x==y) return x;
for(int j=24;j>=0;j--){
if(p[x][j]!=p[y][j]){
x=p[x][j];
y=p[y][j];
}
}
return p[x][0];
};
vi ord(n),pa(n);
iota(all(ord),0);
iota(all(pa),0);
sort(all(ord),[](int i,int j){return h[i]<h[j];});
function<int(int)> get=[&](int x){
return x==pa[x] ? x : pa[x]=get(pa[x]);
};
function<int(int,int)> get_dist=[&](int x,int y){
return dep[x]+dep[y]-2*dep[lca(x,y)];
};
for(int i:ord){
for(int j:g[i]){
if(h[i]>h[j]){
dp[i]=max(dp[i],dp[get(j)]+get_dist(i,get(j)));
pa[get(j)]=i;
}
}
}
cout << dp[ord[n-1]] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8792 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8792 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8792 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8792 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
4 ms |
9308 KB |
Output is correct |
19 |
Correct |
4 ms |
9308 KB |
Output is correct |
20 |
Correct |
4 ms |
9308 KB |
Output is correct |
21 |
Correct |
5 ms |
9308 KB |
Output is correct |
22 |
Correct |
5 ms |
9320 KB |
Output is correct |
23 |
Correct |
4 ms |
9304 KB |
Output is correct |
24 |
Correct |
5 ms |
9404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8792 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8792 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
4 ms |
9308 KB |
Output is correct |
19 |
Correct |
4 ms |
9308 KB |
Output is correct |
20 |
Correct |
4 ms |
9308 KB |
Output is correct |
21 |
Correct |
5 ms |
9308 KB |
Output is correct |
22 |
Correct |
5 ms |
9320 KB |
Output is correct |
23 |
Correct |
4 ms |
9304 KB |
Output is correct |
24 |
Correct |
5 ms |
9404 KB |
Output is correct |
25 |
Correct |
2 ms |
8792 KB |
Output is correct |
26 |
Correct |
5 ms |
9052 KB |
Output is correct |
27 |
Correct |
5 ms |
9132 KB |
Output is correct |
28 |
Correct |
5 ms |
9052 KB |
Output is correct |
29 |
Correct |
5 ms |
9288 KB |
Output is correct |
30 |
Correct |
5 ms |
8796 KB |
Output is correct |
31 |
Correct |
4 ms |
8796 KB |
Output is correct |
32 |
Correct |
4 ms |
8796 KB |
Output is correct |
33 |
Correct |
5 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8792 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8792 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
4 ms |
9308 KB |
Output is correct |
19 |
Correct |
4 ms |
9308 KB |
Output is correct |
20 |
Correct |
4 ms |
9308 KB |
Output is correct |
21 |
Correct |
5 ms |
9308 KB |
Output is correct |
22 |
Correct |
5 ms |
9320 KB |
Output is correct |
23 |
Correct |
4 ms |
9304 KB |
Output is correct |
24 |
Correct |
5 ms |
9404 KB |
Output is correct |
25 |
Incorrect |
129 ms |
50548 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
175 ms |
34712 KB |
Output is correct |
4 |
Correct |
158 ms |
34636 KB |
Output is correct |
5 |
Correct |
158 ms |
34724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8792 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8792 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
4 ms |
9308 KB |
Output is correct |
19 |
Correct |
4 ms |
9308 KB |
Output is correct |
20 |
Correct |
4 ms |
9308 KB |
Output is correct |
21 |
Correct |
5 ms |
9308 KB |
Output is correct |
22 |
Correct |
5 ms |
9320 KB |
Output is correct |
23 |
Correct |
4 ms |
9304 KB |
Output is correct |
24 |
Correct |
5 ms |
9404 KB |
Output is correct |
25 |
Correct |
2 ms |
8792 KB |
Output is correct |
26 |
Correct |
5 ms |
9052 KB |
Output is correct |
27 |
Correct |
5 ms |
9132 KB |
Output is correct |
28 |
Correct |
5 ms |
9052 KB |
Output is correct |
29 |
Correct |
5 ms |
9288 KB |
Output is correct |
30 |
Correct |
5 ms |
8796 KB |
Output is correct |
31 |
Correct |
4 ms |
8796 KB |
Output is correct |
32 |
Correct |
4 ms |
8796 KB |
Output is correct |
33 |
Correct |
5 ms |
8796 KB |
Output is correct |
34 |
Incorrect |
129 ms |
50548 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |