#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <random>
#include <cassert>
using namespace std;
#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
typedef long long ll;
typedef long double ld;
struct DSU {
vector<set<int>> lol;
vector<int> p;
DSU(vector<set<int>> lol, int n) : lol(lol) {
p.resize(n);
iota(p.begin(), p.end(), 0);
}
int gt(int a) {
while (p[a] != a) a = p[a] = p[p[a]];
return a;
}
void unite(int a, int b) {
a = gt(a);
b = gt(b);
if (a == b) return;
if (lol[a].size() < lol[b].size()) swap(a, b);
for (auto e : lol[b]) lol[a].insert(e);
p[b] = a;
}
int ans(int a, int x) {
a = gt(a);
// cout << x << '\n';
// for (auto e : lol[a]) cout << e << ' ';
// cout << endl;
return (*lol[a].upper_bound(x));
}
};
struct SparseTable {
vector<vector<int>> sp;
SparseTable(){}
void build(vector<int> a) {
int n = a.size();
int lg2n = log2(n) + 2;
sp.resize(lg2n, vector<int>(n));
for (int i = 0; i < n; ++i) sp[0][i] = a[i];
for (int i = 1; i < lg2n; ++i) {
for (int j = 0; j < n; ++j) {
int t = min(n - 1, j + (1 << (i - 1)));
sp[i][j] = min(sp[i - 1][j], sp[i - 1][t]);
}
}
}
int gt(int l, int r) {
if (l > r) swap(l, r);
++r;
int t = log2(r - l);
return min(sp[t][l], sp[t][r - (1 << t)]);
}
};
struct LCA {
vector<int> ord;
vector<int> backord, h;
SparseTable lol;
vector<vector<int>> gr;
void dfs(int v, int pp) {
backord[v] = ord.size();
ord.push_back(h[v]);
for (auto e : gr[v]) {
if (e == pp) continue;
h[e] = h[v] + 1;
dfs(e, v);
ord.push_back(h[v]);
}
}
LCA(vector<vector<int>> gr) : gr(gr) {
int n = gr.size();
backord.resize(n);
h.resize(n);
dfs(0, -1);
lol.build(ord);
}
int get(int a, int b) {
return h[a] + h[b] - 2 * lol.gt(backord[a], backord[b]);
}
};
void solve() {
int n;
cin >> n;
vector<int> p(n);
for (int i = 0; i < n; ++i) cin >> p[i];
vector<vector<int>> gr(n);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a; --b;
gr[a].push_back(b);
gr[b].push_back(a);
}
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int i, int j) {return p[i] < p[j];});
vector<set<int>> lol(n);
for (int i = 0; i < n; ++i) {
for (auto e : gr[i]) {
if (p[e] > p[i]) {
lol[i].insert(p[e]);
}
}
}
DSU b(lol, n);
LCA kek(gr);
vector<int> dp(n + 1);
vector<int> fr(n + 1);
for (int i = 0; i < n - 1; ++i) {
int v = ord[i];
for (auto e : gr[v]) {
if (p[e] < p[v]) {
b.unite(v, e);
}
}
int u = ord[b.ans(v, p[v]) - 1];
fr[i + 1] = p[u];
// cout << fr[i] << endl;
// dp[i + 1] = dp[p[u]] + kek.get(v, u);
}
for (int i = n - 1; i >= 1; --i) {
dp[i] = dp[fr[i]] + kek.get(ord[i - 1], ord[fr[i] - 1]);
}
cout << *max_element(dp.begin(), dp.end()) << '\n';
}
signed main() {
int tc = 1;
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#else
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#endif
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
7 ms |
4208 KB |
Output is correct |
19 |
Correct |
6 ms |
3928 KB |
Output is correct |
20 |
Correct |
7 ms |
3940 KB |
Output is correct |
21 |
Correct |
9 ms |
4196 KB |
Output is correct |
22 |
Correct |
9 ms |
4180 KB |
Output is correct |
23 |
Correct |
8 ms |
4196 KB |
Output is correct |
24 |
Correct |
8 ms |
4196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
7 ms |
4208 KB |
Output is correct |
19 |
Correct |
6 ms |
3928 KB |
Output is correct |
20 |
Correct |
7 ms |
3940 KB |
Output is correct |
21 |
Correct |
9 ms |
4196 KB |
Output is correct |
22 |
Correct |
9 ms |
4180 KB |
Output is correct |
23 |
Correct |
8 ms |
4196 KB |
Output is correct |
24 |
Correct |
8 ms |
4196 KB |
Output is correct |
25 |
Correct |
1 ms |
764 KB |
Output is correct |
26 |
Correct |
7 ms |
3940 KB |
Output is correct |
27 |
Correct |
8 ms |
3940 KB |
Output is correct |
28 |
Correct |
8 ms |
3940 KB |
Output is correct |
29 |
Correct |
7 ms |
3940 KB |
Output is correct |
30 |
Correct |
8 ms |
3940 KB |
Output is correct |
31 |
Correct |
8 ms |
3940 KB |
Output is correct |
32 |
Correct |
8 ms |
3820 KB |
Output is correct |
33 |
Correct |
7 ms |
3940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
7 ms |
4208 KB |
Output is correct |
19 |
Correct |
6 ms |
3928 KB |
Output is correct |
20 |
Correct |
7 ms |
3940 KB |
Output is correct |
21 |
Correct |
9 ms |
4196 KB |
Output is correct |
22 |
Correct |
9 ms |
4180 KB |
Output is correct |
23 |
Correct |
8 ms |
4196 KB |
Output is correct |
24 |
Correct |
8 ms |
4196 KB |
Output is correct |
25 |
Correct |
229 ms |
162468 KB |
Output is correct |
26 |
Correct |
215 ms |
163152 KB |
Output is correct |
27 |
Correct |
231 ms |
163244 KB |
Output is correct |
28 |
Correct |
363 ms |
183460 KB |
Output is correct |
29 |
Correct |
379 ms |
183856 KB |
Output is correct |
30 |
Correct |
373 ms |
184460 KB |
Output is correct |
31 |
Correct |
374 ms |
184804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
340 ms |
157780 KB |
Output is correct |
4 |
Correct |
322 ms |
156804 KB |
Output is correct |
5 |
Correct |
317 ms |
157604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
7 ms |
4208 KB |
Output is correct |
19 |
Correct |
6 ms |
3928 KB |
Output is correct |
20 |
Correct |
7 ms |
3940 KB |
Output is correct |
21 |
Correct |
9 ms |
4196 KB |
Output is correct |
22 |
Correct |
9 ms |
4180 KB |
Output is correct |
23 |
Correct |
8 ms |
4196 KB |
Output is correct |
24 |
Correct |
8 ms |
4196 KB |
Output is correct |
25 |
Correct |
1 ms |
764 KB |
Output is correct |
26 |
Correct |
7 ms |
3940 KB |
Output is correct |
27 |
Correct |
8 ms |
3940 KB |
Output is correct |
28 |
Correct |
8 ms |
3940 KB |
Output is correct |
29 |
Correct |
7 ms |
3940 KB |
Output is correct |
30 |
Correct |
8 ms |
3940 KB |
Output is correct |
31 |
Correct |
8 ms |
3940 KB |
Output is correct |
32 |
Correct |
8 ms |
3820 KB |
Output is correct |
33 |
Correct |
7 ms |
3940 KB |
Output is correct |
34 |
Correct |
229 ms |
162468 KB |
Output is correct |
35 |
Correct |
215 ms |
163152 KB |
Output is correct |
36 |
Correct |
231 ms |
163244 KB |
Output is correct |
37 |
Correct |
363 ms |
183460 KB |
Output is correct |
38 |
Correct |
379 ms |
183856 KB |
Output is correct |
39 |
Correct |
373 ms |
184460 KB |
Output is correct |
40 |
Correct |
374 ms |
184804 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
340 ms |
157780 KB |
Output is correct |
44 |
Correct |
322 ms |
156804 KB |
Output is correct |
45 |
Correct |
317 ms |
157604 KB |
Output is correct |
46 |
Correct |
313 ms |
159836 KB |
Output is correct |
47 |
Correct |
337 ms |
159396 KB |
Output is correct |
48 |
Correct |
342 ms |
160416 KB |
Output is correct |
49 |
Correct |
315 ms |
159396 KB |
Output is correct |
50 |
Correct |
412 ms |
165284 KB |
Output is correct |
51 |
Correct |
405 ms |
162980 KB |
Output is correct |
52 |
Correct |
401 ms |
165044 KB |
Output is correct |
53 |
Correct |
389 ms |
162980 KB |
Output is correct |