#include <bits/stdc++.h>
#define fi first
#define se second
#define sajz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 2e5+7, P = 19;
int n, timer = -1, p[N], d[N], tin[N], tout[N], up[N][P];
int par[N], sz[N], mx[N], pos[N]; ll dp[N];
vector<int> g[N];
void dfs(int v, int parent){
tin[v] = ++timer;
up[v][0] = parent;
for (int i=1; i<P; i++) up[v][i] = up[up[v][i-1]][i-1];
for (auto u : g[v]){
if (u == parent) continue;
d[u] = d[v] + 1;
dfs(u, v);
}
tout[v] = timer;
}
bool ancestor(int a, int b){return tin[a] <= tin[b] && tout[a] >= tout[b];}
int lca(int a, int b){
if (ancestor(a, b)) return a;
for (int i=P-1; i>=0; i--){
if (!ancestor(up[a][i], b)) a = up[a][i];
}
return up[a][0];
}
int Find(int a){return a == par[a] ? a : par[a] = Find(par[a]);}
void Union(int a, int b){
a = Find(a); b = Find(b);
if (a != b){
if (sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
par[b] = a;
mx[a] = max(mx[a], mx[b]);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
vector<pair<int,int>> sorted;
for (int i=1; i<=n; i++){
cin >> p[i];
sz[i] = 1;
par[i] = i;
mx[i] = p[i];
pos[p[i]] = i;
sorted.push_back({p[i], i});
}
sort(all(sorted));
for (int i=0; i<n-1; i++){
int a, b; cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1, 1);
int res = 0;
for (auto [val, v] : sorted){
for (auto u : g[v]){
if (p[u] < val){
int x = pos[mx[Find(u)]];
ll dist = d[v] + d[x] - 2 * d[lca(v, x)];
dp[v] = max(dp[v], dp[x] + dist);
Union(v, u);
}
}
if (val == n){cout << dp[v] << '\n'; return 0;}
}
return 0;
}
/*
4
3 4 1 2
1 2
2 3
3 4
*/
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:70:6: warning: unused variable 'res' [-Wunused-variable]
70 | int res = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14936 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
2 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14936 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
2 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14996 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14936 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
2 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14996 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
5 ms |
15704 KB |
Output is correct |
19 |
Correct |
5 ms |
15452 KB |
Output is correct |
20 |
Correct |
5 ms |
15516 KB |
Output is correct |
21 |
Correct |
5 ms |
15508 KB |
Output is correct |
22 |
Correct |
5 ms |
15452 KB |
Output is correct |
23 |
Correct |
5 ms |
15448 KB |
Output is correct |
24 |
Correct |
5 ms |
15452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14936 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
2 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14996 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
5 ms |
15704 KB |
Output is correct |
19 |
Correct |
5 ms |
15452 KB |
Output is correct |
20 |
Correct |
5 ms |
15516 KB |
Output is correct |
21 |
Correct |
5 ms |
15508 KB |
Output is correct |
22 |
Correct |
5 ms |
15452 KB |
Output is correct |
23 |
Correct |
5 ms |
15448 KB |
Output is correct |
24 |
Correct |
5 ms |
15452 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
5 ms |
15196 KB |
Output is correct |
27 |
Correct |
5 ms |
15196 KB |
Output is correct |
28 |
Correct |
5 ms |
15248 KB |
Output is correct |
29 |
Correct |
5 ms |
15196 KB |
Output is correct |
30 |
Correct |
6 ms |
15212 KB |
Output is correct |
31 |
Correct |
6 ms |
15196 KB |
Output is correct |
32 |
Correct |
5 ms |
15196 KB |
Output is correct |
33 |
Correct |
6 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14936 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
2 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14996 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
5 ms |
15704 KB |
Output is correct |
19 |
Correct |
5 ms |
15452 KB |
Output is correct |
20 |
Correct |
5 ms |
15516 KB |
Output is correct |
21 |
Correct |
5 ms |
15508 KB |
Output is correct |
22 |
Correct |
5 ms |
15452 KB |
Output is correct |
23 |
Correct |
5 ms |
15448 KB |
Output is correct |
24 |
Correct |
5 ms |
15452 KB |
Output is correct |
25 |
Correct |
111 ms |
48068 KB |
Output is correct |
26 |
Correct |
115 ms |
51860 KB |
Output is correct |
27 |
Correct |
126 ms |
51756 KB |
Output is correct |
28 |
Correct |
165 ms |
51984 KB |
Output is correct |
29 |
Correct |
163 ms |
51904 KB |
Output is correct |
30 |
Correct |
163 ms |
51908 KB |
Output is correct |
31 |
Correct |
189 ms |
51920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
166 ms |
35528 KB |
Output is correct |
4 |
Correct |
144 ms |
35524 KB |
Output is correct |
5 |
Correct |
157 ms |
35780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14936 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
2 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14996 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
5 ms |
15704 KB |
Output is correct |
19 |
Correct |
5 ms |
15452 KB |
Output is correct |
20 |
Correct |
5 ms |
15516 KB |
Output is correct |
21 |
Correct |
5 ms |
15508 KB |
Output is correct |
22 |
Correct |
5 ms |
15452 KB |
Output is correct |
23 |
Correct |
5 ms |
15448 KB |
Output is correct |
24 |
Correct |
5 ms |
15452 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
5 ms |
15196 KB |
Output is correct |
27 |
Correct |
5 ms |
15196 KB |
Output is correct |
28 |
Correct |
5 ms |
15248 KB |
Output is correct |
29 |
Correct |
5 ms |
15196 KB |
Output is correct |
30 |
Correct |
6 ms |
15212 KB |
Output is correct |
31 |
Correct |
6 ms |
15196 KB |
Output is correct |
32 |
Correct |
5 ms |
15196 KB |
Output is correct |
33 |
Correct |
6 ms |
15196 KB |
Output is correct |
34 |
Correct |
111 ms |
48068 KB |
Output is correct |
35 |
Correct |
115 ms |
51860 KB |
Output is correct |
36 |
Correct |
126 ms |
51756 KB |
Output is correct |
37 |
Correct |
165 ms |
51984 KB |
Output is correct |
38 |
Correct |
163 ms |
51904 KB |
Output is correct |
39 |
Correct |
163 ms |
51908 KB |
Output is correct |
40 |
Correct |
189 ms |
51920 KB |
Output is correct |
41 |
Correct |
2 ms |
14940 KB |
Output is correct |
42 |
Correct |
2 ms |
14940 KB |
Output is correct |
43 |
Correct |
166 ms |
35528 KB |
Output is correct |
44 |
Correct |
144 ms |
35524 KB |
Output is correct |
45 |
Correct |
157 ms |
35780 KB |
Output is correct |
46 |
Correct |
275 ms |
46188 KB |
Output is correct |
47 |
Correct |
254 ms |
45052 KB |
Output is correct |
48 |
Correct |
266 ms |
46736 KB |
Output is correct |
49 |
Correct |
257 ms |
45196 KB |
Output is correct |
50 |
Correct |
237 ms |
39352 KB |
Output is correct |
51 |
Correct |
224 ms |
39368 KB |
Output is correct |
52 |
Correct |
225 ms |
39376 KB |
Output is correct |
53 |
Correct |
228 ms |
39364 KB |
Output is correct |