#include <bits/stdc++.h>
#include "catdog.h"
using namespace std;
const int inf = 1e5;
struct node
{
int dp[2][2];
node()
{
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
dp[i][j] = inf;
}
}
}
};
struct aint
{
vector<node> a;
void init(int n)
{
a = vector<node>(4 * n);
}
node combine(node a, node b)
{
node c;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
for (int k = 0; k < 2; ++k)
{
for (int l = 0; l < 2; ++l)
{
c.dp[i][j] = min(c.dp[i][j], a.dp[i][k] + b.dp[l][j] + (k != l));
}
}
}
}
return c;
}
void update(int node, int left, int right, int pos, pair<int, int> val)
{
if (left == right)
{
a[node].dp[0][0] = val.first;
a[node].dp[1][1] = val.second;
a[node].dp[0][1] = inf;
a[node].dp[1][0] = inf;
return;
}
int mid = (left + right) / 2;
if (pos <= mid)
{
update(2 * node, left, mid, pos, val);
}
else
{
update(2 * node + 1, mid + 1, right, pos, val);
}
a[node] = combine(a[2 * node], a[2 * node + 1]);
}
node query(int node, int left, int right, int st, int dr)
{
if (st <= left && dr >= right)
{
return a[node];
}
int mid = (left + right) / 2;
if (st <= mid && mid + 1 <= dr)
{
return combine(query(2 * node, left, mid, st, dr), query(2 * node + 1, mid + 1, right, st, dr));
}
if (st <= mid)
{
return query(2 * node, left, mid, st, dr);
}
return query(2 * node + 1, mid + 1, right, st, dr);
}
};
int n;
vector<int> a;
vector<vector<int>> g;
vector<int> heavy, pos, head, par, down;
vector<vector<int>> sum, prv;
aint tree;
int p = 0;
void init_hld()
{
pos = head = par = down = vector<int>(n + 1);
heavy = vector<int>(n + 1, -1);
function<int(int, int)> dfs = [&](int node, int parent)
{
int sz = 1;
int maxi = 0;
for (auto i : g[node])
{
if (i != parent)
{
int cine = dfs(i, node);
sz += cine;
if (cine > maxi)
{
maxi = cine;
heavy[node] = i;
}
}
}
return sz;
};
dfs(1, 0);
function<void(int, int, int)> get = [&](int node, int boss, int parent)
{
par[node] = parent;
head[node] = boss;
pos[node] = ++p;
down[head[node]] = node;
if (heavy[node] != -1)
{
get(heavy[node], boss, node);
}
for (auto i : g[node])
{
if (i != parent && i != heavy[node])
{
get(i, i, node);
}
}
};
get(1, 1, 0);
}
void initialize(int N, vector<int> A, vector<int> B)
{
n = N;
g = vector<vector<int>>(n + 1);
a = vector<int>(n + 1, 2);
prv = sum = vector<vector<int>>(n + 1, vector<int>(2));
for (int i = 0; i <= n - 2; ++i)
{
g[A[i]].push_back(B[i]);
g[B[i]].push_back(A[i]);
}
init_hld();
tree.init(n);
for (int i = 1; i <= n; ++i)
{
tree.update(1, 1, n, i, {0, 0});
}
}
node update(int nd)
{
if (a[nd] == 0)
{
tree.update(1, 1, n, pos[nd], {sum[nd][0], inf});
}
if (a[nd] == 1)
{
tree.update(1, 1, n, pos[nd], {inf, sum[nd][1]});
}
if (a[nd] == 2)
{
tree.update(1, 1, n, pos[nd], {sum[nd][0], sum[nd][1]});
}
int qui = head[nd];
node dp_qui = tree.query(1, 1, n, pos[qui], pos[down[qui]]);
if (qui == 1)
{
return dp_qui;
}
for (int i = 0; i < 2; ++i)
{
sum[par[qui]][i] -= prv[qui][i];
int best = inf;
for (int j = 0; j < 2; ++j)
{
best = min(best, dp_qui.dp[i][j]);
best = min(best, dp_qui.dp[i ^ 1][j] + 1);
}
prv[qui][i] = best;
sum[par[qui]][i] += prv[qui][i];
}
return update(par[qui]);
}
int cat(int v)
{
a[v] = 0;
node ans = update(v);
int best = inf;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
best = min(best, ans.dp[i][j]);
}
}
return best;
}
int dog(int v)
{
a[v] = 1;
node ans = update(v);
int best = inf;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
best = min(best, ans.dp[i][j]);
}
}
return best;
}
int neighbor(int v)
{
a[v] = 2;
node ans = update(v);
int best = inf;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
best = min(best, ans.dp[i][j]);
}
}
return best;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
313 ms |
14564 KB |
Output is correct |
34 |
Correct |
137 ms |
18516 KB |
Output is correct |
35 |
Correct |
262 ms |
11224 KB |
Output is correct |
36 |
Correct |
483 ms |
26716 KB |
Output is correct |
37 |
Correct |
31 ms |
8652 KB |
Output is correct |
38 |
Correct |
544 ms |
29808 KB |
Output is correct |
39 |
Correct |
607 ms |
29812 KB |
Output is correct |
40 |
Correct |
540 ms |
29828 KB |
Output is correct |
41 |
Correct |
536 ms |
29812 KB |
Output is correct |
42 |
Correct |
491 ms |
29824 KB |
Output is correct |
43 |
Correct |
524 ms |
29856 KB |
Output is correct |
44 |
Correct |
491 ms |
29828 KB |
Output is correct |
45 |
Correct |
498 ms |
29780 KB |
Output is correct |
46 |
Correct |
523 ms |
29820 KB |
Output is correct |
47 |
Correct |
527 ms |
29756 KB |
Output is correct |
48 |
Correct |
178 ms |
21392 KB |
Output is correct |
49 |
Correct |
234 ms |
27124 KB |
Output is correct |
50 |
Correct |
60 ms |
5800 KB |
Output is correct |
51 |
Correct |
76 ms |
10700 KB |
Output is correct |
52 |
Correct |
34 ms |
5620 KB |
Output is correct |
53 |
Correct |
248 ms |
28452 KB |
Output is correct |
54 |
Correct |
172 ms |
12388 KB |
Output is correct |
55 |
Correct |
423 ms |
20600 KB |
Output is correct |
56 |
Correct |
245 ms |
13688 KB |
Output is correct |
57 |
Correct |
352 ms |
26248 KB |
Output is correct |
58 |
Correct |
46 ms |
12156 KB |
Output is correct |
59 |
Correct |
68 ms |
9716 KB |
Output is correct |
60 |
Correct |
168 ms |
24080 KB |
Output is correct |
61 |
Correct |
192 ms |
25148 KB |
Output is correct |
62 |
Correct |
119 ms |
20272 KB |
Output is correct |
63 |
Correct |
69 ms |
19148 KB |
Output is correct |
64 |
Correct |
77 ms |
22272 KB |
Output is correct |
65 |
Correct |
113 ms |
35592 KB |
Output is correct |
66 |
Correct |
78 ms |
8416 KB |
Output is correct |
67 |
Correct |
98 ms |
24912 KB |
Output is correct |
68 |
Correct |
203 ms |
36016 KB |
Output is correct |
69 |
Correct |
32 ms |
3020 KB |
Output is correct |
70 |
Correct |
7 ms |
596 KB |
Output is correct |
71 |
Correct |
78 ms |
15904 KB |
Output is correct |
72 |
Correct |
116 ms |
29140 KB |
Output is correct |
73 |
Correct |
251 ms |
40356 KB |
Output is correct |
74 |
Correct |
288 ms |
35372 KB |
Output is correct |
75 |
Correct |
214 ms |
40240 KB |
Output is correct |
76 |
Correct |
192 ms |
38604 KB |
Output is correct |
77 |
Correct |
286 ms |
35884 KB |
Output is correct |