#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])
if(v[i]==0){
v[i]=v[node]+1;
bl[0][i]=node;
dfs(i);
}
}
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;
}
const long long Inf = 1e9 + 7;
struct LowerCommonAncestor
{
vector <int> L, E, H; int idx, n;
void dfs(int cur, int depth, int parent)
{
H[cur] = idx;
E[idx] = cur;
L[idx++] = depth;
for (auto& u : G[cur]) if(u != parent)
{
dfs(u, depth + 1, cur);
E[idx] = cur;
L[idx++] = depth;
}
}
vector <int> tree; int l;
int query(int node, int x, int y, int s, int e)
{
if (x > e || y < s) return 0;
if (s >= x && e <= y)
return tree[node];
int middle = (s + e) / 2;
int id1 = query(node * 2, x, y, s, middle),
id2 = query(node * 2 + 1, x, y, middle + 1, e);
return (L[id1] > L[id2] ? id2 : id1);
}
int query(int i, int j) { return query(1, i, j, 1, l); }
void Segment_tree(int n)
{
for (l = 1; l < n; l = (l << 1));
tree.assign(2 * l, 0); L[0] = Inf;
for (int i = 0; i < n; i++)
tree[i + l] = i + 1;
for (int i = l - 1; i > 0; i--)
{
tree[i] = (L[tree[2 * i]] > L[tree[2 * i + 1]] ?
tree[2 * i + 1] : tree[2 * i]);
}
}
int Ancestor(int i, int j)
{
int a = min(H[i], H[j]), b = max(H[i], H[j]);
return E[query(a, b)];
}
int Dist(int x)
{
return L[H[x]];
}
int sol(int i, int j)
{
int g = Ancestor(i, j);
return (Dist(i) + Dist(j) - (2 * Dist(g)));
}
LowerCommonAncestor(int lenght, int x)
{
n = lenght; idx = 1;
L.assign(2 * n, -1);
E.assign(2 * n, -1);
H.assign(2 * n, -1);
dfs(x, 0, 0); Segment_tree(2*n-2); //RMQ();
}
};
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);
}
LowerCommonAncestor D(n, 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)]+D.sol(temp.second,asd.fs(i)));
asd.us(temp.second,i);
}
}
cout<<sol[cad[n-1].second];
}
Compilation message
Main.cpp: In constructor 'DSUF::DSUF(ll)':
Main.cpp:17:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i=0; i<P.size(); i++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12632 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
4 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12632 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
4 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
12636 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12860 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
4 ms |
12856 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12632 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
4 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
12636 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12860 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
4 ms |
12856 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12860 KB |
Output is correct |
18 |
Correct |
8 ms |
13620 KB |
Output is correct |
19 |
Correct |
7 ms |
13660 KB |
Output is correct |
20 |
Correct |
7 ms |
13660 KB |
Output is correct |
21 |
Correct |
8 ms |
13660 KB |
Output is correct |
22 |
Correct |
8 ms |
13660 KB |
Output is correct |
23 |
Correct |
8 ms |
13640 KB |
Output is correct |
24 |
Correct |
10 ms |
13660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12632 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
4 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
12636 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12860 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
4 ms |
12856 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12860 KB |
Output is correct |
18 |
Correct |
8 ms |
13620 KB |
Output is correct |
19 |
Correct |
7 ms |
13660 KB |
Output is correct |
20 |
Correct |
7 ms |
13660 KB |
Output is correct |
21 |
Correct |
8 ms |
13660 KB |
Output is correct |
22 |
Correct |
8 ms |
13660 KB |
Output is correct |
23 |
Correct |
8 ms |
13640 KB |
Output is correct |
24 |
Correct |
10 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
12636 KB |
Output is correct |
26 |
Correct |
8 ms |
13660 KB |
Output is correct |
27 |
Correct |
8 ms |
13660 KB |
Output is correct |
28 |
Correct |
8 ms |
13660 KB |
Output is correct |
29 |
Correct |
8 ms |
13660 KB |
Output is correct |
30 |
Correct |
8 ms |
13516 KB |
Output is correct |
31 |
Correct |
8 ms |
13404 KB |
Output is correct |
32 |
Correct |
8 ms |
13616 KB |
Output is correct |
33 |
Correct |
9 ms |
13408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12632 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
4 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
12636 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12860 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
4 ms |
12856 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12860 KB |
Output is correct |
18 |
Correct |
8 ms |
13620 KB |
Output is correct |
19 |
Correct |
7 ms |
13660 KB |
Output is correct |
20 |
Correct |
7 ms |
13660 KB |
Output is correct |
21 |
Correct |
8 ms |
13660 KB |
Output is correct |
22 |
Correct |
8 ms |
13660 KB |
Output is correct |
23 |
Correct |
8 ms |
13640 KB |
Output is correct |
24 |
Correct |
10 ms |
13660 KB |
Output is correct |
25 |
Correct |
218 ms |
49108 KB |
Output is correct |
26 |
Correct |
227 ms |
48572 KB |
Output is correct |
27 |
Correct |
211 ms |
48584 KB |
Output is correct |
28 |
Correct |
245 ms |
46944 KB |
Output is correct |
29 |
Correct |
251 ms |
47060 KB |
Output is correct |
30 |
Correct |
250 ms |
47292 KB |
Output is correct |
31 |
Correct |
251 ms |
47180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
267 ms |
43192 KB |
Output is correct |
4 |
Correct |
281 ms |
43360 KB |
Output is correct |
5 |
Correct |
270 ms |
43364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12632 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
4 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
12636 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12860 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
4 ms |
12856 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12860 KB |
Output is correct |
18 |
Correct |
8 ms |
13620 KB |
Output is correct |
19 |
Correct |
7 ms |
13660 KB |
Output is correct |
20 |
Correct |
7 ms |
13660 KB |
Output is correct |
21 |
Correct |
8 ms |
13660 KB |
Output is correct |
22 |
Correct |
8 ms |
13660 KB |
Output is correct |
23 |
Correct |
8 ms |
13640 KB |
Output is correct |
24 |
Correct |
10 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
12636 KB |
Output is correct |
26 |
Correct |
8 ms |
13660 KB |
Output is correct |
27 |
Correct |
8 ms |
13660 KB |
Output is correct |
28 |
Correct |
8 ms |
13660 KB |
Output is correct |
29 |
Correct |
8 ms |
13660 KB |
Output is correct |
30 |
Correct |
8 ms |
13516 KB |
Output is correct |
31 |
Correct |
8 ms |
13404 KB |
Output is correct |
32 |
Correct |
8 ms |
13616 KB |
Output is correct |
33 |
Correct |
9 ms |
13408 KB |
Output is correct |
34 |
Correct |
218 ms |
49108 KB |
Output is correct |
35 |
Correct |
227 ms |
48572 KB |
Output is correct |
36 |
Correct |
211 ms |
48584 KB |
Output is correct |
37 |
Correct |
245 ms |
46944 KB |
Output is correct |
38 |
Correct |
251 ms |
47060 KB |
Output is correct |
39 |
Correct |
250 ms |
47292 KB |
Output is correct |
40 |
Correct |
251 ms |
47180 KB |
Output is correct |
41 |
Correct |
3 ms |
12632 KB |
Output is correct |
42 |
Correct |
3 ms |
12636 KB |
Output is correct |
43 |
Correct |
267 ms |
43192 KB |
Output is correct |
44 |
Correct |
281 ms |
43360 KB |
Output is correct |
45 |
Correct |
270 ms |
43364 KB |
Output is correct |
46 |
Correct |
288 ms |
45836 KB |
Output is correct |
47 |
Correct |
290 ms |
45272 KB |
Output is correct |
48 |
Correct |
351 ms |
46308 KB |
Output is correct |
49 |
Correct |
309 ms |
45408 KB |
Output is correct |
50 |
Correct |
338 ms |
43088 KB |
Output is correct |
51 |
Correct |
354 ms |
43324 KB |
Output is correct |
52 |
Correct |
331 ms |
42992 KB |
Output is correct |
53 |
Correct |
356 ms |
43348 KB |
Output is correct |