#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<class A, class B>
ostream& operator<<(ostream& o, const pair<A, B>& p) {return o << '(' << p.first << ", " << p.second << ')';}
template<size_t Index = 0, typename... Types>
ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;}
template<typename... Types>
ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";}
template<class T>
auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';}
//#define DEBUG
#ifdef DEBUG
#define fastio()
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#define check(x) if (!(x)) { cerr << "Check failed: " << #x << " in line " << __LINE__ << endl; exit(1); }
#else
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0);
#define debug(...)
#define check(x)
#endif
typedef long long ll;
#define pi pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
constexpr int LIM = 2e5;
vi g[LIM + 10];
int tab[LIM + 10];
ll dp[LIM + 10];
int nxt[LIM + 10][18];
int pre[LIM + 10];
int post[LIM + 10];
int oj[LIM + 10];
int mx[LIM + 10];
int siz[LIM + 10];
int aktpre = 1;
int aktpost = 1;
void dfs(int v, int o) {
pre[v] = aktpre++;
nxt[v][0] = o;
for(int i = 1; i <= 17; i++) {
nxt[v][i] = nxt[nxt[v][i - 1]][i - 1];
}
for(auto x : g[v]) {
if(x != o) {
dfs(x, v);
}
}
post[v] = aktpost++;
}
bool czy(int a, int b) {
return pre[a] <= pre[b] && post[a] >= post[b];
}
int dist(int v, int o) {
if(v == o) {
return 0;
}
if(pre[v] < pre[o]) {
swap(v, o);
}
int ans = 0;
for(int i = 17; i >= 0; i--) {
if(!czy(nxt[v][i], o)) {
ans += (1 << i);
v = nxt[v][i];
}
}
if(!czy(v, o)) {
ans++;
v = nxt[v][0];
}
for(int i = 17; i >= 0; i--) {
if(!czy(nxt[o][i], v)) {
ans += (1 << i);
o = nxt[o][i];
}
}
if(!czy(o, v)) {
ans++;
o = nxt[o][0];
}
return ans;
}
int find(int a) {
if(oj[a] == a) {
return a;
}
return oj[a] = find(oj[a]);
}
void uni(int a, int b) {
if(a == b) {
return;
}
a = find(a);
b = find(b);
if(siz[a] > siz[b]) {
swap(a, b);
}
oj[a] = b;
siz[b] += siz[a];
mx[b] = max(mx[b], mx[a]);
}
void solve() {
//ifstream cin("nazwa.in");
//ofstream cout("nazwa.out");
int n;
cin >> n;
vector<pi>vs;
vi pos(n + 1, 0);
for(int i = 1; i <= n; i++) {
cin >> tab[i];
vs.eb(tab[i], i);
pos[tab[i]] = i;
mx[i] = tab[i];
oj[i] = i;
siz[i] = 1;
}
sort(all(vs));
for(int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].eb(b);
g[b].eb(a);
}
dfs(1, 1);
for(auto [val, v] : vs) {
for(auto x : g[v]) {
if(tab[x] < val) {
int a = pos[mx[find(x)]];
dp[v] = max(dp[v], dp[a] + dist(v, a));
uni(x, v);
}
}
debug(v, dp[v]);
if(val == n) {
cout << dp[v] << '\n';
return;
}
}
}
int main() {
fastio();
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10940 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
11104 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10940 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
11104 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10932 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10940 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
11104 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10932 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
4 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11356 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
4 ms |
11372 KB |
Output is correct |
22 |
Correct |
4 ms |
11608 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10940 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
11104 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10932 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
4 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11356 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
4 ms |
11372 KB |
Output is correct |
22 |
Correct |
4 ms |
11608 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11328 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
4 ms |
11356 KB |
Output is correct |
27 |
Correct |
4 ms |
11356 KB |
Output is correct |
28 |
Correct |
4 ms |
11356 KB |
Output is correct |
29 |
Correct |
4 ms |
11356 KB |
Output is correct |
30 |
Correct |
5 ms |
11200 KB |
Output is correct |
31 |
Correct |
4 ms |
11100 KB |
Output is correct |
32 |
Correct |
4 ms |
11100 KB |
Output is correct |
33 |
Correct |
4 ms |
11100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10940 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
11104 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10932 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
4 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11356 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
4 ms |
11372 KB |
Output is correct |
22 |
Correct |
4 ms |
11608 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11328 KB |
Output is correct |
25 |
Correct |
114 ms |
43296 KB |
Output is correct |
26 |
Correct |
106 ms |
43308 KB |
Output is correct |
27 |
Correct |
110 ms |
43464 KB |
Output is correct |
28 |
Correct |
155 ms |
43324 KB |
Output is correct |
29 |
Correct |
152 ms |
43428 KB |
Output is correct |
30 |
Correct |
191 ms |
43340 KB |
Output is correct |
31 |
Correct |
166 ms |
43464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
151 ms |
33996 KB |
Output is correct |
4 |
Correct |
166 ms |
34100 KB |
Output is correct |
5 |
Correct |
154 ms |
33980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10940 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
11104 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10932 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
4 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11356 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
4 ms |
11372 KB |
Output is correct |
22 |
Correct |
4 ms |
11608 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11328 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
4 ms |
11356 KB |
Output is correct |
27 |
Correct |
4 ms |
11356 KB |
Output is correct |
28 |
Correct |
4 ms |
11356 KB |
Output is correct |
29 |
Correct |
4 ms |
11356 KB |
Output is correct |
30 |
Correct |
5 ms |
11200 KB |
Output is correct |
31 |
Correct |
4 ms |
11100 KB |
Output is correct |
32 |
Correct |
4 ms |
11100 KB |
Output is correct |
33 |
Correct |
4 ms |
11100 KB |
Output is correct |
34 |
Correct |
114 ms |
43296 KB |
Output is correct |
35 |
Correct |
106 ms |
43308 KB |
Output is correct |
36 |
Correct |
110 ms |
43464 KB |
Output is correct |
37 |
Correct |
155 ms |
43324 KB |
Output is correct |
38 |
Correct |
152 ms |
43428 KB |
Output is correct |
39 |
Correct |
191 ms |
43340 KB |
Output is correct |
40 |
Correct |
166 ms |
43464 KB |
Output is correct |
41 |
Correct |
2 ms |
10844 KB |
Output is correct |
42 |
Correct |
2 ms |
10844 KB |
Output is correct |
43 |
Correct |
151 ms |
33996 KB |
Output is correct |
44 |
Correct |
166 ms |
34100 KB |
Output is correct |
45 |
Correct |
154 ms |
33980 KB |
Output is correct |
46 |
Correct |
207 ms |
39240 KB |
Output is correct |
47 |
Correct |
199 ms |
38348 KB |
Output is correct |
48 |
Correct |
245 ms |
40140 KB |
Output is correct |
49 |
Correct |
205 ms |
38308 KB |
Output is correct |
50 |
Correct |
210 ms |
34220 KB |
Output is correct |
51 |
Correct |
206 ms |
34252 KB |
Output is correct |
52 |
Correct |
208 ms |
34024 KB |
Output is correct |
53 |
Correct |
239 ms |
33996 KB |
Output is correct |