#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";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2000 KB |
Output is correct |
20 |
Correct |
3 ms |
2140 KB |
Output is correct |
21 |
Correct |
3 ms |
2140 KB |
Output is correct |
22 |
Correct |
3 ms |
2140 KB |
Output is correct |
23 |
Correct |
3 ms |
2136 KB |
Output is correct |
24 |
Correct |
3 ms |
2136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2000 KB |
Output is correct |
20 |
Correct |
3 ms |
2140 KB |
Output is correct |
21 |
Correct |
3 ms |
2140 KB |
Output is correct |
22 |
Correct |
3 ms |
2140 KB |
Output is correct |
23 |
Correct |
3 ms |
2136 KB |
Output is correct |
24 |
Correct |
3 ms |
2136 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1924 KB |
Output is correct |
27 |
Correct |
5 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 |
4 ms |
1880 KB |
Output is correct |
31 |
Correct |
4 ms |
1880 KB |
Output is correct |
32 |
Correct |
4 ms |
1880 KB |
Output is correct |
33 |
Correct |
3 ms |
1924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2000 KB |
Output is correct |
20 |
Correct |
3 ms |
2140 KB |
Output is correct |
21 |
Correct |
3 ms |
2140 KB |
Output is correct |
22 |
Correct |
3 ms |
2140 KB |
Output is correct |
23 |
Correct |
3 ms |
2136 KB |
Output is correct |
24 |
Correct |
3 ms |
2136 KB |
Output is correct |
25 |
Correct |
116 ms |
68412 KB |
Output is correct |
26 |
Correct |
117 ms |
68236 KB |
Output is correct |
27 |
Correct |
116 ms |
68324 KB |
Output is correct |
28 |
Correct |
165 ms |
68436 KB |
Output is correct |
29 |
Correct |
175 ms |
68420 KB |
Output is correct |
30 |
Correct |
195 ms |
68408 KB |
Output is correct |
31 |
Correct |
172 ms |
68432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
320 ms |
60720 KB |
Output is correct |
4 |
Correct |
293 ms |
60716 KB |
Output is correct |
5 |
Correct |
299 ms |
60720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2000 KB |
Output is correct |
20 |
Correct |
3 ms |
2140 KB |
Output is correct |
21 |
Correct |
3 ms |
2140 KB |
Output is correct |
22 |
Correct |
3 ms |
2140 KB |
Output is correct |
23 |
Correct |
3 ms |
2136 KB |
Output is correct |
24 |
Correct |
3 ms |
2136 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1924 KB |
Output is correct |
27 |
Correct |
5 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 |
4 ms |
1880 KB |
Output is correct |
31 |
Correct |
4 ms |
1880 KB |
Output is correct |
32 |
Correct |
4 ms |
1880 KB |
Output is correct |
33 |
Correct |
3 ms |
1924 KB |
Output is correct |
34 |
Correct |
116 ms |
68412 KB |
Output is correct |
35 |
Correct |
117 ms |
68236 KB |
Output is correct |
36 |
Correct |
116 ms |
68324 KB |
Output is correct |
37 |
Correct |
165 ms |
68436 KB |
Output is correct |
38 |
Correct |
175 ms |
68420 KB |
Output is correct |
39 |
Correct |
195 ms |
68408 KB |
Output is correct |
40 |
Correct |
172 ms |
68432 KB |
Output is correct |
41 |
Correct |
1 ms |
364 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
320 ms |
60720 KB |
Output is correct |
44 |
Correct |
293 ms |
60716 KB |
Output is correct |
45 |
Correct |
299 ms |
60720 KB |
Output is correct |
46 |
Correct |
370 ms |
64816 KB |
Output is correct |
47 |
Correct |
310 ms |
63816 KB |
Output is correct |
48 |
Correct |
287 ms |
65236 KB |
Output is correct |
49 |
Correct |
321 ms |
63992 KB |
Output is correct |
50 |
Correct |
395 ms |
60164 KB |
Output is correct |
51 |
Correct |
350 ms |
59984 KB |
Output is correct |
52 |
Correct |
366 ms |
60196 KB |
Output is correct |
53 |
Correct |
339 ms |
60192 KB |
Output is correct |