//
// Created by 42kangaroo on 26/08/2023.
//
#include "bits/stdc++.h"
using namespace std;
#define int long long
using g_t = vector<vector<int>>;
struct FenTr {
vector<int> a;
void inc(int k, int v) {
for (int i = k + 1; i < a.size(); i += i & (-i)) {
a[i] += v;
}
}
int get(int k) {
int res = 0;
for (int i = k + 1; i > 0; i -= i & (-i)) {
res += a[i];
}
return res;
}
};
void dfs(int n, int p, int &t, int d, g_t &gr, vector<int> &pre, vector<int> &po, vector<int> &pa, vector<int> &de) {
pre[n] = t++;
pa[n] = p;
de[n] = d;
for (auto e: gr[n]) {
if (e == p) continue;
dfs(e, n, t, d + 1, gr, pre, po, pa, de);
}
po[n] = t++;
}
g_t binJT(vector<int> &pa) {
g_t binT(20, vector<int>(pa.size()));
binT[0] = pa;
for (int i = 1; i < 20; ++i) {
for (int j = 0; j < pa.size(); ++j) {
binT[i][j] = binT[i - 1][binT[i - 1][j]];
}
}
return binT;
}
int up(int a, int k, g_t& binT) {
for (int i = 0; k; k >>= 1, i++) {
if (k & 1) a = binT[i][a];
}
return a;
}
int lca(int a, int b, g_t &binT, vector<int> &de) {
if (de[a] < de[b]) swap(a, b);
int k = de[a] - de[b];
a = up(a, k, binT);
if (a == b) return a;
for (int i = 19; i >= 0; --i) {
if (binT[i][a] != binT[i][b]) {
a = binT[i][a];
b = binT[i][b];
}
}
return binT[0][a];
}
int findFirst(int a, FenTr &tr, vector<int> &pr, g_t &binT) {
int num = tr.get(pr[a]);
for (int i = 19; i >= 0; --i) {
if (tr.get(pr[binT[i][a]]) == num) a = binT[i][a];
}
return a;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> h(n);
g_t gr(n);
int ro = 0;
for (int i = 0; i < n; ++i) {
cin >> h[i];
if (h[i] == n) ro = i;
}
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
gr[--a].push_back(--b);
gr[b].push_back(a);
}
vector<int> pre(n), po(n), pa(n), de(n);
int t = 0;
dfs(ro, ro, t, 0, gr, pre, po, pa, de);
g_t binT = binJT(pa);
FenTr tr{vector<int>(2 * n + 2, 0)};
vector<int> o(n);
std::iota(o.begin(), o.end(),0);
std::sort(o.begin(), o.end(), [&](int l, int r) {return h[l] > h[r];});
vector<int> dom(n, -1);
vector<int> fiDe(n, -1);
fiDe[ro] = 0;
for (int i = 1; i < n; ++i) {
int v = o[i];
int fi = findFirst(v, tr, pre, binT);
int und = up(v, de[v] - de[fi] - 1, binT);
if (dom[und] != -1) {
fi = dom[und];
}
fiDe[v] = fiDe[fi] + de[v] + de[fi] - 2*de[lca(v, fi, binT, de)];
dom[und] = v;
tr.inc(pre[v], 1);
tr.inc(po[v], -1);
}
cout << *max_element(fiDe.begin(), fiDe.end());
}
Compilation message
Main.cpp: In member function 'void FenTr::inc(long long int, long long int)':
Main.cpp:16:25: 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]
16 | for (int i = k + 1; i < a.size(); i += i & (-i)) {
| ~~^~~~~~~~~~
Main.cpp: In function 'g_t binJT(std::vector<long long int>&)':
Main.cpp:45:21: 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]
45 | for (int j = 0; j < pa.size(); ++j) {
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2304 KB |
Output is correct |
19 |
Correct |
4 ms |
2260 KB |
Output is correct |
20 |
Correct |
4 ms |
2260 KB |
Output is correct |
21 |
Correct |
5 ms |
2132 KB |
Output is correct |
22 |
Correct |
5 ms |
2004 KB |
Output is correct |
23 |
Correct |
5 ms |
2004 KB |
Output is correct |
24 |
Correct |
7 ms |
2004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2304 KB |
Output is correct |
19 |
Correct |
4 ms |
2260 KB |
Output is correct |
20 |
Correct |
4 ms |
2260 KB |
Output is correct |
21 |
Correct |
5 ms |
2132 KB |
Output is correct |
22 |
Correct |
5 ms |
2004 KB |
Output is correct |
23 |
Correct |
5 ms |
2004 KB |
Output is correct |
24 |
Correct |
7 ms |
2004 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
5 ms |
2132 KB |
Output is correct |
27 |
Correct |
6 ms |
2132 KB |
Output is correct |
28 |
Correct |
5 ms |
2132 KB |
Output is correct |
29 |
Correct |
5 ms |
2132 KB |
Output is correct |
30 |
Correct |
5 ms |
1748 KB |
Output is correct |
31 |
Correct |
5 ms |
1748 KB |
Output is correct |
32 |
Correct |
5 ms |
1748 KB |
Output is correct |
33 |
Correct |
5 ms |
1800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2304 KB |
Output is correct |
19 |
Correct |
4 ms |
2260 KB |
Output is correct |
20 |
Correct |
4 ms |
2260 KB |
Output is correct |
21 |
Correct |
5 ms |
2132 KB |
Output is correct |
22 |
Correct |
5 ms |
2004 KB |
Output is correct |
23 |
Correct |
5 ms |
2004 KB |
Output is correct |
24 |
Correct |
7 ms |
2004 KB |
Output is correct |
25 |
Correct |
231 ms |
80132 KB |
Output is correct |
26 |
Correct |
258 ms |
83984 KB |
Output is correct |
27 |
Correct |
245 ms |
83984 KB |
Output is correct |
28 |
Correct |
835 ms |
78496 KB |
Output is correct |
29 |
Correct |
793 ms |
81508 KB |
Output is correct |
30 |
Correct |
747 ms |
80620 KB |
Output is correct |
31 |
Correct |
818 ms |
81788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
654 ms |
59844 KB |
Output is correct |
4 |
Correct |
699 ms |
59848 KB |
Output is correct |
5 |
Correct |
681 ms |
59952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2304 KB |
Output is correct |
19 |
Correct |
4 ms |
2260 KB |
Output is correct |
20 |
Correct |
4 ms |
2260 KB |
Output is correct |
21 |
Correct |
5 ms |
2132 KB |
Output is correct |
22 |
Correct |
5 ms |
2004 KB |
Output is correct |
23 |
Correct |
5 ms |
2004 KB |
Output is correct |
24 |
Correct |
7 ms |
2004 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
5 ms |
2132 KB |
Output is correct |
27 |
Correct |
6 ms |
2132 KB |
Output is correct |
28 |
Correct |
5 ms |
2132 KB |
Output is correct |
29 |
Correct |
5 ms |
2132 KB |
Output is correct |
30 |
Correct |
5 ms |
1748 KB |
Output is correct |
31 |
Correct |
5 ms |
1748 KB |
Output is correct |
32 |
Correct |
5 ms |
1748 KB |
Output is correct |
33 |
Correct |
5 ms |
1800 KB |
Output is correct |
34 |
Correct |
231 ms |
80132 KB |
Output is correct |
35 |
Correct |
258 ms |
83984 KB |
Output is correct |
36 |
Correct |
245 ms |
83984 KB |
Output is correct |
37 |
Correct |
835 ms |
78496 KB |
Output is correct |
38 |
Correct |
793 ms |
81508 KB |
Output is correct |
39 |
Correct |
747 ms |
80620 KB |
Output is correct |
40 |
Correct |
818 ms |
81788 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
654 ms |
59844 KB |
Output is correct |
44 |
Correct |
699 ms |
59848 KB |
Output is correct |
45 |
Correct |
681 ms |
59952 KB |
Output is correct |
46 |
Correct |
696 ms |
77876 KB |
Output is correct |
47 |
Correct |
643 ms |
77872 KB |
Output is correct |
48 |
Correct |
748 ms |
80084 KB |
Output is correct |
49 |
Correct |
696 ms |
77888 KB |
Output is correct |
50 |
Correct |
866 ms |
63148 KB |
Output is correct |
51 |
Correct |
841 ms |
63124 KB |
Output is correct |
52 |
Correct |
839 ms |
63136 KB |
Output is correct |
53 |
Correct |
841 ms |
63128 KB |
Output is correct |