#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
#include "catdog.h"
constexpr int kInf = 1'000'000;
std::vector<std::vector<int>> g, nd;
std::vector<int> sz, to, fa, fr, dep, ksz;
void Dfs(int x, int p) {
sz[x] = 1;
fa[x] = p;
for (int u : g[x]) {
if (u == p) continue;
dep[u] = dep[x] + 1;
Dfs(u, x);
sz[x] += sz[u];
if (to[x] == -1 || sz[u] > sz[to[x]]) to[x] = u;
}
}
void Link(int x, int f) {
fr[x] = f;
if (to[x] != -1) Link(to[x], f);
for (int u : g[x]) {
if (u == fa[x] || u == to[x]) continue;
Link(u, u);
}
}
struct IntervalTree {
int n;
std::vector<std::vector<int>> tree;
std::vector<std::vector<int>> aux;
IntervalTree() = default;
IntervalTree(int n)
: n(n), tree(n * 4, std::vector<int>(4)), aux(n, std::vector<int>(2)) {
for (auto &v : tree) {
v[1] = kInf;
v[2] = kInf;
}
}
std::vector<int> Merge(const std::vector<int> &a, const std::vector<int> &b,
int p) {
std::vector<int> res(4, kInf);
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
res[(i & 2) | (j & 1)] = std::min(
res[(i & 2) | (j & 1)],
a[i] + b[j] + ((i & 1) != (j >> 1 & 1)) + aux[p][j >> 1 & 1]);
}
}
return res;
}
void UpdateAux(int p, const std::vector<int> &v) {
auto Impl = [&](auto self, int l, int r, int o = 0) {
if (r - l == 1) {
aux[l] = v;
return;
}
int m = (l + r) >> 1;
if (p < m) {
self(self, l, m, o * 2 + 1);
} else {
self(self, m, r, o * 2 + 2);
}
tree[o] = Merge(tree[o * 2 + 1], tree[o * 2 + 2], m);
};
Impl(Impl, 0, n);
}
void Modify(int p, const std::vector<int> &v) {
auto Impl = [&](auto self, int l, int r, int o = 0) {
if (r - l == 1) {
std::fill(tree[o].begin(), tree[o].end(), kInf);
for (int i = 0; i < 2; ++i) tree[o][i << 1 | i] = v[i];
return;
}
int m = (l + r) >> 1;
if (p < m) {
self(self, l, m, o * 2 + 1);
} else {
self(self, m, r, o * 2 + 2);
}
tree[o] = Merge(tree[o * 2 + 1], tree[o * 2 + 2], m);
};
Impl(Impl, 0, n);
}
std::vector<int> Query(int ql, int qr) {
auto Impl = [&](auto self, int l, int r, int o = 0) {
if (l >= ql && r <= qr) return tree[o];
int m = (l + r) >> 1;
if (qr <= m) return self(self, l, m, o * 2 + 1);
if (ql >= m) return self(self, m, r, o * 2 + 2);
return Merge(self(self, l, m, o * 2 + 1), self(self, m, r, o * 2 + 2), m);
};
return Impl(Impl, 0, n);
}
};
std::vector<IntervalTree> tree;
void initialize(int n, std::vector<int> a, std::vector<int> b) {
g.resize(n);
for (int i = 0; i < n - 1; ++i) {
int u = a[i], v = b[i];
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
sz.assign(n, 0);
ksz.assign(n, 0);
to.assign(n, -1);
fa.assign(n, -1);
fr.assign(n, -1);
dep.assign(n, 0);
tree.assign(n, IntervalTree());
Dfs(0, -1);
Link(0, 0);
for (int i = 0; i < n; ++i) ksz[fr[i]]++;
for (int i = 0; i < n; ++i) {
if (fr[i] == i) {
tree[i] = IntervalTree(ksz[i]);
}
}
}
std::vector<int> GetAux(int v) {
int p = dep[v] - dep[fr[v]];
return tree[fr[v]].aux[p];
}
std::vector<int> GetValue(int v) {
int p = dep[v] - dep[fr[v]];
auto val = tree[fr[v]].Query(p, ksz[fr[v]]);
std::vector<int> res(2, kInf);
for (int i = 0; i < 4; ++i) {
res[i >> 1 & 1] = std::min(res[i >> 1 & 1], val[i]);
}
for (int i = 0; i < 2; ++i) res[i] += GetAux(v)[i];
return res;
}
void UpdateAux(int v, const std::vector<int> &aux) {
int p = dep[v] - dep[fr[v]];
tree[fr[v]].UpdateAux(p, aux);
}
int cat(int v) {
v--;
std::vector<std::vector<int>> hist;
for (int w = fr[v]; fa[w] != -1; w = fr[fa[w]]) {
hist.push_back(GetValue(w));
}
tree[fr[v]].Modify(dep[v] - dep[fr[v]], {0, kInf});
v = fr[v];
int ptr = 0;
while (fa[v] >= 0) {
auto aux = GetAux(fa[v]);
auto old = hist[ptr];
aux[0] -= std::min(old[0], old[1] + 1);
aux[1] -= std::min(old[1], old[0] + 1);
auto upd = GetValue(v);
aux[0] += std::min(upd[0], upd[1] + 1);
aux[1] += std::min(upd[1], upd[0] + 1);
ptr++;
// std::cerr << "v = " << v << " fa = " << fa[v] << " upd-aux = " << aux[0]
// << ", " << aux[1] << std::endl;
UpdateAux(fa[v], aux);
v = fr[fa[v]];
}
// for (int i = 0; i < g.size(); ++i) {
// auto gv = GetValue(i);
// std::cerr << "dp[" << i << "][0] = " << gv[0] << std::endl;
// std::cerr << "dp[" << i << "][1] = " << gv[1] << std::endl;
// auto aux = GetAux(i);
// std::cerr << "aux[" << i << "][0] = " << aux[0] << std::endl;
// std::cerr << "aux[" << i << "][1] = " << aux[1] << std::endl;
// }
// std::cerr << std::endl;
auto res = GetValue(0);
return std::min(res[0], res[1]);
}
int dog(int v) {
v--;
std::vector<std::vector<int>> hist;
for (int w = fr[v]; fa[w] != -1; w = fr[fa[w]]) {
hist.push_back(GetValue(w));
}
tree[fr[v]].Modify(dep[v] - dep[fr[v]], {kInf, 0});
v = fr[v];
int ptr = 0;
while (fa[v] >= 0) {
auto aux = GetAux(fa[v]);
auto old = hist[ptr];
aux[0] -= std::min(old[0], old[1] + 1);
aux[1] -= std::min(old[1], old[0] + 1);
auto upd = GetValue(v);
aux[0] += std::min(upd[0], upd[1] + 1);
aux[1] += std::min(upd[1], upd[0] + 1);
ptr++;
UpdateAux(fa[v], aux);
v = fr[fa[v]];
}
// for (int i = 0; i < g.size(); ++i) {
// auto gv = GetValue(i);
// std::cerr << "dp[" << i << "][0] = " << gv[0] << std::endl;
// std::cerr << "dp[" << i << "][1] = " << gv[1] << std::endl;
// auto aux = GetAux(i);
// std::cerr << "aux[" << i << "][0] = " << aux[0] << std::endl;
// std::cerr << "aux[" << i << "][1] = " << aux[1] << std::endl;
// }
// std::cerr << std::endl;
auto res = GetValue(0);
return std::min(res[0], res[1]);
}
int neighbor(int v) {
v--;
std::vector<std::vector<int>> hist;
for (int w = fr[v]; fa[w] != -1; w = fr[fa[w]]) {
hist.push_back(GetValue(w));
}
tree[fr[v]].Modify(dep[v] - dep[fr[v]], {0, 0});
v = fr[v];
int ptr = 0;
while (fa[v] >= 0) {
auto aux = GetAux(fa[v]);
auto old = hist[ptr];
aux[0] -= std::min(old[0], old[1] + 1);
aux[1] -= std::min(old[1], old[0] + 1);
auto upd = GetValue(v);
aux[0] += std::min(upd[0], upd[1] + 1);
aux[1] += std::min(upd[1], upd[0] + 1);
ptr++;
// std::cerr << "v = " << v << " fa = " << fa[v] << " upd-aux = " << aux[0]
// << ", " << aux[1] << std::endl;
UpdateAux(fa[v], aux);
v = fr[fa[v]];
}
// for (int i = 0; i < g.size(); ++i) {
// auto gv = GetValue(i);
// std::cerr << "dp[" << i << "][0] = " << gv[0] << std::endl;
// std::cerr << "dp[" << i << "][1] = " << gv[1] << std::endl;
// auto aux = GetAux(i);
// std::cerr << "aux[" << i << "][0] = " << aux[0] << std::endl;
// std::cerr << "aux[" << i << "][1] = " << aux[1] << std::endl;
// }
// std::cerr << std::endl;
auto res = GetValue(0);
return std::min(res[0], res[1]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
248 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
248 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
380 KB |
Output is correct |
17 |
Correct |
8 ms |
632 KB |
Output is correct |
18 |
Correct |
8 ms |
760 KB |
Output is correct |
19 |
Correct |
7 ms |
716 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
6 ms |
376 KB |
Output is correct |
22 |
Correct |
6 ms |
504 KB |
Output is correct |
23 |
Correct |
8 ms |
632 KB |
Output is correct |
24 |
Correct |
8 ms |
632 KB |
Output is correct |
25 |
Correct |
7 ms |
504 KB |
Output is correct |
26 |
Correct |
6 ms |
504 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
6 ms |
632 KB |
Output is correct |
29 |
Correct |
7 ms |
760 KB |
Output is correct |
30 |
Correct |
6 ms |
376 KB |
Output is correct |
31 |
Correct |
6 ms |
636 KB |
Output is correct |
32 |
Correct |
6 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
248 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
380 KB |
Output is correct |
17 |
Correct |
8 ms |
632 KB |
Output is correct |
18 |
Correct |
8 ms |
760 KB |
Output is correct |
19 |
Correct |
7 ms |
716 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
6 ms |
376 KB |
Output is correct |
22 |
Correct |
6 ms |
504 KB |
Output is correct |
23 |
Correct |
8 ms |
632 KB |
Output is correct |
24 |
Correct |
8 ms |
632 KB |
Output is correct |
25 |
Correct |
7 ms |
504 KB |
Output is correct |
26 |
Correct |
6 ms |
504 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
6 ms |
632 KB |
Output is correct |
29 |
Correct |
7 ms |
760 KB |
Output is correct |
30 |
Correct |
6 ms |
376 KB |
Output is correct |
31 |
Correct |
6 ms |
636 KB |
Output is correct |
32 |
Correct |
6 ms |
504 KB |
Output is correct |
33 |
Correct |
554 ms |
24296 KB |
Output is correct |
34 |
Correct |
195 ms |
29432 KB |
Output is correct |
35 |
Correct |
479 ms |
16752 KB |
Output is correct |
36 |
Correct |
881 ms |
41664 KB |
Output is correct |
37 |
Correct |
44 ms |
13816 KB |
Output is correct |
38 |
Correct |
961 ms |
46640 KB |
Output is correct |
39 |
Correct |
947 ms |
46568 KB |
Output is correct |
40 |
Correct |
935 ms |
46568 KB |
Output is correct |
41 |
Correct |
961 ms |
46568 KB |
Output is correct |
42 |
Correct |
880 ms |
46568 KB |
Output is correct |
43 |
Correct |
924 ms |
46616 KB |
Output is correct |
44 |
Correct |
879 ms |
46568 KB |
Output is correct |
45 |
Correct |
916 ms |
46572 KB |
Output is correct |
46 |
Correct |
918 ms |
46568 KB |
Output is correct |
47 |
Correct |
914 ms |
46580 KB |
Output is correct |
48 |
Correct |
184 ms |
34368 KB |
Output is correct |
49 |
Correct |
211 ms |
43632 KB |
Output is correct |
50 |
Correct |
71 ms |
8972 KB |
Output is correct |
51 |
Correct |
88 ms |
17140 KB |
Output is correct |
52 |
Correct |
43 ms |
8952 KB |
Output is correct |
53 |
Correct |
405 ms |
45048 KB |
Output is correct |
54 |
Correct |
274 ms |
19448 KB |
Output is correct |
55 |
Correct |
723 ms |
31856 KB |
Output is correct |
56 |
Correct |
429 ms |
21152 KB |
Output is correct |
57 |
Correct |
522 ms |
41336 KB |
Output is correct |
58 |
Correct |
62 ms |
19700 KB |
Output is correct |
59 |
Correct |
84 ms |
15480 KB |
Output is correct |
60 |
Correct |
183 ms |
38640 KB |
Output is correct |
61 |
Correct |
204 ms |
40176 KB |
Output is correct |
62 |
Correct |
134 ms |
32752 KB |
Output is correct |
63 |
Correct |
107 ms |
26872 KB |
Output is correct |
64 |
Correct |
108 ms |
29944 KB |
Output is correct |
65 |
Correct |
139 ms |
49020 KB |
Output is correct |
66 |
Correct |
150 ms |
11476 KB |
Output is correct |
67 |
Correct |
150 ms |
34040 KB |
Output is correct |
68 |
Correct |
306 ms |
49656 KB |
Output is correct |
69 |
Correct |
73 ms |
3832 KB |
Output is correct |
70 |
Correct |
19 ms |
888 KB |
Output is correct |
71 |
Correct |
130 ms |
20984 KB |
Output is correct |
72 |
Correct |
198 ms |
38520 KB |
Output is correct |
73 |
Correct |
482 ms |
52984 KB |
Output is correct |
74 |
Correct |
497 ms |
49528 KB |
Output is correct |
75 |
Correct |
335 ms |
52976 KB |
Output is correct |
76 |
Correct |
327 ms |
51704 KB |
Output is correct |
77 |
Correct |
495 ms |
49912 KB |
Output is correct |