//
// 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];
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) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |