#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())
ll n;
ll a[200010];
pll b[200010];
ll all, bll;
vector<ll> vec[200010];
ll dep[200010];
ll spa[200010][21];
ll p[200010], ra[200010], maxx[200010];
ll dp[200010];
void dfs(ll here, ll pa)
{
dep[here] = dep[pa] + 1;
spa[here][0] = pa;
for(auto &i : vec[here])
{
if(i == pa)
continue;
dfs(i, here);
}
}
ll LCA(ll X, ll Y)
{
if(dep[X] < dep[Y])
swap(X, Y);
ll cha = dep[X] - dep[Y];
for(ll i = 20 ; i >= 0 ; i--)
{
if((1LL << i) <= cha)
{
cha -= (1LL << i);
X = spa[X][i];
}
}
if(X == Y)
return X;
for(ll i = 20 ; i >= 0 ; i--)
{
if(spa[X][i] != spa[Y][i])
{
X = spa[X][i];
Y = spa[Y][i];
}
}
return spa[X][0];
}
ll dist(ll X, ll Y)
{
return dep[X] + dep[Y] - dep[LCA(X, Y)] * 2;
}
ll ffind(ll here)
{
if(p[here] == here)
return p[here];
return p[here] = ffind(p[here]);
}
void uunion(ll X, ll Y)
{
X = ffind(X);
Y = ffind(Y);
if(ra[X] > ra[Y])
swap(X, Y);
if(ra[X] == ra[Y])
ra[Y]++;
p[X] = Y;
if(a[maxx[Y]] < a[maxx[X]])
maxx[Y] = maxx[X];
}
int main(void)
{
fastio
cin >> n;
for(ll i = 1 ; i <= n ; i++)
{
cin >> a[i];
b[i] = {a[i], i};
}
sort(b + 1, b + 1 + n);
for(ll i = 1 ; i < n ; i++)
{
cin >> all >> bll;
vec[all].push_back(bll);
vec[bll].push_back(all);
}
dfs(1, 0);
for(ll i = 1 ; i <= 20 ; i++)
{
for(ll j = 1 ; j <= n ; j++)
spa[j][i] = spa[spa[j][i - 1]][i - 1];
}
for(ll i = 1 ; i <= n ; i++)
p[i] = i, ra[i] = 0, maxx[i] = i;
for(ll i = 1 ; i <= n ; i++)
{
ll num = b[i].se;
for(auto &j : vec[num])
{
if(a[j] < a[num])
{
dp[num] = max(dp[num], dp[maxx[ffind(j)]] + dist(maxx[ffind(j)], num));
uunion(j, num);
}
}
}
cout << dp[b[n].se];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
5 ms |
6612 KB |
Output is correct |
19 |
Correct |
6 ms |
6612 KB |
Output is correct |
20 |
Correct |
5 ms |
6612 KB |
Output is correct |
21 |
Correct |
5 ms |
6612 KB |
Output is correct |
22 |
Correct |
5 ms |
6612 KB |
Output is correct |
23 |
Correct |
5 ms |
6612 KB |
Output is correct |
24 |
Correct |
5 ms |
6556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
5 ms |
6612 KB |
Output is correct |
19 |
Correct |
6 ms |
6612 KB |
Output is correct |
20 |
Correct |
5 ms |
6612 KB |
Output is correct |
21 |
Correct |
5 ms |
6612 KB |
Output is correct |
22 |
Correct |
5 ms |
6612 KB |
Output is correct |
23 |
Correct |
5 ms |
6612 KB |
Output is correct |
24 |
Correct |
5 ms |
6556 KB |
Output is correct |
25 |
Correct |
2 ms |
5076 KB |
Output is correct |
26 |
Correct |
5 ms |
6516 KB |
Output is correct |
27 |
Correct |
6 ms |
6588 KB |
Output is correct |
28 |
Correct |
5 ms |
6456 KB |
Output is correct |
29 |
Correct |
5 ms |
6612 KB |
Output is correct |
30 |
Correct |
6 ms |
6356 KB |
Output is correct |
31 |
Correct |
6 ms |
6456 KB |
Output is correct |
32 |
Correct |
5 ms |
6356 KB |
Output is correct |
33 |
Correct |
5 ms |
6356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
5 ms |
6612 KB |
Output is correct |
19 |
Correct |
6 ms |
6612 KB |
Output is correct |
20 |
Correct |
5 ms |
6612 KB |
Output is correct |
21 |
Correct |
5 ms |
6612 KB |
Output is correct |
22 |
Correct |
5 ms |
6612 KB |
Output is correct |
23 |
Correct |
5 ms |
6612 KB |
Output is correct |
24 |
Correct |
5 ms |
6556 KB |
Output is correct |
25 |
Correct |
167 ms |
69832 KB |
Output is correct |
26 |
Correct |
137 ms |
69804 KB |
Output is correct |
27 |
Correct |
133 ms |
69780 KB |
Output is correct |
28 |
Correct |
183 ms |
69844 KB |
Output is correct |
29 |
Correct |
207 ms |
69724 KB |
Output is correct |
30 |
Correct |
206 ms |
69808 KB |
Output is correct |
31 |
Correct |
185 ms |
69872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
214 ms |
61868 KB |
Output is correct |
4 |
Correct |
208 ms |
61780 KB |
Output is correct |
5 |
Correct |
212 ms |
61800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
5 ms |
6612 KB |
Output is correct |
19 |
Correct |
6 ms |
6612 KB |
Output is correct |
20 |
Correct |
5 ms |
6612 KB |
Output is correct |
21 |
Correct |
5 ms |
6612 KB |
Output is correct |
22 |
Correct |
5 ms |
6612 KB |
Output is correct |
23 |
Correct |
5 ms |
6612 KB |
Output is correct |
24 |
Correct |
5 ms |
6556 KB |
Output is correct |
25 |
Correct |
2 ms |
5076 KB |
Output is correct |
26 |
Correct |
5 ms |
6516 KB |
Output is correct |
27 |
Correct |
6 ms |
6588 KB |
Output is correct |
28 |
Correct |
5 ms |
6456 KB |
Output is correct |
29 |
Correct |
5 ms |
6612 KB |
Output is correct |
30 |
Correct |
6 ms |
6356 KB |
Output is correct |
31 |
Correct |
6 ms |
6456 KB |
Output is correct |
32 |
Correct |
5 ms |
6356 KB |
Output is correct |
33 |
Correct |
5 ms |
6356 KB |
Output is correct |
34 |
Correct |
167 ms |
69832 KB |
Output is correct |
35 |
Correct |
137 ms |
69804 KB |
Output is correct |
36 |
Correct |
133 ms |
69780 KB |
Output is correct |
37 |
Correct |
183 ms |
69844 KB |
Output is correct |
38 |
Correct |
207 ms |
69724 KB |
Output is correct |
39 |
Correct |
206 ms |
69808 KB |
Output is correct |
40 |
Correct |
185 ms |
69872 KB |
Output is correct |
41 |
Correct |
3 ms |
5032 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
214 ms |
61868 KB |
Output is correct |
44 |
Correct |
208 ms |
61780 KB |
Output is correct |
45 |
Correct |
212 ms |
61800 KB |
Output is correct |
46 |
Correct |
270 ms |
66100 KB |
Output is correct |
47 |
Correct |
279 ms |
65196 KB |
Output is correct |
48 |
Correct |
249 ms |
66564 KB |
Output is correct |
49 |
Correct |
257 ms |
65212 KB |
Output is correct |
50 |
Correct |
263 ms |
61488 KB |
Output is correct |
51 |
Correct |
273 ms |
61480 KB |
Output is correct |
52 |
Correct |
274 ms |
61460 KB |
Output is correct |
53 |
Correct |
248 ms |
61484 KB |
Output is correct |