#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#define ll long long
#define pii pair<long long,long long>
#define F first
#define S second
#define pb emplace_back
#define endl "\n"
#define all(a) a.begin(),a.end()
#define int long long
vector<int> p;
struct DSU{
vector<int> rt,sz,mx;
DSU(int _n){
rt.resize(_n+1);
sz.assign(_n+1,1);
mx.resize(_n+1);
iota(all(rt),0);
iota(all(mx),0);
}
int find(int i){
if(rt[i]==i) return i;
return rt[i]=find(rt[i]);
}
void un(int u,int v){
u=find(u),v=find(v);
if(u==v) return;
if(sz[u]>sz[v]) swap(u,v);
rt[u]=v;
sz[v]+=sz[u];
mx[v]=p[mx[u]]>p[mx[v]]?mx[u]:mx[v];
}
};
void solve(){
int n;
cin>>n;
p.resize(n+1);
vector<int> pa(n+1),dep(n+1),idx(n+1),dp(n+1);
vector<bool> vis(n+1);
vector<vector<int>> g(n+1);
for(int i=1;i<=n;i++){
cin>>p[i];
idx[p[i]]=i;
}
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
g[a].pb(b);
g[b].pb(a);
}
auto dfs=[&](auto dfs,int i,int pre)->void{
dep[i]=dep[pre]+1;
pa[i]=pre;
for(int j:g[i]){
if(j==pre) continue;
dfs(dfs,j,i);
}
};
dfs(dfs,1,1);
vector<vector<int>> table(n+1,vector<int>(19));
for(int i=1;i<=n;i++) table[i][0]=pa[i];
for(int i=1;i<19;i++){
for(int j=1;j<=n;j++){
table[j][i]=table[table[j][i-1]][i-1];
}
}
auto lca=[&](int u,int v){
if(dep[u]>dep[v]) swap(u,v);
for(int i=18;i>=0;i--){
if(dep[table[v][i]]>=dep[u]) v=table[v][i];
}
if(u==v) return u;
for(int i=18;i>=0;i--){
if(table[u][i]!=table[v][i]){
u=table[u][i];
v=table[v][i];
}
}
return table[u][0];
};
DSU dsu(n);
for(int i=1;i<=n;i++){
vis[idx[i]]=1;
for(int j:g[idx[i]]){
if(vis[j]){
int node=dsu.mx[dsu.find(j)];
dp[idx[i]]=max(dp[idx[i]],dp[node]+dep[idx[i]]+dep[node]-2*dep[lca(idx[i],node)]);
dsu.un(idx[i],j);
}
}
}
cout<<dp[idx[n]];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 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 |
600 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 |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 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 |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2140 KB |
Output is correct |
20 |
Correct |
5 ms |
2320 KB |
Output is correct |
21 |
Correct |
4 ms |
2140 KB |
Output is correct |
22 |
Correct |
3 ms |
2140 KB |
Output is correct |
23 |
Correct |
4 ms |
2140 KB |
Output is correct |
24 |
Correct |
4 ms |
2136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 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 |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2140 KB |
Output is correct |
20 |
Correct |
5 ms |
2320 KB |
Output is correct |
21 |
Correct |
4 ms |
2140 KB |
Output is correct |
22 |
Correct |
3 ms |
2140 KB |
Output is correct |
23 |
Correct |
4 ms |
2140 KB |
Output is correct |
24 |
Correct |
4 ms |
2136 KB |
Output is correct |
25 |
Correct |
1 ms |
452 KB |
Output is correct |
26 |
Correct |
4 ms |
2140 KB |
Output is correct |
27 |
Correct |
4 ms |
2140 KB |
Output is correct |
28 |
Correct |
4 ms |
2004 KB |
Output is correct |
29 |
Correct |
4 ms |
2140 KB |
Output is correct |
30 |
Correct |
4 ms |
1880 KB |
Output is correct |
31 |
Correct |
4 ms |
1884 KB |
Output is correct |
32 |
Correct |
4 ms |
1880 KB |
Output is correct |
33 |
Correct |
4 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 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 |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2140 KB |
Output is correct |
20 |
Correct |
5 ms |
2320 KB |
Output is correct |
21 |
Correct |
4 ms |
2140 KB |
Output is correct |
22 |
Correct |
3 ms |
2140 KB |
Output is correct |
23 |
Correct |
4 ms |
2140 KB |
Output is correct |
24 |
Correct |
4 ms |
2136 KB |
Output is correct |
25 |
Correct |
170 ms |
72352 KB |
Output is correct |
26 |
Correct |
168 ms |
72180 KB |
Output is correct |
27 |
Correct |
166 ms |
72216 KB |
Output is correct |
28 |
Correct |
251 ms |
72272 KB |
Output is correct |
29 |
Correct |
265 ms |
72692 KB |
Output is correct |
30 |
Correct |
248 ms |
72436 KB |
Output is correct |
31 |
Correct |
250 ms |
72428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
257 ms |
64608 KB |
Output is correct |
4 |
Correct |
290 ms |
64616 KB |
Output is correct |
5 |
Correct |
252 ms |
64596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 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 |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2140 KB |
Output is correct |
20 |
Correct |
5 ms |
2320 KB |
Output is correct |
21 |
Correct |
4 ms |
2140 KB |
Output is correct |
22 |
Correct |
3 ms |
2140 KB |
Output is correct |
23 |
Correct |
4 ms |
2140 KB |
Output is correct |
24 |
Correct |
4 ms |
2136 KB |
Output is correct |
25 |
Correct |
1 ms |
452 KB |
Output is correct |
26 |
Correct |
4 ms |
2140 KB |
Output is correct |
27 |
Correct |
4 ms |
2140 KB |
Output is correct |
28 |
Correct |
4 ms |
2004 KB |
Output is correct |
29 |
Correct |
4 ms |
2140 KB |
Output is correct |
30 |
Correct |
4 ms |
1880 KB |
Output is correct |
31 |
Correct |
4 ms |
1884 KB |
Output is correct |
32 |
Correct |
4 ms |
1880 KB |
Output is correct |
33 |
Correct |
4 ms |
1884 KB |
Output is correct |
34 |
Correct |
170 ms |
72352 KB |
Output is correct |
35 |
Correct |
168 ms |
72180 KB |
Output is correct |
36 |
Correct |
166 ms |
72216 KB |
Output is correct |
37 |
Correct |
251 ms |
72272 KB |
Output is correct |
38 |
Correct |
265 ms |
72692 KB |
Output is correct |
39 |
Correct |
248 ms |
72436 KB |
Output is correct |
40 |
Correct |
250 ms |
72428 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
257 ms |
64608 KB |
Output is correct |
44 |
Correct |
290 ms |
64616 KB |
Output is correct |
45 |
Correct |
252 ms |
64596 KB |
Output is correct |
46 |
Correct |
372 ms |
68812 KB |
Output is correct |
47 |
Correct |
362 ms |
67748 KB |
Output is correct |
48 |
Correct |
403 ms |
69036 KB |
Output is correct |
49 |
Correct |
387 ms |
67896 KB |
Output is correct |
50 |
Correct |
309 ms |
64076 KB |
Output is correct |
51 |
Correct |
291 ms |
64080 KB |
Output is correct |
52 |
Correct |
329 ms |
64188 KB |
Output is correct |
53 |
Correct |
325 ms |
64084 KB |
Output is correct |