#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)
{
for(int j=1; j<22; j++)
bl[j][node]=bl[j-1][bl[j-1][node]];
for(auto i:G[node])
{
v[i]=v[node]+1;
bl[0][i]=node;
dfs(i);
}
}
ll cost(ll a, ll b)
{
ll a2=a,b2=b;
if(v[a]<v[b])
swap(a,b);
ll temp=v[a]-v[b];
for(int i=21; i>-1; i--)
if((temp&(1LL<<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];
}
}
if(a!=b) a=bl[0][a];
return v[a2]+v[b2]-2*v[a];
}
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;
G[a].push_back(b);
if(cad[a-1].first<cad[b-1].first) swap(a,b);
g[a].push_back(b);
}
dfs(1);
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);
sol[asd.fs(i)]=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++)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
45640 KB |
Output is correct |
2 |
Correct |
8 ms |
45660 KB |
Output is correct |
3 |
Correct |
9 ms |
45656 KB |
Output is correct |
4 |
Correct |
7 ms |
45656 KB |
Output is correct |
5 |
Correct |
7 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45660 KB |
Output is correct |
7 |
Correct |
7 ms |
45656 KB |
Output is correct |
8 |
Correct |
7 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45660 KB |
Output is correct |
10 |
Correct |
7 ms |
45660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
45640 KB |
Output is correct |
2 |
Correct |
8 ms |
45660 KB |
Output is correct |
3 |
Correct |
9 ms |
45656 KB |
Output is correct |
4 |
Correct |
7 ms |
45656 KB |
Output is correct |
5 |
Correct |
7 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45660 KB |
Output is correct |
7 |
Correct |
7 ms |
45656 KB |
Output is correct |
8 |
Correct |
7 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45660 KB |
Output is correct |
10 |
Correct |
7 ms |
45660 KB |
Output is correct |
11 |
Correct |
7 ms |
45656 KB |
Output is correct |
12 |
Correct |
7 ms |
45660 KB |
Output is correct |
13 |
Correct |
7 ms |
45696 KB |
Output is correct |
14 |
Correct |
9 ms |
45664 KB |
Output is correct |
15 |
Correct |
8 ms |
45660 KB |
Output is correct |
16 |
Correct |
7 ms |
45660 KB |
Output is correct |
17 |
Correct |
8 ms |
45704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
45640 KB |
Output is correct |
2 |
Correct |
8 ms |
45660 KB |
Output is correct |
3 |
Correct |
9 ms |
45656 KB |
Output is correct |
4 |
Correct |
7 ms |
45656 KB |
Output is correct |
5 |
Correct |
7 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45660 KB |
Output is correct |
7 |
Correct |
7 ms |
45656 KB |
Output is correct |
8 |
Correct |
7 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45660 KB |
Output is correct |
10 |
Correct |
7 ms |
45660 KB |
Output is correct |
11 |
Correct |
7 ms |
45656 KB |
Output is correct |
12 |
Correct |
7 ms |
45660 KB |
Output is correct |
13 |
Correct |
7 ms |
45696 KB |
Output is correct |
14 |
Correct |
9 ms |
45664 KB |
Output is correct |
15 |
Correct |
8 ms |
45660 KB |
Output is correct |
16 |
Correct |
7 ms |
45660 KB |
Output is correct |
17 |
Correct |
8 ms |
45704 KB |
Output is correct |
18 |
Correct |
11 ms |
46424 KB |
Output is correct |
19 |
Correct |
11 ms |
46428 KB |
Output is correct |
20 |
Correct |
11 ms |
46428 KB |
Output is correct |
21 |
Correct |
15 ms |
46408 KB |
Output is correct |
22 |
Correct |
13 ms |
46172 KB |
Output is correct |
23 |
Correct |
13 ms |
46168 KB |
Output is correct |
24 |
Correct |
12 ms |
46272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
45640 KB |
Output is correct |
2 |
Correct |
8 ms |
45660 KB |
Output is correct |
3 |
Correct |
9 ms |
45656 KB |
Output is correct |
4 |
Correct |
7 ms |
45656 KB |
Output is correct |
5 |
Correct |
7 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45660 KB |
Output is correct |
7 |
Correct |
7 ms |
45656 KB |
Output is correct |
8 |
Correct |
7 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45660 KB |
Output is correct |
10 |
Correct |
7 ms |
45660 KB |
Output is correct |
11 |
Correct |
7 ms |
45656 KB |
Output is correct |
12 |
Correct |
7 ms |
45660 KB |
Output is correct |
13 |
Correct |
7 ms |
45696 KB |
Output is correct |
14 |
Correct |
9 ms |
45664 KB |
Output is correct |
15 |
Correct |
8 ms |
45660 KB |
Output is correct |
16 |
Correct |
7 ms |
45660 KB |
Output is correct |
17 |
Correct |
8 ms |
45704 KB |
Output is correct |
18 |
Correct |
11 ms |
46424 KB |
Output is correct |
19 |
Correct |
11 ms |
46428 KB |
Output is correct |
20 |
Correct |
11 ms |
46428 KB |
Output is correct |
21 |
Correct |
15 ms |
46408 KB |
Output is correct |
22 |
Correct |
13 ms |
46172 KB |
Output is correct |
23 |
Correct |
13 ms |
46168 KB |
Output is correct |
24 |
Correct |
12 ms |
46272 KB |
Output is correct |
25 |
Correct |
8 ms |
45660 KB |
Output is correct |
26 |
Incorrect |
13 ms |
46088 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
45640 KB |
Output is correct |
2 |
Correct |
8 ms |
45660 KB |
Output is correct |
3 |
Correct |
9 ms |
45656 KB |
Output is correct |
4 |
Correct |
7 ms |
45656 KB |
Output is correct |
5 |
Correct |
7 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45660 KB |
Output is correct |
7 |
Correct |
7 ms |
45656 KB |
Output is correct |
8 |
Correct |
7 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45660 KB |
Output is correct |
10 |
Correct |
7 ms |
45660 KB |
Output is correct |
11 |
Correct |
7 ms |
45656 KB |
Output is correct |
12 |
Correct |
7 ms |
45660 KB |
Output is correct |
13 |
Correct |
7 ms |
45696 KB |
Output is correct |
14 |
Correct |
9 ms |
45664 KB |
Output is correct |
15 |
Correct |
8 ms |
45660 KB |
Output is correct |
16 |
Correct |
7 ms |
45660 KB |
Output is correct |
17 |
Correct |
8 ms |
45704 KB |
Output is correct |
18 |
Correct |
11 ms |
46424 KB |
Output is correct |
19 |
Correct |
11 ms |
46428 KB |
Output is correct |
20 |
Correct |
11 ms |
46428 KB |
Output is correct |
21 |
Correct |
15 ms |
46408 KB |
Output is correct |
22 |
Correct |
13 ms |
46172 KB |
Output is correct |
23 |
Correct |
13 ms |
46168 KB |
Output is correct |
24 |
Correct |
12 ms |
46272 KB |
Output is correct |
25 |
Correct |
221 ms |
75348 KB |
Output is correct |
26 |
Correct |
245 ms |
74836 KB |
Output is correct |
27 |
Correct |
238 ms |
75128 KB |
Output is correct |
28 |
Correct |
626 ms |
73484 KB |
Output is correct |
29 |
Correct |
609 ms |
73604 KB |
Output is correct |
30 |
Correct |
558 ms |
73488 KB |
Output is correct |
31 |
Correct |
582 ms |
73628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
45616 KB |
Output is correct |
2 |
Correct |
7 ms |
45660 KB |
Output is correct |
3 |
Correct |
522 ms |
59528 KB |
Output is correct |
4 |
Correct |
496 ms |
59524 KB |
Output is correct |
5 |
Correct |
528 ms |
59396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
45640 KB |
Output is correct |
2 |
Correct |
8 ms |
45660 KB |
Output is correct |
3 |
Correct |
9 ms |
45656 KB |
Output is correct |
4 |
Correct |
7 ms |
45656 KB |
Output is correct |
5 |
Correct |
7 ms |
45660 KB |
Output is correct |
6 |
Correct |
8 ms |
45660 KB |
Output is correct |
7 |
Correct |
7 ms |
45656 KB |
Output is correct |
8 |
Correct |
7 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45660 KB |
Output is correct |
10 |
Correct |
7 ms |
45660 KB |
Output is correct |
11 |
Correct |
7 ms |
45656 KB |
Output is correct |
12 |
Correct |
7 ms |
45660 KB |
Output is correct |
13 |
Correct |
7 ms |
45696 KB |
Output is correct |
14 |
Correct |
9 ms |
45664 KB |
Output is correct |
15 |
Correct |
8 ms |
45660 KB |
Output is correct |
16 |
Correct |
7 ms |
45660 KB |
Output is correct |
17 |
Correct |
8 ms |
45704 KB |
Output is correct |
18 |
Correct |
11 ms |
46424 KB |
Output is correct |
19 |
Correct |
11 ms |
46428 KB |
Output is correct |
20 |
Correct |
11 ms |
46428 KB |
Output is correct |
21 |
Correct |
15 ms |
46408 KB |
Output is correct |
22 |
Correct |
13 ms |
46172 KB |
Output is correct |
23 |
Correct |
13 ms |
46168 KB |
Output is correct |
24 |
Correct |
12 ms |
46272 KB |
Output is correct |
25 |
Correct |
8 ms |
45660 KB |
Output is correct |
26 |
Incorrect |
13 ms |
46088 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |