#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
ll n;
ll p[200005];
vector<vll>adj;
ll dp[200005];
ll par[200005];
ll an[200005][20];
ll depth[200005] = {0};
ll find_par(ll x){
if(par[x] == x){
return x;
}
return par[x] = find_par(par[x]);
}
void enose(ll a,ll b){
ll p1 = find_par(a);
ll p2 = find_par(b);
par[p1] = p2;
}
ll kth(ll a,ll k){
ll pos = 0;
while(k){
if(k % 2){
a = an[a][pos];
}
pos++;
k /= 2;
}
return a;
}
ll lca(ll a,ll b){
if(depth[a] > depth[b]){
swap(a,b);
}
b = kth(b,depth[b] - depth[a]);
if(a == b){
return a;
}
for(ll j = 19;j >= 0;j--){
if(an[a][j] != an[b][j]){
a = an[a][j];
b = an[b][j];
}
}
return an[a][0];
}
ll dist(ll a,ll b){
return depth[a] + depth[b] - 2 * depth[lca(a,b)];
}
void build(ll idx,ll par){
an[idx][0] = par;
f(j,1,20){
an[idx][j] = an[an[idx][j-1]][j-1];
}
for(auto x:adj[idx]){
if(x != par){
depth[x] = depth[idx] + 1;
build(x,idx);
}
}
}
ll solve(ll idx){
if(dp[idx] != -1){
return dp[idx];
}
ll ans = 0;
for(auto x:adj[idx]){
if(p[x] < p[idx]){
ans = max(ans,solve(find_par(x)) + dist(find_par(x),idx));
enose(x,idx);
}
}
return dp[idx] = ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
adj.assign(n+5,vll());
ll root = -1;
vector<ii>ex;
f(i,0,n){
cin>>p[i+1];
ex.pb(ii(p[i+1],i+1));
if(p[i+1] == n){
root = i+1;
}
}
f(i,0,n-1){
ll a,b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
f(i,0,n+5){
par[i] = i;
}
memset(dp,-1,sizeof dp);
build(root,0);
sort(all(ex));
for(auto x:ex){
solve(x.S);
}
cout<<solve(root)<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1872 KB |
Output is correct |
3 |
Correct |
1 ms |
1832 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1872 KB |
Output is correct |
9 |
Correct |
1 ms |
1868 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1872 KB |
Output is correct |
3 |
Correct |
1 ms |
1832 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1872 KB |
Output is correct |
9 |
Correct |
1 ms |
1868 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
2004 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
1 ms |
1908 KB |
Output is correct |
14 |
Correct |
1 ms |
2004 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
2004 KB |
Output is correct |
17 |
Correct |
1 ms |
1996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1872 KB |
Output is correct |
3 |
Correct |
1 ms |
1832 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1872 KB |
Output is correct |
9 |
Correct |
1 ms |
1868 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
2004 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
1 ms |
1908 KB |
Output is correct |
14 |
Correct |
1 ms |
2004 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
2004 KB |
Output is correct |
17 |
Correct |
1 ms |
1996 KB |
Output is correct |
18 |
Correct |
4 ms |
3412 KB |
Output is correct |
19 |
Correct |
4 ms |
3412 KB |
Output is correct |
20 |
Correct |
3 ms |
3412 KB |
Output is correct |
21 |
Correct |
3 ms |
3416 KB |
Output is correct |
22 |
Correct |
3 ms |
3292 KB |
Output is correct |
23 |
Correct |
3 ms |
3284 KB |
Output is correct |
24 |
Correct |
3 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1872 KB |
Output is correct |
3 |
Correct |
1 ms |
1832 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1872 KB |
Output is correct |
9 |
Correct |
1 ms |
1868 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
2004 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
1 ms |
1908 KB |
Output is correct |
14 |
Correct |
1 ms |
2004 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
2004 KB |
Output is correct |
17 |
Correct |
1 ms |
1996 KB |
Output is correct |
18 |
Correct |
4 ms |
3412 KB |
Output is correct |
19 |
Correct |
4 ms |
3412 KB |
Output is correct |
20 |
Correct |
3 ms |
3412 KB |
Output is correct |
21 |
Correct |
3 ms |
3416 KB |
Output is correct |
22 |
Correct |
3 ms |
3292 KB |
Output is correct |
23 |
Correct |
3 ms |
3284 KB |
Output is correct |
24 |
Correct |
3 ms |
3284 KB |
Output is correct |
25 |
Correct |
2 ms |
1876 KB |
Output is correct |
26 |
Correct |
4 ms |
3412 KB |
Output is correct |
27 |
Correct |
4 ms |
3412 KB |
Output is correct |
28 |
Correct |
4 ms |
3412 KB |
Output is correct |
29 |
Correct |
4 ms |
3412 KB |
Output is correct |
30 |
Correct |
4 ms |
3284 KB |
Output is correct |
31 |
Correct |
4 ms |
3216 KB |
Output is correct |
32 |
Correct |
4 ms |
3284 KB |
Output is correct |
33 |
Correct |
4 ms |
3204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1872 KB |
Output is correct |
3 |
Correct |
1 ms |
1832 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1872 KB |
Output is correct |
9 |
Correct |
1 ms |
1868 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
2004 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
1 ms |
1908 KB |
Output is correct |
14 |
Correct |
1 ms |
2004 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
2004 KB |
Output is correct |
17 |
Correct |
1 ms |
1996 KB |
Output is correct |
18 |
Correct |
4 ms |
3412 KB |
Output is correct |
19 |
Correct |
4 ms |
3412 KB |
Output is correct |
20 |
Correct |
3 ms |
3412 KB |
Output is correct |
21 |
Correct |
3 ms |
3416 KB |
Output is correct |
22 |
Correct |
3 ms |
3292 KB |
Output is correct |
23 |
Correct |
3 ms |
3284 KB |
Output is correct |
24 |
Correct |
3 ms |
3284 KB |
Output is correct |
25 |
Correct |
135 ms |
61924 KB |
Output is correct |
26 |
Correct |
129 ms |
65156 KB |
Output is correct |
27 |
Correct |
146 ms |
65048 KB |
Output is correct |
28 |
Correct |
164 ms |
62752 KB |
Output is correct |
29 |
Correct |
174 ms |
63972 KB |
Output is correct |
30 |
Correct |
166 ms |
63676 KB |
Output is correct |
31 |
Correct |
149 ms |
64196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
164 ms |
53888 KB |
Output is correct |
4 |
Correct |
176 ms |
57160 KB |
Output is correct |
5 |
Correct |
171 ms |
57156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1872 KB |
Output is correct |
3 |
Correct |
1 ms |
1832 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1872 KB |
Output is correct |
9 |
Correct |
1 ms |
1868 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
2004 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
1 ms |
1908 KB |
Output is correct |
14 |
Correct |
1 ms |
2004 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
2004 KB |
Output is correct |
17 |
Correct |
1 ms |
1996 KB |
Output is correct |
18 |
Correct |
4 ms |
3412 KB |
Output is correct |
19 |
Correct |
4 ms |
3412 KB |
Output is correct |
20 |
Correct |
3 ms |
3412 KB |
Output is correct |
21 |
Correct |
3 ms |
3416 KB |
Output is correct |
22 |
Correct |
3 ms |
3292 KB |
Output is correct |
23 |
Correct |
3 ms |
3284 KB |
Output is correct |
24 |
Correct |
3 ms |
3284 KB |
Output is correct |
25 |
Correct |
2 ms |
1876 KB |
Output is correct |
26 |
Correct |
4 ms |
3412 KB |
Output is correct |
27 |
Correct |
4 ms |
3412 KB |
Output is correct |
28 |
Correct |
4 ms |
3412 KB |
Output is correct |
29 |
Correct |
4 ms |
3412 KB |
Output is correct |
30 |
Correct |
4 ms |
3284 KB |
Output is correct |
31 |
Correct |
4 ms |
3216 KB |
Output is correct |
32 |
Correct |
4 ms |
3284 KB |
Output is correct |
33 |
Correct |
4 ms |
3204 KB |
Output is correct |
34 |
Correct |
135 ms |
61924 KB |
Output is correct |
35 |
Correct |
129 ms |
65156 KB |
Output is correct |
36 |
Correct |
146 ms |
65048 KB |
Output is correct |
37 |
Correct |
164 ms |
62752 KB |
Output is correct |
38 |
Correct |
174 ms |
63972 KB |
Output is correct |
39 |
Correct |
166 ms |
63676 KB |
Output is correct |
40 |
Correct |
149 ms |
64196 KB |
Output is correct |
41 |
Correct |
1 ms |
1876 KB |
Output is correct |
42 |
Correct |
1 ms |
1876 KB |
Output is correct |
43 |
Correct |
164 ms |
53888 KB |
Output is correct |
44 |
Correct |
176 ms |
57160 KB |
Output is correct |
45 |
Correct |
171 ms |
57156 KB |
Output is correct |
46 |
Correct |
262 ms |
62752 KB |
Output is correct |
47 |
Correct |
311 ms |
62736 KB |
Output is correct |
48 |
Correct |
284 ms |
63676 KB |
Output is correct |
49 |
Correct |
253 ms |
62840 KB |
Output is correct |
50 |
Correct |
324 ms |
56728 KB |
Output is correct |
51 |
Correct |
341 ms |
56732 KB |
Output is correct |
52 |
Correct |
276 ms |
56752 KB |
Output is correct |
53 |
Correct |
338 ms |
56752 KB |
Output is correct |