#include <bits/stdc++.h>
using namespace std;
#define int long long
struct DSU
{
vector<int> v;
vector<pair<int, int>> mx;
void init(int n, vector<int>& a){
v.assign(n, -1);
mx.resize(n);
for(int i = 0; i < n; i++){
mx[i] = {a[i], i};
}
}
int get(int x){return v[x] < 0 ? x : v[x] = get(v[x]);}
void unite(int x, int y){
x = get(x); y = get(y);
if(x == y) return;
if(v[x] > v[y]) swap(x, y);
v[x] += v[y]; v[y] = x;
mx[x] = max(mx[x], mx[y]);
}
int getMx(int x){
x = get(x);
return mx[x].second;
}
};
const int LOG = 20;
vector<int> depth, parents;
vector<vector<int>> dp, adj;
void DFS(int x){
for(int node : adj[x]){
if(node != parents[x]){
parents[node] = x;
depth[node] = depth[x] + 1;
DFS(node);
}
}
}
int kth(int x, int dist){
for(int k = 0; k < LOG; k++){
if((1 << k) & dist){
x = dp[k][x];
}
}
return x;
}
int lca(int a, int b){
if(depth[a] > depth[b]) swap(a, b);
b = kth(b, depth[b] - depth[a]);
if(a==b) return a;
int anc = a;
for(int k = LOG - 1; k >= 0; k--){
if(dp[k][a] == dp[k][b]) anc = dp[k][a];
else{
a = dp[k][a];
b = dp[k][b];
}
}
return anc;
}
int getDist(int a, int b){
int anc = lca(a, b);
return depth[a] + depth[b] - 2 * depth[anc];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int N; cin >> N;
vector<pair<int, int>> P(N);
vector<int> a(N);
for(int i = 0; i < N; i++){
int x; cin >> x;
P[i] = {x, i};
a[i] = x;
}
DSU UF; UF.init(N, a);
adj.resize(N);
for(int i = 0; i < N - 1; i++){
int u, v; cin >> u >> v; u--; v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
parents.assign(N, -1);
depth.assign(N, 0);
DFS(0);
dp.assign(LOG, vector<int>(N));
for(int k = 0; k < LOG; k++){
for(int i = 0; i < N; i++){
if(k == 0) dp[k][i] = parents[i];
else{
if(dp[k-1][i] == -1) dp[k][i] = -1;
else dp[k][i] = dp[k-1][dp[k-1][i]];
}
}
}
sort(P.begin(), P.end());
vector<int> ans(N, 0);
for(pair<int, int> p : P){
int i = p.second;
for(int node : adj[i]){
if(a[node] > a[i]) continue;
ans[i] = max(ans[i], ans[UF.getMx(node)]+getDist(i, UF.getMx(node)));
UF.unite(i, node);
}
}
cout << ans[P[N-1].second] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1932 KB |
Output is correct |
21 |
Correct |
3 ms |
1884 KB |
Output is correct |
22 |
Correct |
3 ms |
1884 KB |
Output is correct |
23 |
Correct |
3 ms |
1880 KB |
Output is correct |
24 |
Correct |
3 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1932 KB |
Output is correct |
21 |
Correct |
3 ms |
1884 KB |
Output is correct |
22 |
Correct |
3 ms |
1884 KB |
Output is correct |
23 |
Correct |
3 ms |
1880 KB |
Output is correct |
24 |
Correct |
3 ms |
1884 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1884 KB |
Output is correct |
27 |
Correct |
3 ms |
1884 KB |
Output is correct |
28 |
Correct |
3 ms |
1884 KB |
Output is correct |
29 |
Correct |
3 ms |
1884 KB |
Output is correct |
30 |
Correct |
3 ms |
1884 KB |
Output is correct |
31 |
Correct |
3 ms |
1884 KB |
Output is correct |
32 |
Correct |
3 ms |
1884 KB |
Output is correct |
33 |
Correct |
3 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1932 KB |
Output is correct |
21 |
Correct |
3 ms |
1884 KB |
Output is correct |
22 |
Correct |
3 ms |
1884 KB |
Output is correct |
23 |
Correct |
3 ms |
1880 KB |
Output is correct |
24 |
Correct |
3 ms |
1884 KB |
Output is correct |
25 |
Correct |
107 ms |
66132 KB |
Output is correct |
26 |
Correct |
104 ms |
69916 KB |
Output is correct |
27 |
Correct |
106 ms |
69996 KB |
Output is correct |
28 |
Correct |
153 ms |
69968 KB |
Output is correct |
29 |
Correct |
151 ms |
69988 KB |
Output is correct |
30 |
Correct |
154 ms |
70104 KB |
Output is correct |
31 |
Correct |
157 ms |
69972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
291 ms |
58224 KB |
Output is correct |
4 |
Correct |
288 ms |
58388 KB |
Output is correct |
5 |
Correct |
279 ms |
58412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1932 KB |
Output is correct |
21 |
Correct |
3 ms |
1884 KB |
Output is correct |
22 |
Correct |
3 ms |
1884 KB |
Output is correct |
23 |
Correct |
3 ms |
1880 KB |
Output is correct |
24 |
Correct |
3 ms |
1884 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1884 KB |
Output is correct |
27 |
Correct |
3 ms |
1884 KB |
Output is correct |
28 |
Correct |
3 ms |
1884 KB |
Output is correct |
29 |
Correct |
3 ms |
1884 KB |
Output is correct |
30 |
Correct |
3 ms |
1884 KB |
Output is correct |
31 |
Correct |
3 ms |
1884 KB |
Output is correct |
32 |
Correct |
3 ms |
1884 KB |
Output is correct |
33 |
Correct |
3 ms |
1884 KB |
Output is correct |
34 |
Correct |
107 ms |
66132 KB |
Output is correct |
35 |
Correct |
104 ms |
69916 KB |
Output is correct |
36 |
Correct |
106 ms |
69996 KB |
Output is correct |
37 |
Correct |
153 ms |
69968 KB |
Output is correct |
38 |
Correct |
151 ms |
69988 KB |
Output is correct |
39 |
Correct |
154 ms |
70104 KB |
Output is correct |
40 |
Correct |
157 ms |
69972 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
291 ms |
58224 KB |
Output is correct |
44 |
Correct |
288 ms |
58388 KB |
Output is correct |
45 |
Correct |
279 ms |
58412 KB |
Output is correct |
46 |
Correct |
291 ms |
66180 KB |
Output is correct |
47 |
Correct |
279 ms |
65292 KB |
Output is correct |
48 |
Correct |
267 ms |
66868 KB |
Output is correct |
49 |
Correct |
303 ms |
65364 KB |
Output is correct |
50 |
Correct |
362 ms |
61672 KB |
Output is correct |
51 |
Correct |
332 ms |
61652 KB |
Output is correct |
52 |
Correct |
354 ms |
61748 KB |
Output is correct |
53 |
Correct |
358 ms |
61520 KB |
Output is correct |