#include <bits/stdc++.h>
#include "catdog.h"
using namespace std;
using ll = long long;
int n;
constexpr int N = 100001, inf = 1e9 + 7;
vector<int> g[N];
int head[N], sz[N], parent[N], tin[N], T = 0, hld_sz[N], type[N];
void dfs_init(int v) {
auto it = find(g[v].begin(), g[v].end(), parent[v]);
if (it != g[v].end()) {
g[v].erase(it);
}
sz[v] = 1;
for (int &to: g[v]) {
parent[to] = v;
dfs_init(to);
sz[v] += sz[to];
}
for (int i = 1; i < g[v].size(); ++i) {
if (sz[g[v][i]] > sz[g[v][0]]) {
swap(g[v][i], g[v][0]);
}
}
}
void dfs_hld(int v) {
if (head[v] == 0) {
head[v] = v;
}
tin[v] = T++;
hld_sz[head[v]] += 1;
for (int to : g[v]) {
if (to == g[v][0]) {
head[to] = head[v];
}
dfs_hld(to);
}
}
struct Info {
array<ll, 4> dp{0, inf, inf, 0};
Info() = default;
Info(array<ll, 4> d) : dp(d) {}
};
Info operator+(Info a, Info b) {
Info res;
fill(res.dp.begin(), res.dp.end(), inf);
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
int x = (i & 2) + (j & 1);
res.dp[x] = min(res.dp[x], a.dp[i] + b.dp[j] + ((i & 1) != bool(j & 2)));
}
}
return res;
}
struct SegmentTree {
vector<Info> t;
int n{};
void init(int m) {
n = 1 << (__lg(m) + bool(m & (m - 1)));
t.assign(2 * n, {});
}
void modify(int i, ll x, ll y) {
i += n;
t[i].dp[0] += x, t[i].dp[3] += y;
while (i >>= 1) {
t[i] = t[i << 1] + t[i << 1 | 1];
}
}
};
array<ll, 2> transform(array<ll, 4> &a) {
ll x = min(a[0], a[1]), y = min(a[2], a[3]);
return {min(x, y + 1), min(x + 1, y)};
}
SegmentTree t[N];
void update(int v, ll x, ll y) {
while (v != 0) {
int u = head[v];
auto was = transform(t[u].t[1].dp);
t[u].modify(tin[v] - tin[u], x, y);
auto now = transform(t[u].t[1].dp);
v = parent[u];
x = now[0] - was[0];
y = now[1] - was[1];
}
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
n = N;
for (int i = 0; i < n - 1; ++i) {
g[A[i]].push_back(B[i]);
g[B[i]].push_back(A[i]);
}
dfs_init(1);
dfs_hld(1);
for (int i = 1; i <= n; ++i) {
if (head[i] == i) {
t[i].init(hld_sz[i]);
}
}
}
int query() {
auto res = t[1].t[1].dp;
return *min_element(res.begin(), res.end());
}
int cat(int v) {
type[v] = 0;
update(v, 0, N);
return query();
}
int dog(int v) {
type[v] = 1;
update(v, N, 0);
return query();
}
int neighbor(int v) {
if (type[v] == 0) {
update(v, 0, -N);
} else {
update(v, -N, 0);
}
type[v] = 2;
return query();
}
Compilation message
catdog.cpp: In function 'void dfs_init(int)':
catdog.cpp:30:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int i = 1; i < g[v].size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5796 KB |
Output is correct |
2 |
Correct |
4 ms |
5796 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
4 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
4 ms |
5792 KB |
Output is correct |
7 |
Correct |
4 ms |
5796 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5788 KB |
Output is correct |
10 |
Correct |
3 ms |
5716 KB |
Output is correct |
11 |
Correct |
3 ms |
5716 KB |
Output is correct |
12 |
Correct |
4 ms |
5716 KB |
Output is correct |
13 |
Correct |
4 ms |
5760 KB |
Output is correct |
14 |
Correct |
4 ms |
5716 KB |
Output is correct |
15 |
Correct |
4 ms |
5716 KB |
Output is correct |
16 |
Correct |
4 ms |
5716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5796 KB |
Output is correct |
2 |
Correct |
4 ms |
5796 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
4 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
4 ms |
5792 KB |
Output is correct |
7 |
Correct |
4 ms |
5796 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5788 KB |
Output is correct |
10 |
Correct |
3 ms |
5716 KB |
Output is correct |
11 |
Correct |
3 ms |
5716 KB |
Output is correct |
12 |
Correct |
4 ms |
5716 KB |
Output is correct |
13 |
Correct |
4 ms |
5760 KB |
Output is correct |
14 |
Correct |
4 ms |
5716 KB |
Output is correct |
15 |
Correct |
4 ms |
5716 KB |
Output is correct |
16 |
Correct |
4 ms |
5716 KB |
Output is correct |
17 |
Correct |
6 ms |
5844 KB |
Output is correct |
18 |
Correct |
4 ms |
5844 KB |
Output is correct |
19 |
Correct |
4 ms |
5932 KB |
Output is correct |
20 |
Correct |
4 ms |
5844 KB |
Output is correct |
21 |
Correct |
4 ms |
5808 KB |
Output is correct |
22 |
Correct |
4 ms |
5796 KB |
Output is correct |
23 |
Correct |
8 ms |
5844 KB |
Output is correct |
24 |
Correct |
4 ms |
5928 KB |
Output is correct |
25 |
Correct |
7 ms |
5860 KB |
Output is correct |
26 |
Correct |
3 ms |
5844 KB |
Output is correct |
27 |
Correct |
4 ms |
5844 KB |
Output is correct |
28 |
Correct |
6 ms |
5972 KB |
Output is correct |
29 |
Correct |
4 ms |
6012 KB |
Output is correct |
30 |
Correct |
3 ms |
5796 KB |
Output is correct |
31 |
Correct |
4 ms |
5844 KB |
Output is correct |
32 |
Correct |
5 ms |
5800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5796 KB |
Output is correct |
2 |
Correct |
4 ms |
5796 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
4 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
4 ms |
5792 KB |
Output is correct |
7 |
Correct |
4 ms |
5796 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5788 KB |
Output is correct |
10 |
Correct |
3 ms |
5716 KB |
Output is correct |
11 |
Correct |
3 ms |
5716 KB |
Output is correct |
12 |
Correct |
4 ms |
5716 KB |
Output is correct |
13 |
Correct |
4 ms |
5760 KB |
Output is correct |
14 |
Correct |
4 ms |
5716 KB |
Output is correct |
15 |
Correct |
4 ms |
5716 KB |
Output is correct |
16 |
Correct |
4 ms |
5716 KB |
Output is correct |
17 |
Correct |
6 ms |
5844 KB |
Output is correct |
18 |
Correct |
4 ms |
5844 KB |
Output is correct |
19 |
Correct |
4 ms |
5932 KB |
Output is correct |
20 |
Correct |
4 ms |
5844 KB |
Output is correct |
21 |
Correct |
4 ms |
5808 KB |
Output is correct |
22 |
Correct |
4 ms |
5796 KB |
Output is correct |
23 |
Correct |
8 ms |
5844 KB |
Output is correct |
24 |
Correct |
4 ms |
5928 KB |
Output is correct |
25 |
Correct |
7 ms |
5860 KB |
Output is correct |
26 |
Correct |
3 ms |
5844 KB |
Output is correct |
27 |
Correct |
4 ms |
5844 KB |
Output is correct |
28 |
Correct |
6 ms |
5972 KB |
Output is correct |
29 |
Correct |
4 ms |
6012 KB |
Output is correct |
30 |
Correct |
3 ms |
5796 KB |
Output is correct |
31 |
Correct |
4 ms |
5844 KB |
Output is correct |
32 |
Correct |
5 ms |
5800 KB |
Output is correct |
33 |
Correct |
127 ms |
15324 KB |
Output is correct |
34 |
Correct |
64 ms |
16332 KB |
Output is correct |
35 |
Correct |
173 ms |
12952 KB |
Output is correct |
36 |
Correct |
239 ms |
22080 KB |
Output is correct |
37 |
Correct |
19 ms |
10668 KB |
Output is correct |
38 |
Correct |
249 ms |
23892 KB |
Output is correct |
39 |
Correct |
291 ms |
23892 KB |
Output is correct |
40 |
Correct |
217 ms |
23936 KB |
Output is correct |
41 |
Correct |
287 ms |
23912 KB |
Output is correct |
42 |
Correct |
328 ms |
23904 KB |
Output is correct |
43 |
Correct |
241 ms |
23812 KB |
Output is correct |
44 |
Correct |
222 ms |
23884 KB |
Output is correct |
45 |
Correct |
207 ms |
23912 KB |
Output is correct |
46 |
Correct |
250 ms |
23904 KB |
Output is correct |
47 |
Correct |
217 ms |
23856 KB |
Output is correct |
48 |
Correct |
63 ms |
18524 KB |
Output is correct |
49 |
Correct |
90 ms |
21808 KB |
Output is correct |
50 |
Correct |
23 ms |
9264 KB |
Output is correct |
51 |
Correct |
33 ms |
12048 KB |
Output is correct |
52 |
Correct |
14 ms |
9020 KB |
Output is correct |
53 |
Correct |
111 ms |
22204 KB |
Output is correct |
54 |
Correct |
81 ms |
13152 KB |
Output is correct |
55 |
Correct |
179 ms |
18524 KB |
Output is correct |
56 |
Correct |
104 ms |
14196 KB |
Output is correct |
57 |
Correct |
186 ms |
21344 KB |
Output is correct |
58 |
Correct |
28 ms |
12488 KB |
Output is correct |
59 |
Correct |
28 ms |
11512 KB |
Output is correct |
60 |
Correct |
63 ms |
19940 KB |
Output is correct |
61 |
Correct |
70 ms |
20600 KB |
Output is correct |
62 |
Correct |
55 ms |
17552 KB |
Output is correct |
63 |
Correct |
49 ms |
16172 KB |
Output is correct |
64 |
Correct |
46 ms |
18632 KB |
Output is correct |
65 |
Correct |
65 ms |
27916 KB |
Output is correct |
66 |
Correct |
71 ms |
11264 KB |
Output is correct |
67 |
Correct |
60 ms |
20436 KB |
Output is correct |
68 |
Correct |
138 ms |
26448 KB |
Output is correct |
69 |
Correct |
30 ms |
7760 KB |
Output is correct |
70 |
Correct |
9 ms |
6056 KB |
Output is correct |
71 |
Correct |
55 ms |
16084 KB |
Output is correct |
72 |
Correct |
81 ms |
24792 KB |
Output is correct |
73 |
Correct |
176 ms |
29220 KB |
Output is correct |
74 |
Correct |
202 ms |
26260 KB |
Output is correct |
75 |
Correct |
140 ms |
29340 KB |
Output is correct |
76 |
Correct |
174 ms |
30144 KB |
Output is correct |
77 |
Correct |
181 ms |
26508 KB |
Output is correct |