#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int up[N+1][20], h[N+1];
vector<int> adj[N+1];
bool vis[N+1];
void dfs(int u)
{
vis[u]=1;
for(int v : adj[u])
{
if(vis[v]) continue;
h[v]=h[u]+1;
up[v][0]=u;
for(int i=1;i<20;++i) up[v][i] = up[up[v][i-1]][i-1];
dfs(v);
}
}
int lca(int u,int v)
{
if(h[u]<h[v]) swap(u,v);
int k = h[u]-h[v];
for(int i=0;i<20;++i)
{
if(k>>i&1) u=up[u][i];
}
if(u==v) return u;
k=__lg(h[u]);
for(k;k>=0;--k)
{
if(up[u][k]!=up[v][k])
{
u=up[u][k];
v=up[v][k];
}
}
return up[u][0];
}
int a[N+1];
int dp[N+1];
int lab[N+1];
int mx[N+1];
int findset(int u)
{
if(lab[u]<0) return u;
else return lab[u]=findset(lab[u]);
}
void Merge(int s,int t)
{
if(lab[s]>lab[t]) swap(s,t);
lab[s]+=lab[t];
mx[s]=max(mx[s],mx[t]);
lab[t]=s;
}
void unite(int a,int b)
{
int s=findset(a);
int t=findset(b);
if(s!=t) Merge(s,t);
}
int dis(int u,int v)
{
int c = lca(u,v);
return h[u]+h[v]-2*h[c];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for(int i=1;i<=n;++i)
{
cin >> a[i];
lab[i]=-1;
mx[i]=i;
}
for(int i=1;i<n;++i)
{
int u,v;
cin >> u >> v;
adj[a[u]].push_back(a[v]);
adj[a[v]].push_back(a[u]);
}
h[1]=1;
dfs(1);
dp[1]=0;
for(int i=2;i<=n;++i)
{
for(int v : adj[i])
{
if(v<i)
{
int k = mx[findset(v)];
dp[i]=max(dp[i],dp[k]+dis(k,i));
unite(v,i);
}
}
}
cout << dp[n];
}
Compilation message
Main.cpp: In function 'int lca(int, int)':
Main.cpp:33:9: warning: statement has no effect [-Wunused-value]
33 | for(k;k>=0;--k)
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5020 KB |
Output is correct |
3 |
Correct |
2 ms |
5028 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5168 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5020 KB |
Output is correct |
3 |
Correct |
2 ms |
5028 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5168 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5172 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5020 KB |
Output is correct |
3 |
Correct |
2 ms |
5028 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5168 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5172 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
4 ms |
5976 KB |
Output is correct |
19 |
Correct |
4 ms |
6200 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
5 ms |
5980 KB |
Output is correct |
22 |
Correct |
5 ms |
5968 KB |
Output is correct |
23 |
Correct |
5 ms |
5980 KB |
Output is correct |
24 |
Correct |
4 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5020 KB |
Output is correct |
3 |
Correct |
2 ms |
5028 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5168 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5172 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
4 ms |
5976 KB |
Output is correct |
19 |
Correct |
4 ms |
6200 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
5 ms |
5980 KB |
Output is correct |
22 |
Correct |
5 ms |
5968 KB |
Output is correct |
23 |
Correct |
5 ms |
5980 KB |
Output is correct |
24 |
Correct |
4 ms |
6236 KB |
Output is correct |
25 |
Correct |
3 ms |
4952 KB |
Output is correct |
26 |
Correct |
5 ms |
5976 KB |
Output is correct |
27 |
Correct |
4 ms |
5980 KB |
Output is correct |
28 |
Correct |
4 ms |
5948 KB |
Output is correct |
29 |
Correct |
5 ms |
5948 KB |
Output is correct |
30 |
Correct |
5 ms |
5724 KB |
Output is correct |
31 |
Correct |
5 ms |
5720 KB |
Output is correct |
32 |
Correct |
5 ms |
5724 KB |
Output is correct |
33 |
Correct |
5 ms |
5724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5020 KB |
Output is correct |
3 |
Correct |
2 ms |
5028 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5168 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5172 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
4 ms |
5976 KB |
Output is correct |
19 |
Correct |
4 ms |
6200 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
5 ms |
5980 KB |
Output is correct |
22 |
Correct |
5 ms |
5968 KB |
Output is correct |
23 |
Correct |
5 ms |
5980 KB |
Output is correct |
24 |
Correct |
4 ms |
6236 KB |
Output is correct |
25 |
Incorrect |
82 ms |
40952 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
5168 KB |
Output is correct |
3 |
Correct |
119 ms |
34664 KB |
Output is correct |
4 |
Correct |
128 ms |
34640 KB |
Output is correct |
5 |
Correct |
112 ms |
34832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5020 KB |
Output is correct |
3 |
Correct |
2 ms |
5028 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5168 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5172 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
4 ms |
5976 KB |
Output is correct |
19 |
Correct |
4 ms |
6200 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
5 ms |
5980 KB |
Output is correct |
22 |
Correct |
5 ms |
5968 KB |
Output is correct |
23 |
Correct |
5 ms |
5980 KB |
Output is correct |
24 |
Correct |
4 ms |
6236 KB |
Output is correct |
25 |
Correct |
3 ms |
4952 KB |
Output is correct |
26 |
Correct |
5 ms |
5976 KB |
Output is correct |
27 |
Correct |
4 ms |
5980 KB |
Output is correct |
28 |
Correct |
4 ms |
5948 KB |
Output is correct |
29 |
Correct |
5 ms |
5948 KB |
Output is correct |
30 |
Correct |
5 ms |
5724 KB |
Output is correct |
31 |
Correct |
5 ms |
5720 KB |
Output is correct |
32 |
Correct |
5 ms |
5724 KB |
Output is correct |
33 |
Correct |
5 ms |
5724 KB |
Output is correct |
34 |
Incorrect |
82 ms |
40952 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |