#include <bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define int long long
const int maxn=2e5+9;
vector<int>a[maxn];
int b[maxn];
int rev[maxn];
int dep[maxn];
int st[maxn][19];
void dfs(int u,int par){
for (auto v:a[u]){
if (v==par)continue;
dep[v]=dep[u]+1;
st[v][0]=u;
for1(i,1,18){
st[v][i]=st[st[v][i-1]][i-1];
}
dfs(v,u);
}
}
long long f[maxn];
int lca(int u,int v){
if (dep[u]<dep[v])swap(u,v);
int k=dep[u]-dep[v];
for2(i,18,0){
if (k>>i&1)u=st[u][i];
}
if (u==v)return u;
for2(i,18,0){
if (!st[u][i]||!st[v][i])continue;
if (st[u][i]!=st[v][i]){
u=st[u][i];
v=st[v][i];
}
}
return st[u][0];
}
long long dis(int u,int v){
return dep[u]+dep[v]-2*dep[lca(u,v)];
}
int c[maxn],mx[maxn];
int findset(int u){
if (c[u]<0)return u;
return c[u]=findset(c[u]);
}
void unite(int u,int v){
u=findset(u),v=findset(v);
if (u==v)return ;
if (mx[u]<mx[v])swap(u,v);
f[u]=max(f[u],f[v]+dis(rev[mx[u]],rev[mx[v]]));
mx[u]=max(mx[u],mx[v]);
c[u]+=c[v];
c[v]=u;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int n;
cin>>n;
for1(i,1,n){
cin>>b[i];
rev[b[i]]=i;
}
for1(i,1,n-1){
int u,v;
cin>>u>>v;
a[u].pb(v);
a[v].pb(u);
}
int root;
for1(i,1,n)if (b[i]==n)root=i;
dfs(root,0);
for1(i,1,n){
c[i]=-1;
mx[i]=b[i];
}
for1(i,1,n){
for(auto v:a[rev[i]]){
if (b[v]<i){
unite(v,rev[i]);
}
}
}
cout<<f[findset(root)];
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:100:20: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
100 | cout<<f[findset(root)];
| ~~~~~~~^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4960 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4960 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5152 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4960 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5152 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
7 ms |
6572 KB |
Output is correct |
19 |
Correct |
5 ms |
6576 KB |
Output is correct |
20 |
Correct |
5 ms |
6568 KB |
Output is correct |
21 |
Correct |
5 ms |
6484 KB |
Output is correct |
22 |
Correct |
5 ms |
6356 KB |
Output is correct |
23 |
Correct |
5 ms |
6448 KB |
Output is correct |
24 |
Correct |
5 ms |
6356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4960 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5152 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
7 ms |
6572 KB |
Output is correct |
19 |
Correct |
5 ms |
6576 KB |
Output is correct |
20 |
Correct |
5 ms |
6568 KB |
Output is correct |
21 |
Correct |
5 ms |
6484 KB |
Output is correct |
22 |
Correct |
5 ms |
6356 KB |
Output is correct |
23 |
Correct |
5 ms |
6448 KB |
Output is correct |
24 |
Correct |
5 ms |
6356 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
6 ms |
6496 KB |
Output is correct |
27 |
Correct |
5 ms |
6448 KB |
Output is correct |
28 |
Correct |
6 ms |
6484 KB |
Output is correct |
29 |
Correct |
5 ms |
6484 KB |
Output is correct |
30 |
Correct |
6 ms |
6192 KB |
Output is correct |
31 |
Correct |
7 ms |
6228 KB |
Output is correct |
32 |
Correct |
5 ms |
6228 KB |
Output is correct |
33 |
Correct |
5 ms |
6184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4960 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5152 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
7 ms |
6572 KB |
Output is correct |
19 |
Correct |
5 ms |
6576 KB |
Output is correct |
20 |
Correct |
5 ms |
6568 KB |
Output is correct |
21 |
Correct |
5 ms |
6484 KB |
Output is correct |
22 |
Correct |
5 ms |
6356 KB |
Output is correct |
23 |
Correct |
5 ms |
6448 KB |
Output is correct |
24 |
Correct |
5 ms |
6356 KB |
Output is correct |
25 |
Correct |
121 ms |
66680 KB |
Output is correct |
26 |
Correct |
146 ms |
66608 KB |
Output is correct |
27 |
Correct |
126 ms |
66584 KB |
Output is correct |
28 |
Correct |
192 ms |
63572 KB |
Output is correct |
29 |
Correct |
164 ms |
65292 KB |
Output is correct |
30 |
Correct |
161 ms |
64716 KB |
Output is correct |
31 |
Correct |
205 ms |
65460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
169 ms |
55628 KB |
Output is correct |
4 |
Correct |
171 ms |
55628 KB |
Output is correct |
5 |
Correct |
180 ms |
55672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4960 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5152 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
7 ms |
6572 KB |
Output is correct |
19 |
Correct |
5 ms |
6576 KB |
Output is correct |
20 |
Correct |
5 ms |
6568 KB |
Output is correct |
21 |
Correct |
5 ms |
6484 KB |
Output is correct |
22 |
Correct |
5 ms |
6356 KB |
Output is correct |
23 |
Correct |
5 ms |
6448 KB |
Output is correct |
24 |
Correct |
5 ms |
6356 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
6 ms |
6496 KB |
Output is correct |
27 |
Correct |
5 ms |
6448 KB |
Output is correct |
28 |
Correct |
6 ms |
6484 KB |
Output is correct |
29 |
Correct |
5 ms |
6484 KB |
Output is correct |
30 |
Correct |
6 ms |
6192 KB |
Output is correct |
31 |
Correct |
7 ms |
6228 KB |
Output is correct |
32 |
Correct |
5 ms |
6228 KB |
Output is correct |
33 |
Correct |
5 ms |
6184 KB |
Output is correct |
34 |
Correct |
121 ms |
66680 KB |
Output is correct |
35 |
Correct |
146 ms |
66608 KB |
Output is correct |
36 |
Correct |
126 ms |
66584 KB |
Output is correct |
37 |
Correct |
192 ms |
63572 KB |
Output is correct |
38 |
Correct |
164 ms |
65292 KB |
Output is correct |
39 |
Correct |
161 ms |
64716 KB |
Output is correct |
40 |
Correct |
205 ms |
65460 KB |
Output is correct |
41 |
Correct |
2 ms |
4948 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
169 ms |
55628 KB |
Output is correct |
44 |
Correct |
171 ms |
55628 KB |
Output is correct |
45 |
Correct |
180 ms |
55672 KB |
Output is correct |
46 |
Correct |
326 ms |
63428 KB |
Output is correct |
47 |
Correct |
253 ms |
63432 KB |
Output is correct |
48 |
Correct |
289 ms |
64820 KB |
Output is correct |
49 |
Correct |
315 ms |
63392 KB |
Output is correct |
50 |
Correct |
256 ms |
55176 KB |
Output is correct |
51 |
Correct |
281 ms |
55220 KB |
Output is correct |
52 |
Correct |
340 ms |
55196 KB |
Output is correct |
53 |
Correct |
270 ms |
55172 KB |
Output is correct |