#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=2e5+5,LG=__lg(maxN)+5;
const ll inf=2e18;
int n,a[maxN+1],h[maxN+1],up[maxN+1][LG+1],root;
ll dp[maxN+1];
vector<int> adj[maxN+1],vec;
void dfs(int u)
{
for(auto v:adj[u])
{
if(v==up[u][0])
{
continue;
}
up[v][0]=u;
h[v]=h[u]+1;
for(int j=1;j<=LG;j++)
{
up[v][j]=up[up[v][j-1]][j-1];
}
dfs(v);
}
}
int lca(int u,int v)
{
if(h[u]<h[v])
{
swap(u,v);
}
for(int j=LG;j>=0;j--)
{
if(h[up[u][j]]>=h[v])
{
u=up[u][j];
}
}
if(u==v)
{
return u;
}
for(int j=LG;j>=0;j--)
{
if(up[u][j]!=up[v][j])
{
u=up[u][j];
v=up[v][j];
}
}
return up[u][0];
}
struct dsu
{
int parent[maxN+1],mx[maxN+1],pos[maxN+1];
dsu()
{
memset(parent,-1,sizeof(parent));
memset(mx,0,sizeof(mx));
memset(pos,-1,sizeof(pos));
}
int Find(int v)
{
if(parent[v]<0)
{
return v;
}
return parent[v]=Find(parent[v]);
}
void Union(int a,int b)
{
a=Find(a);
b=Find(b);
if(a!=b)
{
if(-parent[a]<-parent[b])
{
swap(a,b);
}
parent[a]+=parent[b];
parent[b]=a;
if(mx[a]<mx[b])
{
mx[a]=mx[b];
pos[a]=pos[b];
}
}
}
}ds;
int get(int u,int v)
{
int p=lca(u,v);
return h[u]+h[v]-2*h[p];
}
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
vec.push_back(i);
ds.mx[i]=a[i];
ds.pos[i]=i;
if(a[i]==n)
{
root=i;
}
}
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
h[root]=1;
dfs(root);
sort(vec.begin(),vec.end(),[&](int x,int y)
{
return a[x]<a[y];
});
memset(dp,0,sizeof(dp));
for(auto u:vec)
{
//cout<<u<<'\n';
for(auto v:adj[u])
{
int pos_v=ds.pos[ds.Find(v)];
if(a[pos_v]>a[u])
{
continue;
}
dp[u]=max(dp[u],dp[pos_v]+get(u,pos_v));
//cout<<v<<" "<<pos_v<<" "<<dp[u]<<'\n';
ds.Union(u,v);
}
}
cout<<dp[root];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
2 ms |
12976 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
2 ms |
12976 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
4 ms |
13400 KB |
Output is correct |
19 |
Correct |
4 ms |
13404 KB |
Output is correct |
20 |
Correct |
6 ms |
13468 KB |
Output is correct |
21 |
Correct |
4 ms |
13404 KB |
Output is correct |
22 |
Correct |
4 ms |
13148 KB |
Output is correct |
23 |
Correct |
4 ms |
13148 KB |
Output is correct |
24 |
Correct |
6 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
2 ms |
12976 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
4 ms |
13400 KB |
Output is correct |
19 |
Correct |
4 ms |
13404 KB |
Output is correct |
20 |
Correct |
6 ms |
13468 KB |
Output is correct |
21 |
Correct |
4 ms |
13404 KB |
Output is correct |
22 |
Correct |
4 ms |
13148 KB |
Output is correct |
23 |
Correct |
4 ms |
13148 KB |
Output is correct |
24 |
Correct |
6 ms |
13148 KB |
Output is correct |
25 |
Correct |
2 ms |
12888 KB |
Output is correct |
26 |
Correct |
4 ms |
13148 KB |
Output is correct |
27 |
Correct |
4 ms |
13376 KB |
Output is correct |
28 |
Correct |
4 ms |
13404 KB |
Output is correct |
29 |
Correct |
4 ms |
13236 KB |
Output is correct |
30 |
Correct |
4 ms |
13148 KB |
Output is correct |
31 |
Correct |
4 ms |
13148 KB |
Output is correct |
32 |
Correct |
4 ms |
13152 KB |
Output is correct |
33 |
Correct |
4 ms |
13156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
2 ms |
12976 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
4 ms |
13400 KB |
Output is correct |
19 |
Correct |
4 ms |
13404 KB |
Output is correct |
20 |
Correct |
6 ms |
13468 KB |
Output is correct |
21 |
Correct |
4 ms |
13404 KB |
Output is correct |
22 |
Correct |
4 ms |
13148 KB |
Output is correct |
23 |
Correct |
4 ms |
13148 KB |
Output is correct |
24 |
Correct |
6 ms |
13148 KB |
Output is correct |
25 |
Correct |
106 ms |
48080 KB |
Output is correct |
26 |
Correct |
113 ms |
48076 KB |
Output is correct |
27 |
Correct |
107 ms |
48080 KB |
Output is correct |
28 |
Correct |
143 ms |
45004 KB |
Output is correct |
29 |
Correct |
140 ms |
46544 KB |
Output is correct |
30 |
Correct |
153 ms |
46288 KB |
Output is correct |
31 |
Correct |
139 ms |
46792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
135 ms |
35480 KB |
Output is correct |
4 |
Correct |
120 ms |
35464 KB |
Output is correct |
5 |
Correct |
128 ms |
35536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
2 ms |
12976 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
4 ms |
13400 KB |
Output is correct |
19 |
Correct |
4 ms |
13404 KB |
Output is correct |
20 |
Correct |
6 ms |
13468 KB |
Output is correct |
21 |
Correct |
4 ms |
13404 KB |
Output is correct |
22 |
Correct |
4 ms |
13148 KB |
Output is correct |
23 |
Correct |
4 ms |
13148 KB |
Output is correct |
24 |
Correct |
6 ms |
13148 KB |
Output is correct |
25 |
Correct |
2 ms |
12888 KB |
Output is correct |
26 |
Correct |
4 ms |
13148 KB |
Output is correct |
27 |
Correct |
4 ms |
13376 KB |
Output is correct |
28 |
Correct |
4 ms |
13404 KB |
Output is correct |
29 |
Correct |
4 ms |
13236 KB |
Output is correct |
30 |
Correct |
4 ms |
13148 KB |
Output is correct |
31 |
Correct |
4 ms |
13148 KB |
Output is correct |
32 |
Correct |
4 ms |
13152 KB |
Output is correct |
33 |
Correct |
4 ms |
13156 KB |
Output is correct |
34 |
Correct |
106 ms |
48080 KB |
Output is correct |
35 |
Correct |
113 ms |
48076 KB |
Output is correct |
36 |
Correct |
107 ms |
48080 KB |
Output is correct |
37 |
Correct |
143 ms |
45004 KB |
Output is correct |
38 |
Correct |
140 ms |
46544 KB |
Output is correct |
39 |
Correct |
153 ms |
46288 KB |
Output is correct |
40 |
Correct |
139 ms |
46792 KB |
Output is correct |
41 |
Correct |
2 ms |
12892 KB |
Output is correct |
42 |
Correct |
2 ms |
12892 KB |
Output is correct |
43 |
Correct |
135 ms |
35480 KB |
Output is correct |
44 |
Correct |
120 ms |
35464 KB |
Output is correct |
45 |
Correct |
128 ms |
35536 KB |
Output is correct |
46 |
Correct |
206 ms |
44236 KB |
Output is correct |
47 |
Correct |
190 ms |
44232 KB |
Output is correct |
48 |
Correct |
181 ms |
45520 KB |
Output is correct |
49 |
Correct |
231 ms |
44232 KB |
Output is correct |
50 |
Correct |
235 ms |
35904 KB |
Output is correct |
51 |
Correct |
171 ms |
35684 KB |
Output is correct |
52 |
Correct |
174 ms |
35792 KB |
Output is correct |
53 |
Correct |
162 ms |
35784 KB |
Output is correct |