#include <bits/stdc++.h>
using namespace std;
int n,h[200001],p[200001][20],d[200001],u,v,mx,r;
set <int> ke[200001];
void dfs(int u, int par){
for (int v:ke[u])
if (v!=par){
p[v][0]=u;
for (int i=1;i<20;i++)
p[v][i]=p[p[v][i-1]][i-1];
d[v]=d[u]+1;
dfs(v,u);
}
}
int lca(int u, int v){
if (d[u]<d[v])
swap(u,v);
for (int i=19;i>=0;i--)
if (d[p[u][i]]>=d[v])
u=p[u][i];
if (u==v)
return u;
for (int i=19;i>=0;i--)
if (p[u][i]!=p[v][i]){
u=p[u][i];
v=p[v][i];
}
return p[u][0];
}
int dist(int u, int v){
return d[u]+d[v]-d[lca(u,v)]*2;
}
int dfs3(int u, int par){
int res=u;
for (int v:ke[u])
if (v!=par){
int w=dfs3(v,u);
res=(h[res]>h[w]?res:w);
}
return res;
}
void dfs2(int u, int val){
mx=max(mx,val);
for (int v:ke[u]){
ke[v].erase(u);
int w=dfs3(v,v);
dfs2(w,val+dist(u,w));
}
}
int main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> n;
for (int i=1;i<=n;i++){
cin >> h[i];
if (h[i]==n)
r=i;
}
for (int i=1;i<n;i++){
cin >> u >> v;
ke[u].insert(v);
ke[v].insert(u);
}
dfs(r,r);
dfs2(r,0);
cout << mx;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9800 KB |
Output is correct |
3 |
Correct |
4 ms |
9868 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9768 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9860 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9800 KB |
Output is correct |
3 |
Correct |
4 ms |
9868 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9768 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9860 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
5 ms |
9960 KB |
Output is correct |
12 |
Correct |
5 ms |
9816 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9800 KB |
Output is correct |
3 |
Correct |
4 ms |
9868 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9768 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9860 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
5 ms |
9960 KB |
Output is correct |
12 |
Correct |
5 ms |
9816 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
169 ms |
11212 KB |
Output is correct |
19 |
Correct |
175 ms |
11204 KB |
Output is correct |
20 |
Correct |
170 ms |
11204 KB |
Output is correct |
21 |
Correct |
7 ms |
11096 KB |
Output is correct |
22 |
Correct |
7 ms |
11072 KB |
Output is correct |
23 |
Correct |
7 ms |
10844 KB |
Output is correct |
24 |
Correct |
8 ms |
11048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9800 KB |
Output is correct |
3 |
Correct |
4 ms |
9868 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9768 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9860 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
5 ms |
9960 KB |
Output is correct |
12 |
Correct |
5 ms |
9816 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
169 ms |
11212 KB |
Output is correct |
19 |
Correct |
175 ms |
11204 KB |
Output is correct |
20 |
Correct |
170 ms |
11204 KB |
Output is correct |
21 |
Correct |
7 ms |
11096 KB |
Output is correct |
22 |
Correct |
7 ms |
11072 KB |
Output is correct |
23 |
Correct |
7 ms |
10844 KB |
Output is correct |
24 |
Correct |
8 ms |
11048 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
183 ms |
11104 KB |
Output is correct |
27 |
Correct |
186 ms |
11096 KB |
Output is correct |
28 |
Correct |
185 ms |
11100 KB |
Output is correct |
29 |
Correct |
180 ms |
11100 KB |
Output is correct |
30 |
Correct |
12 ms |
10844 KB |
Output is correct |
31 |
Correct |
19 ms |
10864 KB |
Output is correct |
32 |
Correct |
15 ms |
10856 KB |
Output is correct |
33 |
Correct |
18 ms |
10840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9800 KB |
Output is correct |
3 |
Correct |
4 ms |
9868 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9768 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9860 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
5 ms |
9960 KB |
Output is correct |
12 |
Correct |
5 ms |
9816 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
169 ms |
11212 KB |
Output is correct |
19 |
Correct |
175 ms |
11204 KB |
Output is correct |
20 |
Correct |
170 ms |
11204 KB |
Output is correct |
21 |
Correct |
7 ms |
11096 KB |
Output is correct |
22 |
Correct |
7 ms |
11072 KB |
Output is correct |
23 |
Correct |
7 ms |
10844 KB |
Output is correct |
24 |
Correct |
8 ms |
11048 KB |
Output is correct |
25 |
Execution timed out |
2062 ms |
65084 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9836 KB |
Output is correct |
3 |
Execution timed out |
2096 ms |
49544 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9800 KB |
Output is correct |
3 |
Correct |
4 ms |
9868 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9768 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9860 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
5 ms |
9960 KB |
Output is correct |
12 |
Correct |
5 ms |
9816 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
169 ms |
11212 KB |
Output is correct |
19 |
Correct |
175 ms |
11204 KB |
Output is correct |
20 |
Correct |
170 ms |
11204 KB |
Output is correct |
21 |
Correct |
7 ms |
11096 KB |
Output is correct |
22 |
Correct |
7 ms |
11072 KB |
Output is correct |
23 |
Correct |
7 ms |
10844 KB |
Output is correct |
24 |
Correct |
8 ms |
11048 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
183 ms |
11104 KB |
Output is correct |
27 |
Correct |
186 ms |
11096 KB |
Output is correct |
28 |
Correct |
185 ms |
11100 KB |
Output is correct |
29 |
Correct |
180 ms |
11100 KB |
Output is correct |
30 |
Correct |
12 ms |
10844 KB |
Output is correct |
31 |
Correct |
19 ms |
10864 KB |
Output is correct |
32 |
Correct |
15 ms |
10856 KB |
Output is correct |
33 |
Correct |
18 ms |
10840 KB |
Output is correct |
34 |
Execution timed out |
2062 ms |
65084 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |