#include <bits/stdc++.h>
#define int long long
using namespace std;
const int Nmax=3e5;
int a[Nmax];
int nodes[Nmax],pos[Nmax],p[Nmax];
pair<int,int> maxi[Nmax];
int dp[Nmax];
int nodedepth[Nmax];
pair<int,int> rmq[35][2*Nmax];
vector<int> euler;
vector<int> depths;
vector<int> v[Nmax];
int n;
int sol=0;
int sef(int x)
{
if(p[x]==x) return x;
p[x]=sef(p[x]);
return p[x];
}
bool cmp(int x,int y)
{
return a[x]<a[y];
}
void dfs(int nod,int prev,int depth)
{
euler.push_back(nod);
depths.push_back(depth);
nodedepth[nod]=depth;
pos[nod]=euler.size()-1;
for(auto u:v[nod])
{
if(u!=prev)
{
dfs(u,nod,depth+1);
euler.push_back(nod);
depths.push_back(depth);
}
}
}int lg(int x)
{
return 31-__builtin_clz(x);
}
int query(int l,int r)
{
int len=lg(r-l+1);
return min(rmq[len][l],rmq[len][r-(1<<len)+1]).second;
}
int lca(int u,int v)
{
if(pos[u]>pos[v]) swap(u,v);
return query(pos[u],pos[v]);
}
void calclca()
{
dfs(1,0,0);
int k=lg(euler.size());
for(int j=0;j<euler.size();j++)
{
rmq[0][j]={depths[j],euler[j]};
}
for(int i=1;i<=k;i++)
{
for(int j=0;j+(1<<i)<euler.size();j++)
{
rmq[i][j]=min(rmq[i-1][j],rmq[i-1][j+(1<<(i-1))]);
}
}
}
int dist(int u,int v)
{
int nod=lca(u,v);
return nodedepth[u]+nodedepth[v]-2*nodedepth[nod];
}
void unite(int x,int y)
{
int sefx=sef(x);
int sefy=sef(y);
p[sefy]=sefx;
maxi[sefx]=max(maxi[sefx],maxi[sefy]);
return;
}
void activate(int nod)
{
p[nod]=nod;
maxi[nod]={a[nod],nod};
for(auto u:v[nod])
{
if(p[u]!=0)
{
int nxt=maxi[sef(u)].second;
dp[nod]=max(dp[nod],dist(nod,nxt)+dp[nxt]);
unite(nod,u);
}
}
}
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
nodes[i]=i;
}
sort(nodes+1,nodes+n+1,cmp);
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
calclca();
for(int i=1;i<=n;i++)
{
activate(nodes[i]);
}
cout<<dp[nodes[n]]<<'\n';
return 0;
}
Compilation message
Main.cpp: In function 'void calclca()':
Main.cpp:59:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int j=0;j<euler.size();j++)
| ~^~~~~~~~~~~~~
Main.cpp:65:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int j=0;j+(1<<i)<euler.size();j++)
| ~~~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25176 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
4 ms |
29328 KB |
Output is correct |
4 |
Correct |
4 ms |
29272 KB |
Output is correct |
5 |
Correct |
5 ms |
29284 KB |
Output is correct |
6 |
Correct |
4 ms |
29528 KB |
Output is correct |
7 |
Correct |
4 ms |
23132 KB |
Output is correct |
8 |
Correct |
5 ms |
29276 KB |
Output is correct |
9 |
Correct |
5 ms |
29200 KB |
Output is correct |
10 |
Correct |
5 ms |
29276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25176 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
4 ms |
29328 KB |
Output is correct |
4 |
Correct |
4 ms |
29272 KB |
Output is correct |
5 |
Correct |
5 ms |
29284 KB |
Output is correct |
6 |
Correct |
4 ms |
29528 KB |
Output is correct |
7 |
Correct |
4 ms |
23132 KB |
Output is correct |
8 |
Correct |
5 ms |
29276 KB |
Output is correct |
9 |
Correct |
5 ms |
29200 KB |
Output is correct |
10 |
Correct |
5 ms |
29276 KB |
Output is correct |
11 |
Correct |
9 ms |
39516 KB |
Output is correct |
12 |
Correct |
6 ms |
39516 KB |
Output is correct |
13 |
Correct |
6 ms |
39516 KB |
Output is correct |
14 |
Correct |
6 ms |
39512 KB |
Output is correct |
15 |
Correct |
7 ms |
39512 KB |
Output is correct |
16 |
Correct |
7 ms |
39516 KB |
Output is correct |
17 |
Correct |
7 ms |
39516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25176 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
4 ms |
29328 KB |
Output is correct |
4 |
Correct |
4 ms |
29272 KB |
Output is correct |
5 |
Correct |
5 ms |
29284 KB |
Output is correct |
6 |
Correct |
4 ms |
29528 KB |
Output is correct |
7 |
Correct |
4 ms |
23132 KB |
Output is correct |
8 |
Correct |
5 ms |
29276 KB |
Output is correct |
9 |
Correct |
5 ms |
29200 KB |
Output is correct |
10 |
Correct |
5 ms |
29276 KB |
Output is correct |
11 |
Correct |
9 ms |
39516 KB |
Output is correct |
12 |
Correct |
6 ms |
39516 KB |
Output is correct |
13 |
Correct |
6 ms |
39516 KB |
Output is correct |
14 |
Correct |
6 ms |
39512 KB |
Output is correct |
15 |
Correct |
7 ms |
39512 KB |
Output is correct |
16 |
Correct |
7 ms |
39516 KB |
Output is correct |
17 |
Correct |
7 ms |
39516 KB |
Output is correct |
18 |
Correct |
15 ms |
52660 KB |
Output is correct |
19 |
Correct |
11 ms |
52660 KB |
Output is correct |
20 |
Correct |
12 ms |
52572 KB |
Output is correct |
21 |
Correct |
13 ms |
52572 KB |
Output is correct |
22 |
Correct |
12 ms |
52724 KB |
Output is correct |
23 |
Correct |
14 ms |
52572 KB |
Output is correct |
24 |
Correct |
12 ms |
52728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25176 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
4 ms |
29328 KB |
Output is correct |
4 |
Correct |
4 ms |
29272 KB |
Output is correct |
5 |
Correct |
5 ms |
29284 KB |
Output is correct |
6 |
Correct |
4 ms |
29528 KB |
Output is correct |
7 |
Correct |
4 ms |
23132 KB |
Output is correct |
8 |
Correct |
5 ms |
29276 KB |
Output is correct |
9 |
Correct |
5 ms |
29200 KB |
Output is correct |
10 |
Correct |
5 ms |
29276 KB |
Output is correct |
11 |
Correct |
9 ms |
39516 KB |
Output is correct |
12 |
Correct |
6 ms |
39516 KB |
Output is correct |
13 |
Correct |
6 ms |
39516 KB |
Output is correct |
14 |
Correct |
6 ms |
39512 KB |
Output is correct |
15 |
Correct |
7 ms |
39512 KB |
Output is correct |
16 |
Correct |
7 ms |
39516 KB |
Output is correct |
17 |
Correct |
7 ms |
39516 KB |
Output is correct |
18 |
Correct |
15 ms |
52660 KB |
Output is correct |
19 |
Correct |
11 ms |
52660 KB |
Output is correct |
20 |
Correct |
12 ms |
52572 KB |
Output is correct |
21 |
Correct |
13 ms |
52572 KB |
Output is correct |
22 |
Correct |
12 ms |
52724 KB |
Output is correct |
23 |
Correct |
14 ms |
52572 KB |
Output is correct |
24 |
Correct |
12 ms |
52728 KB |
Output is correct |
25 |
Correct |
4 ms |
27228 KB |
Output is correct |
26 |
Correct |
12 ms |
52516 KB |
Output is correct |
27 |
Correct |
14 ms |
52572 KB |
Output is correct |
28 |
Correct |
11 ms |
52572 KB |
Output is correct |
29 |
Correct |
11 ms |
52572 KB |
Output is correct |
30 |
Correct |
12 ms |
52316 KB |
Output is correct |
31 |
Correct |
12 ms |
52312 KB |
Output is correct |
32 |
Correct |
12 ms |
52316 KB |
Output is correct |
33 |
Correct |
12 ms |
52316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25176 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
4 ms |
29328 KB |
Output is correct |
4 |
Correct |
4 ms |
29272 KB |
Output is correct |
5 |
Correct |
5 ms |
29284 KB |
Output is correct |
6 |
Correct |
4 ms |
29528 KB |
Output is correct |
7 |
Correct |
4 ms |
23132 KB |
Output is correct |
8 |
Correct |
5 ms |
29276 KB |
Output is correct |
9 |
Correct |
5 ms |
29200 KB |
Output is correct |
10 |
Correct |
5 ms |
29276 KB |
Output is correct |
11 |
Correct |
9 ms |
39516 KB |
Output is correct |
12 |
Correct |
6 ms |
39516 KB |
Output is correct |
13 |
Correct |
6 ms |
39516 KB |
Output is correct |
14 |
Correct |
6 ms |
39512 KB |
Output is correct |
15 |
Correct |
7 ms |
39512 KB |
Output is correct |
16 |
Correct |
7 ms |
39516 KB |
Output is correct |
17 |
Correct |
7 ms |
39516 KB |
Output is correct |
18 |
Correct |
15 ms |
52660 KB |
Output is correct |
19 |
Correct |
11 ms |
52660 KB |
Output is correct |
20 |
Correct |
12 ms |
52572 KB |
Output is correct |
21 |
Correct |
13 ms |
52572 KB |
Output is correct |
22 |
Correct |
12 ms |
52724 KB |
Output is correct |
23 |
Correct |
14 ms |
52572 KB |
Output is correct |
24 |
Correct |
12 ms |
52728 KB |
Output is correct |
25 |
Correct |
275 ms |
192680 KB |
Output is correct |
26 |
Correct |
215 ms |
193220 KB |
Output is correct |
27 |
Correct |
213 ms |
192712 KB |
Output is correct |
28 |
Correct |
274 ms |
193688 KB |
Output is correct |
29 |
Correct |
267 ms |
194720 KB |
Output is correct |
30 |
Correct |
266 ms |
194468 KB |
Output is correct |
31 |
Correct |
258 ms |
193364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
27224 KB |
Output is correct |
2 |
Correct |
5 ms |
37468 KB |
Output is correct |
3 |
Correct |
299 ms |
181768 KB |
Output is correct |
4 |
Correct |
269 ms |
181608 KB |
Output is correct |
5 |
Correct |
266 ms |
181588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25176 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
4 ms |
29328 KB |
Output is correct |
4 |
Correct |
4 ms |
29272 KB |
Output is correct |
5 |
Correct |
5 ms |
29284 KB |
Output is correct |
6 |
Correct |
4 ms |
29528 KB |
Output is correct |
7 |
Correct |
4 ms |
23132 KB |
Output is correct |
8 |
Correct |
5 ms |
29276 KB |
Output is correct |
9 |
Correct |
5 ms |
29200 KB |
Output is correct |
10 |
Correct |
5 ms |
29276 KB |
Output is correct |
11 |
Correct |
9 ms |
39516 KB |
Output is correct |
12 |
Correct |
6 ms |
39516 KB |
Output is correct |
13 |
Correct |
6 ms |
39516 KB |
Output is correct |
14 |
Correct |
6 ms |
39512 KB |
Output is correct |
15 |
Correct |
7 ms |
39512 KB |
Output is correct |
16 |
Correct |
7 ms |
39516 KB |
Output is correct |
17 |
Correct |
7 ms |
39516 KB |
Output is correct |
18 |
Correct |
15 ms |
52660 KB |
Output is correct |
19 |
Correct |
11 ms |
52660 KB |
Output is correct |
20 |
Correct |
12 ms |
52572 KB |
Output is correct |
21 |
Correct |
13 ms |
52572 KB |
Output is correct |
22 |
Correct |
12 ms |
52724 KB |
Output is correct |
23 |
Correct |
14 ms |
52572 KB |
Output is correct |
24 |
Correct |
12 ms |
52728 KB |
Output is correct |
25 |
Correct |
4 ms |
27228 KB |
Output is correct |
26 |
Correct |
12 ms |
52516 KB |
Output is correct |
27 |
Correct |
14 ms |
52572 KB |
Output is correct |
28 |
Correct |
11 ms |
52572 KB |
Output is correct |
29 |
Correct |
11 ms |
52572 KB |
Output is correct |
30 |
Correct |
12 ms |
52316 KB |
Output is correct |
31 |
Correct |
12 ms |
52312 KB |
Output is correct |
32 |
Correct |
12 ms |
52316 KB |
Output is correct |
33 |
Correct |
12 ms |
52316 KB |
Output is correct |
34 |
Correct |
275 ms |
192680 KB |
Output is correct |
35 |
Correct |
215 ms |
193220 KB |
Output is correct |
36 |
Correct |
213 ms |
192712 KB |
Output is correct |
37 |
Correct |
274 ms |
193688 KB |
Output is correct |
38 |
Correct |
267 ms |
194720 KB |
Output is correct |
39 |
Correct |
266 ms |
194468 KB |
Output is correct |
40 |
Correct |
258 ms |
193364 KB |
Output is correct |
41 |
Correct |
4 ms |
27224 KB |
Output is correct |
42 |
Correct |
5 ms |
37468 KB |
Output is correct |
43 |
Correct |
299 ms |
181768 KB |
Output is correct |
44 |
Correct |
269 ms |
181608 KB |
Output is correct |
45 |
Correct |
266 ms |
181588 KB |
Output is correct |
46 |
Correct |
271 ms |
187624 KB |
Output is correct |
47 |
Correct |
285 ms |
186516 KB |
Output is correct |
48 |
Correct |
281 ms |
188248 KB |
Output is correct |
49 |
Correct |
287 ms |
187336 KB |
Output is correct |
50 |
Correct |
329 ms |
181100 KB |
Output is correct |
51 |
Correct |
317 ms |
181076 KB |
Output is correct |
52 |
Correct |
313 ms |
181844 KB |
Output is correct |
53 |
Correct |
313 ms |
181332 KB |
Output is correct |