# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
868745 |
2023-11-01T22:04:20 Z |
WLZ |
Cat Exercise (JOI23_ho_t4) |
C++17 |
|
195 ms |
62040 KB |
#include <bits/stdc++.h>
using namespace std;
// https://codeforces.com/blog/entry/74847
class lowest_common_ancestor {
private:
std::vector< std::vector<int> > g;
std::vector<int> p, jump, d;
void dfs(int u) {
if (d[p[u]] - d[jump[p[u]]] == d[jump[p[u]]] - d[jump[jump[p[u]]]]) jump[u] = jump[jump[p[u]]];
for (auto v : g[u]) {
if (v == p[u]) continue;
jump[v] = p[v] = u;
d[v] = d[u] + 1;
dfs(v);
}
}
public:
lowest_common_ancestor(const std::vector< std::vector<int> > &_g, int root) : g(_g) {
int n = (int) g.size();
p.resize(n); jump.resize(n); d.assign(n, 0);
p[root] = root;
dfs(root);
}
int kth_ancestor(int u, int k) const {
int v = u;
while (d[u] - d[v] < k) {
if (d[u] - d[jump[v]] < k) v = jump[v];
else v = p[v];
}
return v;
}
int query(int u, int v) const {
if (d[u] < d[v]) return query(v, u);
u = kth_ancestor(u, d[u] - d[v]);
while (u != v) {
if (jump[u] == jump[v]) u = p[u], v = p[v];
else u = jump[u], v = jump[v];
}
return u;
}
int depth(int u) const { return d[u]; }
int dist(int u, int v) const { return d[u] + d[v] - 2 * d[query(u, v)]; }
};
class union_find {
private:
int n;
std::vector<int> p, h, largest;
public:
union_find() : n(0) {}
explicit union_find(const std::vector<int> &_h) : n(static_cast<int>(_h.size())), p(n, -1), h(_h), largest(n) {
std::iota(largest.begin(), largest.end(), 0);
}
int root(int a) { return p[a] < 0 ? a : (p[a] = root(p[a])); }
bool connected(int a, int b) { return root(a) == root(b); }
int connect(int a, int b) {
a = root(a); b = root(b);
if (a == b) return a;
if (p[a] > p[a]) std::swap(a, b);
p[a] += p[b]; p[b] = a;
largest[a] = h[largest[a]] > h[largest[b]] ? largest[a] : largest[b];
return a;
}
int size() const { return n; }
int size(int a) { return -p[root(a)]; }
int tallest(int a) { return largest[root(a)]; }
};
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
vector<int> h(n + 1);
for (int i = 1; i <= n; i++) cin >> h[i];
vector g(n + 1, vector<int>());
for (int i = 0; i < n - 1; i++) {
int a, b; cin >> a >> b;
g[a].push_back(b); g[b].push_back(a);
}
vector<int> ord(n);
iota(ord.begin(), ord.end(), 1);
sort(ord.begin(), ord.end(), [&](int a, int b) { return h[a] < h[b]; });
vector<bool> used(n + 1, false);
vector g2(n + 1, vector<int>());
union_find uf(h);
for (const int &u : ord) {
used[u] = true;
for (const int &v : g[u]) {
if (used[v]) {
g2[u].push_back(uf.tallest(v));
uf.connect(u, v);
}
}
}
lowest_common_ancestor lca(g, ord[n - 1]);
function<long long(int)> solve = [&](int u) {
long long ans = 0;
for (const int &v : g2[u]) ans = max(ans, lca.dist(u, v) + solve(v));
return ans;
};
cout << solve(ord[n - 1]) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1880 KB |
Output is correct |
19 |
Correct |
3 ms |
1880 KB |
Output is correct |
20 |
Correct |
3 ms |
2136 KB |
Output is correct |
21 |
Correct |
3 ms |
1372 KB |
Output is correct |
22 |
Correct |
3 ms |
1492 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1880 KB |
Output is correct |
19 |
Correct |
3 ms |
1880 KB |
Output is correct |
20 |
Correct |
3 ms |
2136 KB |
Output is correct |
21 |
Correct |
3 ms |
1372 KB |
Output is correct |
22 |
Correct |
3 ms |
1492 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1808 KB |
Output is correct |
27 |
Correct |
3 ms |
1628 KB |
Output is correct |
28 |
Correct |
3 ms |
1628 KB |
Output is correct |
29 |
Correct |
3 ms |
1628 KB |
Output is correct |
30 |
Correct |
3 ms |
1292 KB |
Output is correct |
31 |
Correct |
3 ms |
1372 KB |
Output is correct |
32 |
Correct |
3 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1880 KB |
Output is correct |
19 |
Correct |
3 ms |
1880 KB |
Output is correct |
20 |
Correct |
3 ms |
2136 KB |
Output is correct |
21 |
Correct |
3 ms |
1372 KB |
Output is correct |
22 |
Correct |
3 ms |
1492 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
141 ms |
62040 KB |
Output is correct |
26 |
Correct |
115 ms |
59744 KB |
Output is correct |
27 |
Correct |
112 ms |
59824 KB |
Output is correct |
28 |
Correct |
148 ms |
45776 KB |
Output is correct |
29 |
Correct |
170 ms |
46780 KB |
Output is correct |
30 |
Correct |
144 ms |
46932 KB |
Output is correct |
31 |
Correct |
130 ms |
46792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
133 ms |
42204 KB |
Output is correct |
4 |
Correct |
136 ms |
42448 KB |
Output is correct |
5 |
Correct |
145 ms |
42100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1880 KB |
Output is correct |
19 |
Correct |
3 ms |
1880 KB |
Output is correct |
20 |
Correct |
3 ms |
2136 KB |
Output is correct |
21 |
Correct |
3 ms |
1372 KB |
Output is correct |
22 |
Correct |
3 ms |
1492 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1808 KB |
Output is correct |
27 |
Correct |
3 ms |
1628 KB |
Output is correct |
28 |
Correct |
3 ms |
1628 KB |
Output is correct |
29 |
Correct |
3 ms |
1628 KB |
Output is correct |
30 |
Correct |
3 ms |
1292 KB |
Output is correct |
31 |
Correct |
3 ms |
1372 KB |
Output is correct |
32 |
Correct |
3 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1372 KB |
Output is correct |
34 |
Correct |
141 ms |
62040 KB |
Output is correct |
35 |
Correct |
115 ms |
59744 KB |
Output is correct |
36 |
Correct |
112 ms |
59824 KB |
Output is correct |
37 |
Correct |
148 ms |
45776 KB |
Output is correct |
38 |
Correct |
170 ms |
46780 KB |
Output is correct |
39 |
Correct |
144 ms |
46932 KB |
Output is correct |
40 |
Correct |
130 ms |
46792 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
460 KB |
Output is correct |
43 |
Correct |
133 ms |
42204 KB |
Output is correct |
44 |
Correct |
136 ms |
42448 KB |
Output is correct |
45 |
Correct |
145 ms |
42100 KB |
Output is correct |
46 |
Correct |
195 ms |
55536 KB |
Output is correct |
47 |
Correct |
193 ms |
55412 KB |
Output is correct |
48 |
Correct |
191 ms |
55380 KB |
Output is correct |
49 |
Correct |
181 ms |
55384 KB |
Output is correct |
50 |
Correct |
185 ms |
41364 KB |
Output is correct |
51 |
Correct |
172 ms |
41300 KB |
Output is correct |
52 |
Correct |
169 ms |
41300 KB |
Output is correct |
53 |
Correct |
173 ms |
41292 KB |
Output is correct |