//
// Created by 42kangaroo on 26/08/2023.
//
#include "bits/stdc++.h"
using namespace std;
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(int, int)':
Main.cpp:14:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for (int i = k + 1; i < a.size(); i += i & (-i)) {
| ~~^~~~~~~~~~
Main.cpp: In function 'g_t binJT(std::vector<int>&)':
Main.cpp:43:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 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 |
1876 KB |
Output is correct |
19 |
Correct |
4 ms |
1868 KB |
Output is correct |
20 |
Correct |
5 ms |
1808 KB |
Output is correct |
21 |
Correct |
5 ms |
1748 KB |
Output is correct |
22 |
Correct |
6 ms |
1584 KB |
Output is correct |
23 |
Correct |
5 ms |
1480 KB |
Output is correct |
24 |
Correct |
4 ms |
1492 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 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 |
1876 KB |
Output is correct |
19 |
Correct |
4 ms |
1868 KB |
Output is correct |
20 |
Correct |
5 ms |
1808 KB |
Output is correct |
21 |
Correct |
5 ms |
1748 KB |
Output is correct |
22 |
Correct |
6 ms |
1584 KB |
Output is correct |
23 |
Correct |
5 ms |
1480 KB |
Output is correct |
24 |
Correct |
4 ms |
1492 KB |
Output is correct |
25 |
Correct |
0 ms |
324 KB |
Output is correct |
26 |
Correct |
5 ms |
1688 KB |
Output is correct |
27 |
Correct |
5 ms |
1684 KB |
Output is correct |
28 |
Correct |
5 ms |
1748 KB |
Output is correct |
29 |
Correct |
5 ms |
1736 KB |
Output is correct |
30 |
Correct |
4 ms |
1228 KB |
Output is correct |
31 |
Correct |
4 ms |
1236 KB |
Output is correct |
32 |
Correct |
5 ms |
1148 KB |
Output is correct |
33 |
Correct |
5 ms |
1236 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 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 |
1876 KB |
Output is correct |
19 |
Correct |
4 ms |
1868 KB |
Output is correct |
20 |
Correct |
5 ms |
1808 KB |
Output is correct |
21 |
Correct |
5 ms |
1748 KB |
Output is correct |
22 |
Correct |
6 ms |
1584 KB |
Output is correct |
23 |
Correct |
5 ms |
1480 KB |
Output is correct |
24 |
Correct |
4 ms |
1492 KB |
Output is correct |
25 |
Incorrect |
226 ms |
63652 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
523 ms |
38500 KB |
Output is correct |
4 |
Correct |
467 ms |
38496 KB |
Output is correct |
5 |
Correct |
499 ms |
38496 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 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 |
1876 KB |
Output is correct |
19 |
Correct |
4 ms |
1868 KB |
Output is correct |
20 |
Correct |
5 ms |
1808 KB |
Output is correct |
21 |
Correct |
5 ms |
1748 KB |
Output is correct |
22 |
Correct |
6 ms |
1584 KB |
Output is correct |
23 |
Correct |
5 ms |
1480 KB |
Output is correct |
24 |
Correct |
4 ms |
1492 KB |
Output is correct |
25 |
Correct |
0 ms |
324 KB |
Output is correct |
26 |
Correct |
5 ms |
1688 KB |
Output is correct |
27 |
Correct |
5 ms |
1684 KB |
Output is correct |
28 |
Correct |
5 ms |
1748 KB |
Output is correct |
29 |
Correct |
5 ms |
1736 KB |
Output is correct |
30 |
Correct |
4 ms |
1228 KB |
Output is correct |
31 |
Correct |
4 ms |
1236 KB |
Output is correct |
32 |
Correct |
5 ms |
1148 KB |
Output is correct |
33 |
Correct |
5 ms |
1236 KB |
Output is correct |
34 |
Incorrect |
226 ms |
63652 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |