#include "catdog.h"
#include <vector>
#include <algorithm>
#include <utility>
#include <array>
namespace Solver {
template <class T>
using Vec = std::vector<T>;
constexpr int MAX = 100000;
struct Monoid {
static constexpr Monoid ID() { return Monoid { { 0, MAX, MAX, 0 } }; }
std::array<std::array<long long, 2>, 2> value;
Monoid operator + (const Monoid &other) const {
Monoid ret{ { MAX, MAX, MAX, MAX }};
for (int i: { 0, 1 }) {
for (int j: { 0, 1 }) {
for (int k: { 0, 1 }) {
for (int l: { 0, 1 }) {
ret.value[i][l] = std::min(ret.value[i][l], value[i][j] + other.value[k][l] + (j ^ k));
}
}
}
}
return ret;
}
};
struct Segtree {
int size;
Vec<Monoid> data;
void fix(const int k) {
data[k] = data[k << 1 | 0] + data[k << 1 | 1];
}
Segtree(const int n = 0): size(n), data(2 * n, Monoid::ID()) { }
void set(int k, const std::array<long long, 2> &arr) {
k += size;
data[k].value[0][0] = arr[0];
data[k].value[1][1] = arr[1];
while (k > 1) {
k >>= 1;
fix(k);
}
}
std::array<long long, 2> get(int r) const {
int l = size;
r += size;
Monoid ml = Monoid::ID();
Monoid mr = Monoid::ID();
while (l < r) {
if (l & 1) {
ml = ml + data[l];
l += 1;
}
if (r & 1) {
r -= 1;
mr = data[r] + mr;
}
l >>= 1;
r >>= 1;
}
Monoid tmp = ml + mr;
std::array<long long, 2> ret{ MAX, MAX };
for (int i: { 0, 1 }) {
for (int j: { 0, 1 }) {
ret[i] = std::min(ret[i], tmp.value[i][j]);
}
}
return ret;
}
};
std::array<Vec<int>, MAX> graph;
std::array<int, MAX> parent, size, head, label, state;
std::array<Segtree, MAX> trees;
std::array<std::array<long long, 2>, MAX> vals;
void precalc(const int u, const int p) {
parent[u] = p;
size[u] = 1;
for (const auto v: graph[u]) {
if (v != parent[u]) {
precalc(v, u);
size[u] += size[v];
}
}
}
void decomp(const int u, const int h, const int l) {
head[u] = h;
label[u] = l;
int c = -1;
for (const auto v: graph[u]) {
if (v != parent[u]) {
if (c == -1 || size[c] < size[v]) {
c = v;
}
}
}
if (c == -1) {
trees[h] = Segtree(l + 1);
}
else {
decomp(c, h, l + 1);
for (const auto v: graph[u]) {
if (v != parent[u] && v != c) {
decomp(v, v, 0);
}
}
}
}
long long set(int u, std::array<long long, 2> arr) {
while (true) {
const auto v = head[u];
const auto cur = trees[v].get(trees[v].size);
trees[v].set(label[u], arr);
const auto next = trees[v].get(trees[v].size);
if (v == 0) {
break;
}
u = parent[v];
for (int i: { 0, 1 }) {
vals[u][i] -= std::min(cur[i], cur[i ^ 1] + 1);
vals[u][i] += std::min(next[i], next[i ^ 1] + 1);
arr[i] = (state[u] == 2 - i ? (long long) MAX : vals[u][i]);
}
}
const auto tmp = trees[0].get(trees[0].size);
return std::min(tmp[0], tmp[1]);
}
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
for (int i = 0; i < N - 1; ++i) {
A[i] -= 1;
B[i] -= 1;
Solver::graph[A[i]].push_back(B[i]);
Solver::graph[B[i]].push_back(A[i]);
}
Solver::precalc(0, -1);
Solver::decomp(0, 0, 0);
}
int cat(int v) {
using namespace Solver;
v -= 1;
state[v] = 1;
return set(v, std::array<long long, 2>{ vals[v][0], MAX });
}
int dog(int v) {
using namespace Solver;
v -= 1;
state[v] = 2;
return set(v, std::array<long long, 2>{ MAX, vals[v][1] });
}
int neighbor(int v) {
using namespace Solver;
v -= 1;
state[v] = 0;
return set(v, vals[v]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5888 KB |
Output is correct |
2 |
Correct |
4 ms |
5868 KB |
Output is correct |
3 |
Correct |
4 ms |
5868 KB |
Output is correct |
4 |
Correct |
4 ms |
5868 KB |
Output is correct |
5 |
Correct |
4 ms |
6016 KB |
Output is correct |
6 |
Correct |
4 ms |
5868 KB |
Output is correct |
7 |
Correct |
4 ms |
5868 KB |
Output is correct |
8 |
Correct |
4 ms |
5868 KB |
Output is correct |
9 |
Correct |
4 ms |
5868 KB |
Output is correct |
10 |
Correct |
4 ms |
5868 KB |
Output is correct |
11 |
Correct |
4 ms |
5868 KB |
Output is correct |
12 |
Correct |
4 ms |
5868 KB |
Output is correct |
13 |
Correct |
4 ms |
5888 KB |
Output is correct |
14 |
Correct |
4 ms |
5868 KB |
Output is correct |
15 |
Correct |
4 ms |
5888 KB |
Output is correct |
16 |
Correct |
4 ms |
5868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5888 KB |
Output is correct |
2 |
Correct |
4 ms |
5868 KB |
Output is correct |
3 |
Correct |
4 ms |
5868 KB |
Output is correct |
4 |
Correct |
4 ms |
5868 KB |
Output is correct |
5 |
Correct |
4 ms |
6016 KB |
Output is correct |
6 |
Correct |
4 ms |
5868 KB |
Output is correct |
7 |
Correct |
4 ms |
5868 KB |
Output is correct |
8 |
Correct |
4 ms |
5868 KB |
Output is correct |
9 |
Correct |
4 ms |
5868 KB |
Output is correct |
10 |
Correct |
4 ms |
5868 KB |
Output is correct |
11 |
Correct |
4 ms |
5868 KB |
Output is correct |
12 |
Correct |
4 ms |
5868 KB |
Output is correct |
13 |
Correct |
4 ms |
5888 KB |
Output is correct |
14 |
Correct |
4 ms |
5868 KB |
Output is correct |
15 |
Correct |
4 ms |
5888 KB |
Output is correct |
16 |
Correct |
4 ms |
5868 KB |
Output is correct |
17 |
Correct |
6 ms |
5996 KB |
Output is correct |
18 |
Correct |
7 ms |
6176 KB |
Output is correct |
19 |
Correct |
5 ms |
5996 KB |
Output is correct |
20 |
Correct |
4 ms |
5868 KB |
Output is correct |
21 |
Correct |
6 ms |
5868 KB |
Output is correct |
22 |
Correct |
5 ms |
5868 KB |
Output is correct |
23 |
Correct |
7 ms |
5996 KB |
Output is correct |
24 |
Correct |
6 ms |
5996 KB |
Output is correct |
25 |
Correct |
6 ms |
5868 KB |
Output is correct |
26 |
Correct |
5 ms |
5868 KB |
Output is correct |
27 |
Correct |
5 ms |
5868 KB |
Output is correct |
28 |
Correct |
5 ms |
5996 KB |
Output is correct |
29 |
Correct |
6 ms |
5996 KB |
Output is correct |
30 |
Correct |
6 ms |
5868 KB |
Output is correct |
31 |
Correct |
5 ms |
5996 KB |
Output is correct |
32 |
Correct |
5 ms |
5868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5888 KB |
Output is correct |
2 |
Correct |
4 ms |
5868 KB |
Output is correct |
3 |
Correct |
4 ms |
5868 KB |
Output is correct |
4 |
Correct |
4 ms |
5868 KB |
Output is correct |
5 |
Correct |
4 ms |
6016 KB |
Output is correct |
6 |
Correct |
4 ms |
5868 KB |
Output is correct |
7 |
Correct |
4 ms |
5868 KB |
Output is correct |
8 |
Correct |
4 ms |
5868 KB |
Output is correct |
9 |
Correct |
4 ms |
5868 KB |
Output is correct |
10 |
Correct |
4 ms |
5868 KB |
Output is correct |
11 |
Correct |
4 ms |
5868 KB |
Output is correct |
12 |
Correct |
4 ms |
5868 KB |
Output is correct |
13 |
Correct |
4 ms |
5888 KB |
Output is correct |
14 |
Correct |
4 ms |
5868 KB |
Output is correct |
15 |
Correct |
4 ms |
5888 KB |
Output is correct |
16 |
Correct |
4 ms |
5868 KB |
Output is correct |
17 |
Correct |
6 ms |
5996 KB |
Output is correct |
18 |
Correct |
7 ms |
6176 KB |
Output is correct |
19 |
Correct |
5 ms |
5996 KB |
Output is correct |
20 |
Correct |
4 ms |
5868 KB |
Output is correct |
21 |
Correct |
6 ms |
5868 KB |
Output is correct |
22 |
Correct |
5 ms |
5868 KB |
Output is correct |
23 |
Correct |
7 ms |
5996 KB |
Output is correct |
24 |
Correct |
6 ms |
5996 KB |
Output is correct |
25 |
Correct |
6 ms |
5868 KB |
Output is correct |
26 |
Correct |
5 ms |
5868 KB |
Output is correct |
27 |
Correct |
5 ms |
5868 KB |
Output is correct |
28 |
Correct |
5 ms |
5996 KB |
Output is correct |
29 |
Correct |
6 ms |
5996 KB |
Output is correct |
30 |
Correct |
6 ms |
5868 KB |
Output is correct |
31 |
Correct |
5 ms |
5996 KB |
Output is correct |
32 |
Correct |
5 ms |
5868 KB |
Output is correct |
33 |
Correct |
361 ms |
15576 KB |
Output is correct |
34 |
Correct |
136 ms |
16364 KB |
Output is correct |
35 |
Correct |
285 ms |
12900 KB |
Output is correct |
36 |
Correct |
517 ms |
22196 KB |
Output is correct |
37 |
Correct |
26 ms |
10732 KB |
Output is correct |
38 |
Correct |
627 ms |
23900 KB |
Output is correct |
39 |
Correct |
588 ms |
23992 KB |
Output is correct |
40 |
Correct |
597 ms |
23912 KB |
Output is correct |
41 |
Correct |
624 ms |
23900 KB |
Output is correct |
42 |
Correct |
558 ms |
23900 KB |
Output is correct |
43 |
Correct |
577 ms |
23960 KB |
Output is correct |
44 |
Correct |
556 ms |
23876 KB |
Output is correct |
45 |
Correct |
560 ms |
23944 KB |
Output is correct |
46 |
Correct |
547 ms |
23980 KB |
Output is correct |
47 |
Correct |
582 ms |
23900 KB |
Output is correct |
48 |
Correct |
145 ms |
18404 KB |
Output is correct |
49 |
Correct |
160 ms |
21524 KB |
Output is correct |
50 |
Correct |
51 ms |
9324 KB |
Output is correct |
51 |
Correct |
59 ms |
11984 KB |
Output is correct |
52 |
Correct |
30 ms |
9068 KB |
Output is correct |
53 |
Correct |
260 ms |
22112 KB |
Output is correct |
54 |
Correct |
171 ms |
13268 KB |
Output is correct |
55 |
Correct |
483 ms |
18784 KB |
Output is correct |
56 |
Correct |
283 ms |
14228 KB |
Output is correct |
57 |
Correct |
401 ms |
21160 KB |
Output is correct |
58 |
Correct |
38 ms |
12392 KB |
Output is correct |
59 |
Correct |
60 ms |
11500 KB |
Output is correct |
60 |
Correct |
137 ms |
19820 KB |
Output is correct |
61 |
Correct |
151 ms |
20436 KB |
Output is correct |
62 |
Correct |
101 ms |
17476 KB |
Output is correct |
63 |
Correct |
81 ms |
15596 KB |
Output is correct |
64 |
Correct |
100 ms |
17260 KB |
Output is correct |
65 |
Correct |
108 ms |
23916 KB |
Output is correct |
66 |
Correct |
180 ms |
10604 KB |
Output is correct |
67 |
Correct |
129 ms |
18668 KB |
Output is correct |
68 |
Correct |
327 ms |
24556 KB |
Output is correct |
69 |
Correct |
88 ms |
7660 KB |
Output is correct |
70 |
Correct |
23 ms |
6124 KB |
Output is correct |
71 |
Correct |
147 ms |
14188 KB |
Output is correct |
72 |
Correct |
202 ms |
21100 KB |
Output is correct |
73 |
Correct |
511 ms |
27244 KB |
Output is correct |
74 |
Correct |
544 ms |
24428 KB |
Output is correct |
75 |
Correct |
380 ms |
27392 KB |
Output is correct |
76 |
Correct |
363 ms |
26092 KB |
Output is correct |
77 |
Correct |
504 ms |
24684 KB |
Output is correct |