#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=2e5+13;
int n;
int a[N];
int h[N];
int d[N];
vector<int> g[N];
int x, y;
int par[N];
int st[N][20];
int val[N];
ll dp[N];
void dfs(int v, int par){
for(int c: g[v]){
if(c==par) continue;
d[c]=d[v]+1;
st[c][0]=v;
for(int j=1; j<20; j++){
st[c][j]=st[st[c][j-1]][j-1];
}
dfs(c, v);
}
}
void scan(){
cin >> n;
for(int i=1; i<=n; i++){
cin >> a[i];
h[a[i]]=i;
val[i]=a[i];
par[i]=i;
}
for(int i=1; i<n; i++){
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 1);
}
int lca(int u, int v){
if(d[u] > d[v]) swap(u, v);
int k=d[v]-d[u];
for(int j=0; j<=19; j++){
if(k >> j & 1){
v=st[v][j];
}
}
if(u==v) return u;
for(int j=19; j>=0; j--){
if(st[u][j]!=st[v][j]){
u=st[u][j];
v=st[v][j];
}
}
return st[u][0];
}
int dis(int u, int v){
return d[u]+d[v]-2*d[lca(u, v)];
}
int f(int v){
if(par[v]==v) return v;
par[v]=f(par[v]);
return par[v];
}
void uni(int u, int v){
int Ru=f(u);
int Rv=f(v);
if(Ru!=Rv){
if(val[Ru] > val[Rv]) swap(Ru, Rv); // Ru < Rv
par[Ru]=Rv;
val[Rv]=max(val[Ru], val[Rv]);
}
}
void solve(){
for(int i=1; i<=n; i++){
int u=h[i];
for(int v: g[u]){
if(a[v] < a[u]){
int prv=f(v);
dp[u]=max(dp[u], dp[prv] + dis(prv, u));
uni(u, v);
}
}
}
cout << dp[h[n]];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
scan();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4960 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5160 KB |
Output is correct |
6 |
Correct |
2 ms |
5164 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
5172 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4960 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5160 KB |
Output is correct |
6 |
Correct |
2 ms |
5164 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
5172 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5120 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4960 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5160 KB |
Output is correct |
6 |
Correct |
2 ms |
5164 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
5172 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5120 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
6 ms |
6236 KB |
Output is correct |
19 |
Correct |
5 ms |
6348 KB |
Output is correct |
20 |
Correct |
5 ms |
6096 KB |
Output is correct |
21 |
Correct |
4 ms |
6236 KB |
Output is correct |
22 |
Correct |
4 ms |
6236 KB |
Output is correct |
23 |
Correct |
4 ms |
6236 KB |
Output is correct |
24 |
Correct |
4 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4960 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5160 KB |
Output is correct |
6 |
Correct |
2 ms |
5164 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
5172 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5120 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
6 ms |
6236 KB |
Output is correct |
19 |
Correct |
5 ms |
6348 KB |
Output is correct |
20 |
Correct |
5 ms |
6096 KB |
Output is correct |
21 |
Correct |
4 ms |
6236 KB |
Output is correct |
22 |
Correct |
4 ms |
6236 KB |
Output is correct |
23 |
Correct |
4 ms |
6236 KB |
Output is correct |
24 |
Correct |
4 ms |
6236 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
4 ms |
5936 KB |
Output is correct |
27 |
Correct |
4 ms |
5980 KB |
Output is correct |
28 |
Correct |
4 ms |
6100 KB |
Output is correct |
29 |
Correct |
4 ms |
6236 KB |
Output is correct |
30 |
Correct |
4 ms |
5724 KB |
Output is correct |
31 |
Correct |
4 ms |
5724 KB |
Output is correct |
32 |
Correct |
4 ms |
5724 KB |
Output is correct |
33 |
Correct |
4 ms |
5724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4960 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5160 KB |
Output is correct |
6 |
Correct |
2 ms |
5164 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
5172 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5120 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
6 ms |
6236 KB |
Output is correct |
19 |
Correct |
5 ms |
6348 KB |
Output is correct |
20 |
Correct |
5 ms |
6096 KB |
Output is correct |
21 |
Correct |
4 ms |
6236 KB |
Output is correct |
22 |
Correct |
4 ms |
6236 KB |
Output is correct |
23 |
Correct |
4 ms |
6236 KB |
Output is correct |
24 |
Correct |
4 ms |
6236 KB |
Output is correct |
25 |
Correct |
87 ms |
51836 KB |
Output is correct |
26 |
Correct |
86 ms |
51728 KB |
Output is correct |
27 |
Correct |
86 ms |
51796 KB |
Output is correct |
28 |
Correct |
113 ms |
51792 KB |
Output is correct |
29 |
Correct |
101 ms |
51792 KB |
Output is correct |
30 |
Correct |
105 ms |
51796 KB |
Output is correct |
31 |
Correct |
101 ms |
51792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
119 ms |
36008 KB |
Output is correct |
4 |
Correct |
122 ms |
36108 KB |
Output is correct |
5 |
Correct |
117 ms |
36176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4960 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5160 KB |
Output is correct |
6 |
Correct |
2 ms |
5164 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
5172 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5120 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
6 ms |
6236 KB |
Output is correct |
19 |
Correct |
5 ms |
6348 KB |
Output is correct |
20 |
Correct |
5 ms |
6096 KB |
Output is correct |
21 |
Correct |
4 ms |
6236 KB |
Output is correct |
22 |
Correct |
4 ms |
6236 KB |
Output is correct |
23 |
Correct |
4 ms |
6236 KB |
Output is correct |
24 |
Correct |
4 ms |
6236 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
4 ms |
5936 KB |
Output is correct |
27 |
Correct |
4 ms |
5980 KB |
Output is correct |
28 |
Correct |
4 ms |
6100 KB |
Output is correct |
29 |
Correct |
4 ms |
6236 KB |
Output is correct |
30 |
Correct |
4 ms |
5724 KB |
Output is correct |
31 |
Correct |
4 ms |
5724 KB |
Output is correct |
32 |
Correct |
4 ms |
5724 KB |
Output is correct |
33 |
Correct |
4 ms |
5724 KB |
Output is correct |
34 |
Correct |
87 ms |
51836 KB |
Output is correct |
35 |
Correct |
86 ms |
51728 KB |
Output is correct |
36 |
Correct |
86 ms |
51796 KB |
Output is correct |
37 |
Correct |
113 ms |
51792 KB |
Output is correct |
38 |
Correct |
101 ms |
51792 KB |
Output is correct |
39 |
Correct |
105 ms |
51796 KB |
Output is correct |
40 |
Correct |
101 ms |
51792 KB |
Output is correct |
41 |
Correct |
3 ms |
4952 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
119 ms |
36008 KB |
Output is correct |
44 |
Correct |
122 ms |
36108 KB |
Output is correct |
45 |
Correct |
117 ms |
36176 KB |
Output is correct |
46 |
Correct |
171 ms |
44884 KB |
Output is correct |
47 |
Correct |
178 ms |
43348 KB |
Output is correct |
48 |
Correct |
166 ms |
45392 KB |
Output is correct |
49 |
Correct |
176 ms |
43560 KB |
Output is correct |
50 |
Correct |
148 ms |
36176 KB |
Output is correct |
51 |
Correct |
156 ms |
36316 KB |
Output is correct |
52 |
Correct |
139 ms |
36228 KB |
Output is correct |
53 |
Correct |
135 ms |
36180 KB |
Output is correct |