#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 5;
const int MAX_L = __lg(MAX_N) + 1;
bitset<MAX_N> used_c, used_s;
unordered_map<int, int> pos[MAX_N];
set<pair<int, int>> values[MAX_N];
vector<int> graph[MAX_N], parent[MAX_N], dist[MAX_N];
vector<bool> deleted[MAX_N];
int ancestors[MAX_N][MAX_L], num[MAX_N] = {}, value[MAX_N], sz[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({value[u], 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({value[u], u});
deleted[c][pos[c][u]] = true;
for (int v : graph[u]) {
if (v != p && pos[c].count(v) && !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()) {
auto [valx, x] = *values[A].rbegin();
val = max(val, {valx, -(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 |
19 ms |
42600 KB |
Output is correct |
2 |
Correct |
20 ms |
42580 KB |
Output is correct |
3 |
Correct |
19 ms |
42604 KB |
Output is correct |
4 |
Correct |
19 ms |
42596 KB |
Output is correct |
5 |
Correct |
20 ms |
42632 KB |
Output is correct |
6 |
Correct |
21 ms |
42604 KB |
Output is correct |
7 |
Correct |
20 ms |
42588 KB |
Output is correct |
8 |
Correct |
22 ms |
42576 KB |
Output is correct |
9 |
Correct |
20 ms |
42600 KB |
Output is correct |
10 |
Correct |
23 ms |
42572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
42600 KB |
Output is correct |
2 |
Correct |
20 ms |
42580 KB |
Output is correct |
3 |
Correct |
19 ms |
42604 KB |
Output is correct |
4 |
Correct |
19 ms |
42596 KB |
Output is correct |
5 |
Correct |
20 ms |
42632 KB |
Output is correct |
6 |
Correct |
21 ms |
42604 KB |
Output is correct |
7 |
Correct |
20 ms |
42588 KB |
Output is correct |
8 |
Correct |
22 ms |
42576 KB |
Output is correct |
9 |
Correct |
20 ms |
42600 KB |
Output is correct |
10 |
Correct |
23 ms |
42572 KB |
Output is correct |
11 |
Correct |
20 ms |
42932 KB |
Output is correct |
12 |
Correct |
25 ms |
42980 KB |
Output is correct |
13 |
Correct |
21 ms |
42928 KB |
Output is correct |
14 |
Correct |
24 ms |
42944 KB |
Output is correct |
15 |
Correct |
24 ms |
42856 KB |
Output is correct |
16 |
Correct |
21 ms |
42872 KB |
Output is correct |
17 |
Correct |
22 ms |
42836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
42600 KB |
Output is correct |
2 |
Correct |
20 ms |
42580 KB |
Output is correct |
3 |
Correct |
19 ms |
42604 KB |
Output is correct |
4 |
Correct |
19 ms |
42596 KB |
Output is correct |
5 |
Correct |
20 ms |
42632 KB |
Output is correct |
6 |
Correct |
21 ms |
42604 KB |
Output is correct |
7 |
Correct |
20 ms |
42588 KB |
Output is correct |
8 |
Correct |
22 ms |
42576 KB |
Output is correct |
9 |
Correct |
20 ms |
42600 KB |
Output is correct |
10 |
Correct |
23 ms |
42572 KB |
Output is correct |
11 |
Correct |
20 ms |
42932 KB |
Output is correct |
12 |
Correct |
25 ms |
42980 KB |
Output is correct |
13 |
Correct |
21 ms |
42928 KB |
Output is correct |
14 |
Correct |
24 ms |
42944 KB |
Output is correct |
15 |
Correct |
24 ms |
42856 KB |
Output is correct |
16 |
Correct |
21 ms |
42872 KB |
Output is correct |
17 |
Correct |
22 ms |
42836 KB |
Output is correct |
18 |
Correct |
46 ms |
50580 KB |
Output is correct |
19 |
Correct |
44 ms |
50488 KB |
Output is correct |
20 |
Correct |
44 ms |
50580 KB |
Output is correct |
21 |
Correct |
56 ms |
50084 KB |
Output is correct |
22 |
Correct |
45 ms |
49996 KB |
Output is correct |
23 |
Correct |
46 ms |
50028 KB |
Output is correct |
24 |
Correct |
46 ms |
50092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
42600 KB |
Output is correct |
2 |
Correct |
20 ms |
42580 KB |
Output is correct |
3 |
Correct |
19 ms |
42604 KB |
Output is correct |
4 |
Correct |
19 ms |
42596 KB |
Output is correct |
5 |
Correct |
20 ms |
42632 KB |
Output is correct |
6 |
Correct |
21 ms |
42604 KB |
Output is correct |
7 |
Correct |
20 ms |
42588 KB |
Output is correct |
8 |
Correct |
22 ms |
42576 KB |
Output is correct |
9 |
Correct |
20 ms |
42600 KB |
Output is correct |
10 |
Correct |
23 ms |
42572 KB |
Output is correct |
11 |
Correct |
20 ms |
42932 KB |
Output is correct |
12 |
Correct |
25 ms |
42980 KB |
Output is correct |
13 |
Correct |
21 ms |
42928 KB |
Output is correct |
14 |
Correct |
24 ms |
42944 KB |
Output is correct |
15 |
Correct |
24 ms |
42856 KB |
Output is correct |
16 |
Correct |
21 ms |
42872 KB |
Output is correct |
17 |
Correct |
22 ms |
42836 KB |
Output is correct |
18 |
Correct |
46 ms |
50580 KB |
Output is correct |
19 |
Correct |
44 ms |
50488 KB |
Output is correct |
20 |
Correct |
44 ms |
50580 KB |
Output is correct |
21 |
Correct |
56 ms |
50084 KB |
Output is correct |
22 |
Correct |
45 ms |
49996 KB |
Output is correct |
23 |
Correct |
46 ms |
50028 KB |
Output is correct |
24 |
Correct |
46 ms |
50092 KB |
Output is correct |
25 |
Correct |
19 ms |
42524 KB |
Output is correct |
26 |
Correct |
48 ms |
50224 KB |
Output is correct |
27 |
Correct |
47 ms |
50228 KB |
Output is correct |
28 |
Correct |
47 ms |
50288 KB |
Output is correct |
29 |
Correct |
48 ms |
50192 KB |
Output is correct |
30 |
Correct |
52 ms |
48496 KB |
Output is correct |
31 |
Correct |
44 ms |
48092 KB |
Output is correct |
32 |
Correct |
45 ms |
48464 KB |
Output is correct |
33 |
Correct |
52 ms |
48204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
42600 KB |
Output is correct |
2 |
Correct |
20 ms |
42580 KB |
Output is correct |
3 |
Correct |
19 ms |
42604 KB |
Output is correct |
4 |
Correct |
19 ms |
42596 KB |
Output is correct |
5 |
Correct |
20 ms |
42632 KB |
Output is correct |
6 |
Correct |
21 ms |
42604 KB |
Output is correct |
7 |
Correct |
20 ms |
42588 KB |
Output is correct |
8 |
Correct |
22 ms |
42576 KB |
Output is correct |
9 |
Correct |
20 ms |
42600 KB |
Output is correct |
10 |
Correct |
23 ms |
42572 KB |
Output is correct |
11 |
Correct |
20 ms |
42932 KB |
Output is correct |
12 |
Correct |
25 ms |
42980 KB |
Output is correct |
13 |
Correct |
21 ms |
42928 KB |
Output is correct |
14 |
Correct |
24 ms |
42944 KB |
Output is correct |
15 |
Correct |
24 ms |
42856 KB |
Output is correct |
16 |
Correct |
21 ms |
42872 KB |
Output is correct |
17 |
Correct |
22 ms |
42836 KB |
Output is correct |
18 |
Correct |
46 ms |
50580 KB |
Output is correct |
19 |
Correct |
44 ms |
50488 KB |
Output is correct |
20 |
Correct |
44 ms |
50580 KB |
Output is correct |
21 |
Correct |
56 ms |
50084 KB |
Output is correct |
22 |
Correct |
45 ms |
49996 KB |
Output is correct |
23 |
Correct |
46 ms |
50028 KB |
Output is correct |
24 |
Correct |
46 ms |
50092 KB |
Output is correct |
25 |
Correct |
1459 ms |
461860 KB |
Output is correct |
26 |
Correct |
1576 ms |
458852 KB |
Output is correct |
27 |
Correct |
1588 ms |
458808 KB |
Output is correct |
28 |
Correct |
1843 ms |
438468 KB |
Output is correct |
29 |
Correct |
1823 ms |
438476 KB |
Output is correct |
30 |
Correct |
1913 ms |
438500 KB |
Output is correct |
31 |
Correct |
1823 ms |
438476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
42580 KB |
Output is correct |
2 |
Correct |
26 ms |
42724 KB |
Output is correct |
3 |
Execution timed out |
2113 ms |
403844 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
42600 KB |
Output is correct |
2 |
Correct |
20 ms |
42580 KB |
Output is correct |
3 |
Correct |
19 ms |
42604 KB |
Output is correct |
4 |
Correct |
19 ms |
42596 KB |
Output is correct |
5 |
Correct |
20 ms |
42632 KB |
Output is correct |
6 |
Correct |
21 ms |
42604 KB |
Output is correct |
7 |
Correct |
20 ms |
42588 KB |
Output is correct |
8 |
Correct |
22 ms |
42576 KB |
Output is correct |
9 |
Correct |
20 ms |
42600 KB |
Output is correct |
10 |
Correct |
23 ms |
42572 KB |
Output is correct |
11 |
Correct |
20 ms |
42932 KB |
Output is correct |
12 |
Correct |
25 ms |
42980 KB |
Output is correct |
13 |
Correct |
21 ms |
42928 KB |
Output is correct |
14 |
Correct |
24 ms |
42944 KB |
Output is correct |
15 |
Correct |
24 ms |
42856 KB |
Output is correct |
16 |
Correct |
21 ms |
42872 KB |
Output is correct |
17 |
Correct |
22 ms |
42836 KB |
Output is correct |
18 |
Correct |
46 ms |
50580 KB |
Output is correct |
19 |
Correct |
44 ms |
50488 KB |
Output is correct |
20 |
Correct |
44 ms |
50580 KB |
Output is correct |
21 |
Correct |
56 ms |
50084 KB |
Output is correct |
22 |
Correct |
45 ms |
49996 KB |
Output is correct |
23 |
Correct |
46 ms |
50028 KB |
Output is correct |
24 |
Correct |
46 ms |
50092 KB |
Output is correct |
25 |
Correct |
19 ms |
42524 KB |
Output is correct |
26 |
Correct |
48 ms |
50224 KB |
Output is correct |
27 |
Correct |
47 ms |
50228 KB |
Output is correct |
28 |
Correct |
47 ms |
50288 KB |
Output is correct |
29 |
Correct |
48 ms |
50192 KB |
Output is correct |
30 |
Correct |
52 ms |
48496 KB |
Output is correct |
31 |
Correct |
44 ms |
48092 KB |
Output is correct |
32 |
Correct |
45 ms |
48464 KB |
Output is correct |
33 |
Correct |
52 ms |
48204 KB |
Output is correct |
34 |
Correct |
1459 ms |
461860 KB |
Output is correct |
35 |
Correct |
1576 ms |
458852 KB |
Output is correct |
36 |
Correct |
1588 ms |
458808 KB |
Output is correct |
37 |
Correct |
1843 ms |
438468 KB |
Output is correct |
38 |
Correct |
1823 ms |
438476 KB |
Output is correct |
39 |
Correct |
1913 ms |
438500 KB |
Output is correct |
40 |
Correct |
1823 ms |
438476 KB |
Output is correct |
41 |
Correct |
24 ms |
42580 KB |
Output is correct |
42 |
Correct |
26 ms |
42724 KB |
Output is correct |
43 |
Execution timed out |
2113 ms |
403844 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |