#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
struct custom_hash {
size_t operator()(uint64_t x) const {
return (x + 228) * 239;
}
};
const int MAX_N = 2e5 + 5;
const int MAX_L = __lg(MAX_N) + 1;
bitset<MAX_N> used_c, used_s;
gp_hash_table<int, int> pos[MAX_N];
vector<int> graph[MAX_N], parent[MAX_N], dist[MAX_N];
vector<char> deleted[MAX_N];
int ancestors[MAX_N][MAX_L], num[MAX_N] = {}, value[MAX_N], sz[MAX_N];
struct cmp {
bool operator()(int x, int y) const {
return value[x] < value[y];
}
};
set<int, cmp> values[MAX_N];
void find_sizes(int u, int p) {
sz[u] = 1;
for (int v : graph[u]) {
if (v != p && !used_c[v]) {
find_sizes(v, u);
sz[u] += sz[v];
}
}
}
int find_centroid(int u, int p, int n) {
for (int v : graph[u]) {
if (v != p && !used_c[v] && sz[v] > n / 2) {
return find_centroid(v, u, n);
}
}
return u;
}
void DFS(int u, int c, int p, int d) {
pos[c][u] = (int)dist[c].size();
dist[c].push_back(d);
parent[c].push_back(p);
deleted[c].push_back(false);
values[c].insert(u);
ancestors[u][num[u]++] = c;
for (int v : graph[u]) {
if (v != p && !used_c[v]) {
DFS(v, c, u, d + 1);
}
}
}
void centroid(int u) {
find_sizes(u, -1);
DFS(u, u, -1, 0);
used_c[u] = true;
for (int v : graph[u]) {
if (!used_c[v]) {
centroid(find_centroid(v, u, sz[v]));
}
}
}
void delete_subtree(int u, int p, int c) {
values[c].erase(u);
deleted[c][pos[c][u]] = true;
for (int v : graph[u]) {
if (v != p && pos[c].find(v) != pos[c].end() && !deleted[c][pos[c][v]]) {
delete_subtree(v, u, c);
}
}
}
int64_t solve(int u) {
used_s[u] = true;
int64_t ans = 0;
for (int i = 0; i < num[u]; i++) {
int A = ancestors[u][i];
if (!deleted[A][pos[A][u]]) {
delete_subtree(u, parent[A][pos[A][u]], A);
}
}
for (int v : graph[u]) {
if (!used_s[v]) {
tuple<int, int, int> val = {-1, -1, -1};
for (int i = 0; i < num[v]; i++) {
int A = ancestors[v][i];
if (!deleted[A][pos[A][v]] && !values[A].empty()) {
int x = *values[A].rbegin();
val = max(val, {value[x], -(dist[A][pos[A][x]] + dist[A][pos[A][v]]), x});
}
}
ans = max(ans, solve(get<2>(val)) - get<1>(val) + 1);
}
}
return ans;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> value[i];
}
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
find_sizes(0, -1);
centroid(find_centroid(0, -1, N));
for (int i = 0; i < N; i++) {
if (value[i] == N) {
cout << solve(i) << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
70728 KB |
Output is correct |
2 |
Correct |
45 ms |
70804 KB |
Output is correct |
3 |
Correct |
41 ms |
70792 KB |
Output is correct |
4 |
Correct |
41 ms |
70760 KB |
Output is correct |
5 |
Correct |
41 ms |
70796 KB |
Output is correct |
6 |
Correct |
40 ms |
70740 KB |
Output is correct |
7 |
Correct |
41 ms |
70744 KB |
Output is correct |
8 |
Correct |
42 ms |
70772 KB |
Output is correct |
9 |
Correct |
40 ms |
70808 KB |
Output is correct |
10 |
Correct |
42 ms |
70720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
70728 KB |
Output is correct |
2 |
Correct |
45 ms |
70804 KB |
Output is correct |
3 |
Correct |
41 ms |
70792 KB |
Output is correct |
4 |
Correct |
41 ms |
70760 KB |
Output is correct |
5 |
Correct |
41 ms |
70796 KB |
Output is correct |
6 |
Correct |
40 ms |
70740 KB |
Output is correct |
7 |
Correct |
41 ms |
70744 KB |
Output is correct |
8 |
Correct |
42 ms |
70772 KB |
Output is correct |
9 |
Correct |
40 ms |
70808 KB |
Output is correct |
10 |
Correct |
42 ms |
70720 KB |
Output is correct |
11 |
Correct |
44 ms |
71116 KB |
Output is correct |
12 |
Correct |
48 ms |
71260 KB |
Output is correct |
13 |
Correct |
45 ms |
71092 KB |
Output is correct |
14 |
Correct |
45 ms |
70976 KB |
Output is correct |
15 |
Correct |
44 ms |
71044 KB |
Output is correct |
16 |
Correct |
44 ms |
71068 KB |
Output is correct |
17 |
Correct |
45 ms |
70996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
70728 KB |
Output is correct |
2 |
Correct |
45 ms |
70804 KB |
Output is correct |
3 |
Correct |
41 ms |
70792 KB |
Output is correct |
4 |
Correct |
41 ms |
70760 KB |
Output is correct |
5 |
Correct |
41 ms |
70796 KB |
Output is correct |
6 |
Correct |
40 ms |
70740 KB |
Output is correct |
7 |
Correct |
41 ms |
70744 KB |
Output is correct |
8 |
Correct |
42 ms |
70772 KB |
Output is correct |
9 |
Correct |
40 ms |
70808 KB |
Output is correct |
10 |
Correct |
42 ms |
70720 KB |
Output is correct |
11 |
Correct |
44 ms |
71116 KB |
Output is correct |
12 |
Correct |
48 ms |
71260 KB |
Output is correct |
13 |
Correct |
45 ms |
71092 KB |
Output is correct |
14 |
Correct |
45 ms |
70976 KB |
Output is correct |
15 |
Correct |
44 ms |
71044 KB |
Output is correct |
16 |
Correct |
44 ms |
71068 KB |
Output is correct |
17 |
Correct |
45 ms |
70996 KB |
Output is correct |
18 |
Correct |
64 ms |
77900 KB |
Output is correct |
19 |
Correct |
65 ms |
77712 KB |
Output is correct |
20 |
Correct |
64 ms |
77744 KB |
Output is correct |
21 |
Correct |
67 ms |
77440 KB |
Output is correct |
22 |
Correct |
68 ms |
77388 KB |
Output is correct |
23 |
Correct |
66 ms |
77392 KB |
Output is correct |
24 |
Correct |
68 ms |
77504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
70728 KB |
Output is correct |
2 |
Correct |
45 ms |
70804 KB |
Output is correct |
3 |
Correct |
41 ms |
70792 KB |
Output is correct |
4 |
Correct |
41 ms |
70760 KB |
Output is correct |
5 |
Correct |
41 ms |
70796 KB |
Output is correct |
6 |
Correct |
40 ms |
70740 KB |
Output is correct |
7 |
Correct |
41 ms |
70744 KB |
Output is correct |
8 |
Correct |
42 ms |
70772 KB |
Output is correct |
9 |
Correct |
40 ms |
70808 KB |
Output is correct |
10 |
Correct |
42 ms |
70720 KB |
Output is correct |
11 |
Correct |
44 ms |
71116 KB |
Output is correct |
12 |
Correct |
48 ms |
71260 KB |
Output is correct |
13 |
Correct |
45 ms |
71092 KB |
Output is correct |
14 |
Correct |
45 ms |
70976 KB |
Output is correct |
15 |
Correct |
44 ms |
71044 KB |
Output is correct |
16 |
Correct |
44 ms |
71068 KB |
Output is correct |
17 |
Correct |
45 ms |
70996 KB |
Output is correct |
18 |
Correct |
64 ms |
77900 KB |
Output is correct |
19 |
Correct |
65 ms |
77712 KB |
Output is correct |
20 |
Correct |
64 ms |
77744 KB |
Output is correct |
21 |
Correct |
67 ms |
77440 KB |
Output is correct |
22 |
Correct |
68 ms |
77388 KB |
Output is correct |
23 |
Correct |
66 ms |
77392 KB |
Output is correct |
24 |
Correct |
68 ms |
77504 KB |
Output is correct |
25 |
Correct |
43 ms |
70852 KB |
Output is correct |
26 |
Correct |
68 ms |
77484 KB |
Output is correct |
27 |
Correct |
72 ms |
77420 KB |
Output is correct |
28 |
Correct |
70 ms |
77404 KB |
Output is correct |
29 |
Correct |
69 ms |
77496 KB |
Output is correct |
30 |
Correct |
65 ms |
75596 KB |
Output is correct |
31 |
Correct |
65 ms |
75268 KB |
Output is correct |
32 |
Correct |
65 ms |
75596 KB |
Output is correct |
33 |
Correct |
65 ms |
75396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
70728 KB |
Output is correct |
2 |
Correct |
45 ms |
70804 KB |
Output is correct |
3 |
Correct |
41 ms |
70792 KB |
Output is correct |
4 |
Correct |
41 ms |
70760 KB |
Output is correct |
5 |
Correct |
41 ms |
70796 KB |
Output is correct |
6 |
Correct |
40 ms |
70740 KB |
Output is correct |
7 |
Correct |
41 ms |
70744 KB |
Output is correct |
8 |
Correct |
42 ms |
70772 KB |
Output is correct |
9 |
Correct |
40 ms |
70808 KB |
Output is correct |
10 |
Correct |
42 ms |
70720 KB |
Output is correct |
11 |
Correct |
44 ms |
71116 KB |
Output is correct |
12 |
Correct |
48 ms |
71260 KB |
Output is correct |
13 |
Correct |
45 ms |
71092 KB |
Output is correct |
14 |
Correct |
45 ms |
70976 KB |
Output is correct |
15 |
Correct |
44 ms |
71044 KB |
Output is correct |
16 |
Correct |
44 ms |
71068 KB |
Output is correct |
17 |
Correct |
45 ms |
70996 KB |
Output is correct |
18 |
Correct |
64 ms |
77900 KB |
Output is correct |
19 |
Correct |
65 ms |
77712 KB |
Output is correct |
20 |
Correct |
64 ms |
77744 KB |
Output is correct |
21 |
Correct |
67 ms |
77440 KB |
Output is correct |
22 |
Correct |
68 ms |
77388 KB |
Output is correct |
23 |
Correct |
66 ms |
77392 KB |
Output is correct |
24 |
Correct |
68 ms |
77504 KB |
Output is correct |
25 |
Correct |
1338 ms |
427424 KB |
Output is correct |
26 |
Correct |
1355 ms |
424564 KB |
Output is correct |
27 |
Correct |
1383 ms |
424624 KB |
Output is correct |
28 |
Correct |
1645 ms |
411788 KB |
Output is correct |
29 |
Correct |
1700 ms |
411908 KB |
Output is correct |
30 |
Correct |
1717 ms |
411780 KB |
Output is correct |
31 |
Correct |
1715 ms |
411836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
70724 KB |
Output is correct |
2 |
Correct |
57 ms |
70916 KB |
Output is correct |
3 |
Execution timed out |
2075 ms |
209504 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
70728 KB |
Output is correct |
2 |
Correct |
45 ms |
70804 KB |
Output is correct |
3 |
Correct |
41 ms |
70792 KB |
Output is correct |
4 |
Correct |
41 ms |
70760 KB |
Output is correct |
5 |
Correct |
41 ms |
70796 KB |
Output is correct |
6 |
Correct |
40 ms |
70740 KB |
Output is correct |
7 |
Correct |
41 ms |
70744 KB |
Output is correct |
8 |
Correct |
42 ms |
70772 KB |
Output is correct |
9 |
Correct |
40 ms |
70808 KB |
Output is correct |
10 |
Correct |
42 ms |
70720 KB |
Output is correct |
11 |
Correct |
44 ms |
71116 KB |
Output is correct |
12 |
Correct |
48 ms |
71260 KB |
Output is correct |
13 |
Correct |
45 ms |
71092 KB |
Output is correct |
14 |
Correct |
45 ms |
70976 KB |
Output is correct |
15 |
Correct |
44 ms |
71044 KB |
Output is correct |
16 |
Correct |
44 ms |
71068 KB |
Output is correct |
17 |
Correct |
45 ms |
70996 KB |
Output is correct |
18 |
Correct |
64 ms |
77900 KB |
Output is correct |
19 |
Correct |
65 ms |
77712 KB |
Output is correct |
20 |
Correct |
64 ms |
77744 KB |
Output is correct |
21 |
Correct |
67 ms |
77440 KB |
Output is correct |
22 |
Correct |
68 ms |
77388 KB |
Output is correct |
23 |
Correct |
66 ms |
77392 KB |
Output is correct |
24 |
Correct |
68 ms |
77504 KB |
Output is correct |
25 |
Correct |
43 ms |
70852 KB |
Output is correct |
26 |
Correct |
68 ms |
77484 KB |
Output is correct |
27 |
Correct |
72 ms |
77420 KB |
Output is correct |
28 |
Correct |
70 ms |
77404 KB |
Output is correct |
29 |
Correct |
69 ms |
77496 KB |
Output is correct |
30 |
Correct |
65 ms |
75596 KB |
Output is correct |
31 |
Correct |
65 ms |
75268 KB |
Output is correct |
32 |
Correct |
65 ms |
75596 KB |
Output is correct |
33 |
Correct |
65 ms |
75396 KB |
Output is correct |
34 |
Correct |
1338 ms |
427424 KB |
Output is correct |
35 |
Correct |
1355 ms |
424564 KB |
Output is correct |
36 |
Correct |
1383 ms |
424624 KB |
Output is correct |
37 |
Correct |
1645 ms |
411788 KB |
Output is correct |
38 |
Correct |
1700 ms |
411908 KB |
Output is correct |
39 |
Correct |
1717 ms |
411780 KB |
Output is correct |
40 |
Correct |
1715 ms |
411836 KB |
Output is correct |
41 |
Correct |
49 ms |
70724 KB |
Output is correct |
42 |
Correct |
57 ms |
70916 KB |
Output is correct |
43 |
Execution timed out |
2075 ms |
209504 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |