#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less_equal<>,
rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
#define ll long long
#define ii pair<ll,ll>
#ifndef DEBUG
#define cerr if (0) cerr
#define endl '\n'
#endif
const ll inf=1e15;
const ll maxn=2e5+5;
const ll mod=1e9+7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll arr[maxn];
ii brr[maxn];
ll par[maxn];
ll dp[maxn];
ll root(ll x){
if (par[x]==x) return x;
return par[x]=root(par[x]);
}
void connect(ll a, ll b){
a=root(a),b=root(b);
if (arr[a]>arr[b]) swap(a,b);
par[a]=b;
}
vector<ll> adj[maxn];
ll twok[maxn][22];
ll depth[maxn];
void dfs(ll x, ll p){
twok[x][0]=p;
depth[x]=depth[p]+1;
for (int i=1;i<21;i++){
ll h=twok[x][i-1];
twok[x][i]=twok[h][i-1];
}
for (auto u:adj[x]){
if (u==p) continue;
dfs(u,x);
}
}
ll lca(ll a, ll b){
if (depth[a]<depth[b]) swap(a,b);
for (int i=21;i>=0;i--){
if (depth[twok[a][i]]>=depth[b]) a=twok[a][i];
}
if (a==b) return a;
for (int i=21;i>=0;i--){
if (twok[a][i]!=twok[b][i]) a=twok[a][i],b=twok[b][i];
}
return twok[a][0];
}
ll dis(ll a, ll b){
return depth[a]+depth[b]-2*depth[lca(a,b)];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n;
cin>>n;
for (int i=1;i<=n;i++){
cin>>arr[i];
brr[i].first=arr[i],brr[i].second=i;
par[i]=i;
}
sort(brr+1,brr+1+n);
ll a,b;
for (int i=1;i<n;i++){
cin>>a>>b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
dfs(1,0);
for (ll i=1;i<=n;i++){
ll x=brr[i].second;
for (auto u:adj[x]){
if (arr[u]<arr[x]){
ll u2=root(u);
dp[x]=max(dp[x],dp[u2]+dis(x,u2));
connect(u2,x);
}
}
}
cout<<dp[brr[n].second];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5044 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5044 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
5 ms |
6612 KB |
Output is correct |
19 |
Correct |
5 ms |
6612 KB |
Output is correct |
20 |
Correct |
5 ms |
6620 KB |
Output is correct |
21 |
Correct |
7 ms |
6676 KB |
Output is correct |
22 |
Correct |
5 ms |
6612 KB |
Output is correct |
23 |
Correct |
8 ms |
6612 KB |
Output is correct |
24 |
Correct |
6 ms |
6612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5044 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
5 ms |
6612 KB |
Output is correct |
19 |
Correct |
5 ms |
6612 KB |
Output is correct |
20 |
Correct |
5 ms |
6620 KB |
Output is correct |
21 |
Correct |
7 ms |
6676 KB |
Output is correct |
22 |
Correct |
5 ms |
6612 KB |
Output is correct |
23 |
Correct |
8 ms |
6612 KB |
Output is correct |
24 |
Correct |
6 ms |
6612 KB |
Output is correct |
25 |
Correct |
2 ms |
4948 KB |
Output is correct |
26 |
Correct |
6 ms |
6484 KB |
Output is correct |
27 |
Correct |
6 ms |
6452 KB |
Output is correct |
28 |
Correct |
6 ms |
6428 KB |
Output is correct |
29 |
Correct |
6 ms |
6456 KB |
Output is correct |
30 |
Correct |
6 ms |
6356 KB |
Output is correct |
31 |
Correct |
7 ms |
6324 KB |
Output is correct |
32 |
Correct |
6 ms |
6336 KB |
Output is correct |
33 |
Correct |
7 ms |
6356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5044 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
5 ms |
6612 KB |
Output is correct |
19 |
Correct |
5 ms |
6612 KB |
Output is correct |
20 |
Correct |
5 ms |
6620 KB |
Output is correct |
21 |
Correct |
7 ms |
6676 KB |
Output is correct |
22 |
Correct |
5 ms |
6612 KB |
Output is correct |
23 |
Correct |
8 ms |
6612 KB |
Output is correct |
24 |
Correct |
6 ms |
6612 KB |
Output is correct |
25 |
Correct |
160 ms |
64808 KB |
Output is correct |
26 |
Correct |
152 ms |
64824 KB |
Output is correct |
27 |
Correct |
151 ms |
64832 KB |
Output is correct |
28 |
Correct |
224 ms |
64872 KB |
Output is correct |
29 |
Correct |
227 ms |
64844 KB |
Output is correct |
30 |
Correct |
223 ms |
64860 KB |
Output is correct |
31 |
Correct |
238 ms |
64888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5032 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
220 ms |
57048 KB |
Output is correct |
4 |
Correct |
225 ms |
57104 KB |
Output is correct |
5 |
Correct |
216 ms |
56992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5044 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
5 ms |
6612 KB |
Output is correct |
19 |
Correct |
5 ms |
6612 KB |
Output is correct |
20 |
Correct |
5 ms |
6620 KB |
Output is correct |
21 |
Correct |
7 ms |
6676 KB |
Output is correct |
22 |
Correct |
5 ms |
6612 KB |
Output is correct |
23 |
Correct |
8 ms |
6612 KB |
Output is correct |
24 |
Correct |
6 ms |
6612 KB |
Output is correct |
25 |
Correct |
2 ms |
4948 KB |
Output is correct |
26 |
Correct |
6 ms |
6484 KB |
Output is correct |
27 |
Correct |
6 ms |
6452 KB |
Output is correct |
28 |
Correct |
6 ms |
6428 KB |
Output is correct |
29 |
Correct |
6 ms |
6456 KB |
Output is correct |
30 |
Correct |
6 ms |
6356 KB |
Output is correct |
31 |
Correct |
7 ms |
6324 KB |
Output is correct |
32 |
Correct |
6 ms |
6336 KB |
Output is correct |
33 |
Correct |
7 ms |
6356 KB |
Output is correct |
34 |
Correct |
160 ms |
64808 KB |
Output is correct |
35 |
Correct |
152 ms |
64824 KB |
Output is correct |
36 |
Correct |
151 ms |
64832 KB |
Output is correct |
37 |
Correct |
224 ms |
64872 KB |
Output is correct |
38 |
Correct |
227 ms |
64844 KB |
Output is correct |
39 |
Correct |
223 ms |
64860 KB |
Output is correct |
40 |
Correct |
238 ms |
64888 KB |
Output is correct |
41 |
Correct |
2 ms |
5032 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
220 ms |
57048 KB |
Output is correct |
44 |
Correct |
225 ms |
57104 KB |
Output is correct |
45 |
Correct |
216 ms |
56992 KB |
Output is correct |
46 |
Correct |
387 ms |
61160 KB |
Output is correct |
47 |
Correct |
364 ms |
60232 KB |
Output is correct |
48 |
Correct |
344 ms |
61560 KB |
Output is correct |
49 |
Correct |
390 ms |
60352 KB |
Output is correct |
50 |
Correct |
306 ms |
56528 KB |
Output is correct |
51 |
Correct |
303 ms |
56524 KB |
Output is correct |
52 |
Correct |
358 ms |
56520 KB |
Output is correct |
53 |
Correct |
300 ms |
56524 KB |
Output is correct |