#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct DSUF{
vector<ll> P;
ll fs(ll node){
if(P[node]==node) return node;
return P[node]=fs(P[node]);
}
void us(ll a, ll b){
a=fs(a);b=fs(b);
if(a!=b) P[b]=a;
}
DSUF(ll n){
P.assign(n+5,0);
for(int i=0; i<P.size(); i++)
P[i]=i;
}
};
vector<ll> g[200001], v(200001,0),G[200001];
ll bl[22][200001];
void dfs(ll node,ll padre, ll p)
{
bl[0][node]=padre; v[node]=p;
for(int i=1; i<22; i++)
bl[i][node]=bl[i-1][bl[i-1][node]];
for(auto i:G[node])
if(padre!=i)
dfs(i,node,p+1);
}
ll cost(ll a, ll b)
{
if(v[a]<v[b])
swap(a,b);
ll cost=v[a]-v[b];
for(int i=21; i>-1; i--)
if((cost&(1<<i))!=0)
a=bl[i][a];
for(int i=21; i>-1; i--)
{
if(bl[i][a]!=bl[i][b])
{
a=bl[i][a];
b=bl[i][b];
cost+=(1<<i)*2;
}
}
if(a!=b) cost+=2;
return cost;
}
int main()
{
ll n;
cin>>n;
pair<ll,ll> cad[n];
DSUF asd(n+2);
for(int i=0; i<n; i++)
{
cin>>cad[i].first;
cad[i].second=i+1;
}
for(int i=0; i<n-1; i++)
{
ll a,b;
cin>>a>>b;
if(cad[a-1].first<cad[b-1].first) swap(a,b);
g[a].push_back(b);
G[a].push_back(b);
G[b].push_back(a);
}
dfs(1,0,0);
sort(cad,cad+n);
ll sol[n+2]={ };
for(int i=0; i<n; i++)
{
pair<ll,ll> temp=cad[i];
for(auto i:g[temp.second])
{
sol[temp.second]=max(sol[temp.second],sol[asd.fs(i)]+cost(temp.second,asd.fs(i)));
asd.us(temp.second,i);
}
//cout<<temp.second<<sol[temp.second]<<" ";
}
cout<<sol[cad[n-1].second];
}
Compilation message
Main.cpp: In constructor 'DSUF::DSUF(ll)':
Main.cpp:16:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int i=0; i<P.size(); i++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
45660 KB |
Output is correct |
2 |
Correct |
7 ms |
45660 KB |
Output is correct |
3 |
Correct |
8 ms |
45676 KB |
Output is correct |
4 |
Correct |
8 ms |
45660 KB |
Output is correct |
5 |
Correct |
9 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45632 KB |
Output is correct |
7 |
Correct |
7 ms |
45660 KB |
Output is correct |
8 |
Correct |
10 ms |
45680 KB |
Output is correct |
9 |
Correct |
7 ms |
45632 KB |
Output is correct |
10 |
Correct |
8 ms |
45660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
45660 KB |
Output is correct |
2 |
Correct |
7 ms |
45660 KB |
Output is correct |
3 |
Correct |
8 ms |
45676 KB |
Output is correct |
4 |
Correct |
8 ms |
45660 KB |
Output is correct |
5 |
Correct |
9 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45632 KB |
Output is correct |
7 |
Correct |
7 ms |
45660 KB |
Output is correct |
8 |
Correct |
10 ms |
45680 KB |
Output is correct |
9 |
Correct |
7 ms |
45632 KB |
Output is correct |
10 |
Correct |
8 ms |
45660 KB |
Output is correct |
11 |
Correct |
9 ms |
45660 KB |
Output is correct |
12 |
Correct |
7 ms |
45656 KB |
Output is correct |
13 |
Correct |
8 ms |
45620 KB |
Output is correct |
14 |
Correct |
7 ms |
45660 KB |
Output is correct |
15 |
Correct |
7 ms |
45672 KB |
Output is correct |
16 |
Correct |
7 ms |
45672 KB |
Output is correct |
17 |
Correct |
8 ms |
45936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
45660 KB |
Output is correct |
2 |
Correct |
7 ms |
45660 KB |
Output is correct |
3 |
Correct |
8 ms |
45676 KB |
Output is correct |
4 |
Correct |
8 ms |
45660 KB |
Output is correct |
5 |
Correct |
9 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45632 KB |
Output is correct |
7 |
Correct |
7 ms |
45660 KB |
Output is correct |
8 |
Correct |
10 ms |
45680 KB |
Output is correct |
9 |
Correct |
7 ms |
45632 KB |
Output is correct |
10 |
Correct |
8 ms |
45660 KB |
Output is correct |
11 |
Correct |
9 ms |
45660 KB |
Output is correct |
12 |
Correct |
7 ms |
45656 KB |
Output is correct |
13 |
Correct |
8 ms |
45620 KB |
Output is correct |
14 |
Correct |
7 ms |
45660 KB |
Output is correct |
15 |
Correct |
7 ms |
45672 KB |
Output is correct |
16 |
Correct |
7 ms |
45672 KB |
Output is correct |
17 |
Correct |
8 ms |
45936 KB |
Output is correct |
18 |
Correct |
12 ms |
46400 KB |
Output is correct |
19 |
Correct |
12 ms |
46428 KB |
Output is correct |
20 |
Correct |
12 ms |
46440 KB |
Output is correct |
21 |
Correct |
12 ms |
46368 KB |
Output is correct |
22 |
Correct |
12 ms |
46184 KB |
Output is correct |
23 |
Correct |
12 ms |
46184 KB |
Output is correct |
24 |
Correct |
12 ms |
46184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
45660 KB |
Output is correct |
2 |
Correct |
7 ms |
45660 KB |
Output is correct |
3 |
Correct |
8 ms |
45676 KB |
Output is correct |
4 |
Correct |
8 ms |
45660 KB |
Output is correct |
5 |
Correct |
9 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45632 KB |
Output is correct |
7 |
Correct |
7 ms |
45660 KB |
Output is correct |
8 |
Correct |
10 ms |
45680 KB |
Output is correct |
9 |
Correct |
7 ms |
45632 KB |
Output is correct |
10 |
Correct |
8 ms |
45660 KB |
Output is correct |
11 |
Correct |
9 ms |
45660 KB |
Output is correct |
12 |
Correct |
7 ms |
45656 KB |
Output is correct |
13 |
Correct |
8 ms |
45620 KB |
Output is correct |
14 |
Correct |
7 ms |
45660 KB |
Output is correct |
15 |
Correct |
7 ms |
45672 KB |
Output is correct |
16 |
Correct |
7 ms |
45672 KB |
Output is correct |
17 |
Correct |
8 ms |
45936 KB |
Output is correct |
18 |
Correct |
12 ms |
46400 KB |
Output is correct |
19 |
Correct |
12 ms |
46428 KB |
Output is correct |
20 |
Correct |
12 ms |
46440 KB |
Output is correct |
21 |
Correct |
12 ms |
46368 KB |
Output is correct |
22 |
Correct |
12 ms |
46184 KB |
Output is correct |
23 |
Correct |
12 ms |
46184 KB |
Output is correct |
24 |
Correct |
12 ms |
46184 KB |
Output is correct |
25 |
Correct |
7 ms |
45672 KB |
Output is correct |
26 |
Correct |
12 ms |
46580 KB |
Output is correct |
27 |
Correct |
12 ms |
46184 KB |
Output is correct |
28 |
Correct |
12 ms |
46140 KB |
Output is correct |
29 |
Correct |
12 ms |
46184 KB |
Output is correct |
30 |
Correct |
13 ms |
46120 KB |
Output is correct |
31 |
Correct |
13 ms |
46184 KB |
Output is correct |
32 |
Correct |
15 ms |
46184 KB |
Output is correct |
33 |
Correct |
12 ms |
46184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
45660 KB |
Output is correct |
2 |
Correct |
7 ms |
45660 KB |
Output is correct |
3 |
Correct |
8 ms |
45676 KB |
Output is correct |
4 |
Correct |
8 ms |
45660 KB |
Output is correct |
5 |
Correct |
9 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45632 KB |
Output is correct |
7 |
Correct |
7 ms |
45660 KB |
Output is correct |
8 |
Correct |
10 ms |
45680 KB |
Output is correct |
9 |
Correct |
7 ms |
45632 KB |
Output is correct |
10 |
Correct |
8 ms |
45660 KB |
Output is correct |
11 |
Correct |
9 ms |
45660 KB |
Output is correct |
12 |
Correct |
7 ms |
45656 KB |
Output is correct |
13 |
Correct |
8 ms |
45620 KB |
Output is correct |
14 |
Correct |
7 ms |
45660 KB |
Output is correct |
15 |
Correct |
7 ms |
45672 KB |
Output is correct |
16 |
Correct |
7 ms |
45672 KB |
Output is correct |
17 |
Correct |
8 ms |
45936 KB |
Output is correct |
18 |
Correct |
12 ms |
46400 KB |
Output is correct |
19 |
Correct |
12 ms |
46428 KB |
Output is correct |
20 |
Correct |
12 ms |
46440 KB |
Output is correct |
21 |
Correct |
12 ms |
46368 KB |
Output is correct |
22 |
Correct |
12 ms |
46184 KB |
Output is correct |
23 |
Correct |
12 ms |
46184 KB |
Output is correct |
24 |
Correct |
12 ms |
46184 KB |
Output is correct |
25 |
Correct |
221 ms |
75992 KB |
Output is correct |
26 |
Correct |
224 ms |
75620 KB |
Output is correct |
27 |
Correct |
231 ms |
76116 KB |
Output is correct |
28 |
Correct |
306 ms |
73968 KB |
Output is correct |
29 |
Correct |
320 ms |
74372 KB |
Output is correct |
30 |
Correct |
305 ms |
74068 KB |
Output is correct |
31 |
Correct |
302 ms |
74132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
45656 KB |
Output is correct |
2 |
Correct |
8 ms |
45660 KB |
Output is correct |
3 |
Correct |
315 ms |
67696 KB |
Output is correct |
4 |
Correct |
321 ms |
67672 KB |
Output is correct |
5 |
Correct |
304 ms |
67664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
45660 KB |
Output is correct |
2 |
Correct |
7 ms |
45660 KB |
Output is correct |
3 |
Correct |
8 ms |
45676 KB |
Output is correct |
4 |
Correct |
8 ms |
45660 KB |
Output is correct |
5 |
Correct |
9 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45632 KB |
Output is correct |
7 |
Correct |
7 ms |
45660 KB |
Output is correct |
8 |
Correct |
10 ms |
45680 KB |
Output is correct |
9 |
Correct |
7 ms |
45632 KB |
Output is correct |
10 |
Correct |
8 ms |
45660 KB |
Output is correct |
11 |
Correct |
9 ms |
45660 KB |
Output is correct |
12 |
Correct |
7 ms |
45656 KB |
Output is correct |
13 |
Correct |
8 ms |
45620 KB |
Output is correct |
14 |
Correct |
7 ms |
45660 KB |
Output is correct |
15 |
Correct |
7 ms |
45672 KB |
Output is correct |
16 |
Correct |
7 ms |
45672 KB |
Output is correct |
17 |
Correct |
8 ms |
45936 KB |
Output is correct |
18 |
Correct |
12 ms |
46400 KB |
Output is correct |
19 |
Correct |
12 ms |
46428 KB |
Output is correct |
20 |
Correct |
12 ms |
46440 KB |
Output is correct |
21 |
Correct |
12 ms |
46368 KB |
Output is correct |
22 |
Correct |
12 ms |
46184 KB |
Output is correct |
23 |
Correct |
12 ms |
46184 KB |
Output is correct |
24 |
Correct |
12 ms |
46184 KB |
Output is correct |
25 |
Correct |
7 ms |
45672 KB |
Output is correct |
26 |
Correct |
12 ms |
46580 KB |
Output is correct |
27 |
Correct |
12 ms |
46184 KB |
Output is correct |
28 |
Correct |
12 ms |
46140 KB |
Output is correct |
29 |
Correct |
12 ms |
46184 KB |
Output is correct |
30 |
Correct |
13 ms |
46120 KB |
Output is correct |
31 |
Correct |
13 ms |
46184 KB |
Output is correct |
32 |
Correct |
15 ms |
46184 KB |
Output is correct |
33 |
Correct |
12 ms |
46184 KB |
Output is correct |
34 |
Correct |
221 ms |
75992 KB |
Output is correct |
35 |
Correct |
224 ms |
75620 KB |
Output is correct |
36 |
Correct |
231 ms |
76116 KB |
Output is correct |
37 |
Correct |
306 ms |
73968 KB |
Output is correct |
38 |
Correct |
320 ms |
74372 KB |
Output is correct |
39 |
Correct |
305 ms |
74068 KB |
Output is correct |
40 |
Correct |
302 ms |
74132 KB |
Output is correct |
41 |
Correct |
8 ms |
45656 KB |
Output is correct |
42 |
Correct |
8 ms |
45660 KB |
Output is correct |
43 |
Correct |
315 ms |
67696 KB |
Output is correct |
44 |
Correct |
321 ms |
67672 KB |
Output is correct |
45 |
Correct |
304 ms |
67664 KB |
Output is correct |
46 |
Correct |
490 ms |
71764 KB |
Output is correct |
47 |
Correct |
491 ms |
70724 KB |
Output is correct |
48 |
Correct |
459 ms |
72108 KB |
Output is correct |
49 |
Correct |
488 ms |
70996 KB |
Output is correct |
50 |
Correct |
440 ms |
67408 KB |
Output is correct |
51 |
Correct |
442 ms |
67324 KB |
Output is correct |
52 |
Correct |
465 ms |
67148 KB |
Output is correct |
53 |
Correct |
450 ms |
67372 KB |
Output is correct |