#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int LOG = 18;
const int N = 2e5 + 5;
int a[N];
int dep[N];
int up[LOG][N];
bool active[N];
vector<int> g[N];
long long ans[N];
int p[N], sz[N], mx[N];
void dfs(int v, int pr) {
for (int u : g[v]) {
if (u == pr) {
continue;
}
dep[u] = dep[v] + 1;
up[0][u] = v;
for (int j = 1; j < LOG; ++j) {
up[j][u] = up[j - 1][up[j - 1][u]];
}
dfs(u, v);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int j = 0; j < LOG; ++j) {
if ((dep[u] - dep[v]) >> j & 1) {
u = up[j][u];
}
}
if (u == v) {
return u;
}
for (int j = LOG - 1; j >= 0; --j) {
if (up[j][u] != up[j][v]) {
u = up[j][u];
v = up[j][v];
}
}
return up[0][u];
}
int dist(int u, int v) {
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
int get(int v) {
return p[v] = (p[v] == v ? v : get(p[v]));
}
void unite(int u, int v) {
u = get(u); v = get(v);
p[u] = v;
sz[v] += sz[u];
int w = dist(mx[u], v);
ans[v] = max(ans[v], ans[u] + w);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for(int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 1);
vector<int> ord(n);
iota(ord.begin(), ord.end(), 1);
sort(ord.begin(), ord.end(), [&](int u, int v) {
return a[u] < a[v];
});
for (int i = 1; i <= n; ++i) {
p[i] = i;
sz[i] = 1;
mx[i] = i;
}
for (int v : ord) {
for (int u : g[v]) {
if (active[u]) {
unite(u, v);
}
}
active[v] = true;
}
cout << ans[get(ord.back())] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5284 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5284 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5128 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5168 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5284 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5128 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5168 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5176 KB |
Output is correct |
18 |
Correct |
4 ms |
6236 KB |
Output is correct |
19 |
Correct |
4 ms |
6208 KB |
Output is correct |
20 |
Correct |
4 ms |
6236 KB |
Output is correct |
21 |
Correct |
4 ms |
6236 KB |
Output is correct |
22 |
Correct |
4 ms |
6236 KB |
Output is correct |
23 |
Correct |
4 ms |
6236 KB |
Output is correct |
24 |
Correct |
4 ms |
6232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5284 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5128 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5168 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5176 KB |
Output is correct |
18 |
Correct |
4 ms |
6236 KB |
Output is correct |
19 |
Correct |
4 ms |
6208 KB |
Output is correct |
20 |
Correct |
4 ms |
6236 KB |
Output is correct |
21 |
Correct |
4 ms |
6236 KB |
Output is correct |
22 |
Correct |
4 ms |
6236 KB |
Output is correct |
23 |
Correct |
4 ms |
6236 KB |
Output is correct |
24 |
Correct |
4 ms |
6232 KB |
Output is correct |
25 |
Correct |
2 ms |
5208 KB |
Output is correct |
26 |
Correct |
5 ms |
6204 KB |
Output is correct |
27 |
Correct |
5 ms |
6120 KB |
Output is correct |
28 |
Correct |
5 ms |
5976 KB |
Output is correct |
29 |
Correct |
5 ms |
6236 KB |
Output is correct |
30 |
Correct |
4 ms |
6036 KB |
Output is correct |
31 |
Correct |
4 ms |
5980 KB |
Output is correct |
32 |
Correct |
4 ms |
5980 KB |
Output is correct |
33 |
Correct |
5 ms |
5824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5284 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5128 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5168 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5176 KB |
Output is correct |
18 |
Correct |
4 ms |
6236 KB |
Output is correct |
19 |
Correct |
4 ms |
6208 KB |
Output is correct |
20 |
Correct |
4 ms |
6236 KB |
Output is correct |
21 |
Correct |
4 ms |
6236 KB |
Output is correct |
22 |
Correct |
4 ms |
6236 KB |
Output is correct |
23 |
Correct |
4 ms |
6236 KB |
Output is correct |
24 |
Correct |
4 ms |
6232 KB |
Output is correct |
25 |
Correct |
102 ms |
51284 KB |
Output is correct |
26 |
Correct |
89 ms |
51252 KB |
Output is correct |
27 |
Correct |
104 ms |
51284 KB |
Output is correct |
28 |
Correct |
140 ms |
51376 KB |
Output is correct |
29 |
Correct |
107 ms |
51284 KB |
Output is correct |
30 |
Correct |
100 ms |
51280 KB |
Output is correct |
31 |
Correct |
110 ms |
51284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
112 ms |
35076 KB |
Output is correct |
4 |
Correct |
119 ms |
35112 KB |
Output is correct |
5 |
Correct |
121 ms |
35152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5284 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5128 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5168 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5176 KB |
Output is correct |
18 |
Correct |
4 ms |
6236 KB |
Output is correct |
19 |
Correct |
4 ms |
6208 KB |
Output is correct |
20 |
Correct |
4 ms |
6236 KB |
Output is correct |
21 |
Correct |
4 ms |
6236 KB |
Output is correct |
22 |
Correct |
4 ms |
6236 KB |
Output is correct |
23 |
Correct |
4 ms |
6236 KB |
Output is correct |
24 |
Correct |
4 ms |
6232 KB |
Output is correct |
25 |
Correct |
2 ms |
5208 KB |
Output is correct |
26 |
Correct |
5 ms |
6204 KB |
Output is correct |
27 |
Correct |
5 ms |
6120 KB |
Output is correct |
28 |
Correct |
5 ms |
5976 KB |
Output is correct |
29 |
Correct |
5 ms |
6236 KB |
Output is correct |
30 |
Correct |
4 ms |
6036 KB |
Output is correct |
31 |
Correct |
4 ms |
5980 KB |
Output is correct |
32 |
Correct |
4 ms |
5980 KB |
Output is correct |
33 |
Correct |
5 ms |
5824 KB |
Output is correct |
34 |
Correct |
102 ms |
51284 KB |
Output is correct |
35 |
Correct |
89 ms |
51252 KB |
Output is correct |
36 |
Correct |
104 ms |
51284 KB |
Output is correct |
37 |
Correct |
140 ms |
51376 KB |
Output is correct |
38 |
Correct |
107 ms |
51284 KB |
Output is correct |
39 |
Correct |
100 ms |
51280 KB |
Output is correct |
40 |
Correct |
110 ms |
51284 KB |
Output is correct |
41 |
Correct |
2 ms |
5208 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
112 ms |
35076 KB |
Output is correct |
44 |
Correct |
119 ms |
35112 KB |
Output is correct |
45 |
Correct |
121 ms |
35152 KB |
Output is correct |
46 |
Correct |
197 ms |
44276 KB |
Output is correct |
47 |
Correct |
188 ms |
42788 KB |
Output is correct |
48 |
Correct |
192 ms |
44828 KB |
Output is correct |
49 |
Correct |
186 ms |
42908 KB |
Output is correct |
50 |
Correct |
193 ms |
35840 KB |
Output is correct |
51 |
Correct |
144 ms |
35624 KB |
Output is correct |
52 |
Correct |
164 ms |
35644 KB |
Output is correct |
53 |
Correct |
150 ms |
35672 KB |
Output is correct |