#include <bits/stdc++.h>
#include "catdog.h"
#define INF 0x3f3f3f3f
using namespace std;
const int kN = 1e5;
vector<int> g[1 + kN];
int labels, sz[1 + kN], p[1 + kN], heavySon[1 + kN], chainTop[1 + kN], label[1 + kN], down[1 + kN], last[1 + kN][2], sum[1 + kN][2];
short col[1 + kN];
void minSelf(int &x, int y) {
if (y < x) {
x = y;
}
}
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;
}
}
}
void init(int pos, int c) {
dp[0][1] = dp[1][0] = INF;
for (int i = 0; i < 2; ++i) {
if (i != c && c != 2) {
dp[i][i] = INF;
} else {
dp[i][i] = sum[pos][i];
}
}
}
node operator + (const node &rhs) const {
node ret;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
ret.dp[i][j] = INF;
}
}
for (int a = 0; a < 2; ++a) {
for (int b = 0; b < 2; ++b) {
for (int c = 0; c < 2; ++c) {
for (int d = 0; d < 2; ++d) {
minSelf(ret.dp[a][d], dp[a][b] + rhs.dp[c][d] + (b != c));
}
}
}
}
return ret;
}
};
struct ST {
int n;
vector<node> t;
void init(int N) {
n = N;
int dim = 1;
while (dim < n) {
dim *= 2;
}
t.resize(dim * 2);
}
void build(int x, int lx, int rx) {
if (lx == rx) {
t[x].init(lx, 2);
return;
}
int mid = (lx + rx) / 2;
build(x * 2, lx, mid);
build(x * 2 + 1, mid + 1, rx);
t[x] = t[x * 2] + t[x * 2 + 1];
}
void update(int x, int lx, int rx, int pos, int c) {
if (lx == rx) {
t[x].init(pos, c);
return;
}
int mid = (lx + rx) / 2;
if (pos <= mid) {
update(x * 2, lx, mid, pos, c);
} else {
update(x * 2 + 1, mid + 1, rx, pos, c);
}
t[x] = t[x * 2] + t[x * 2 + 1];
}
void update(int pos, int c) {
update(1, 1, n, pos, c);
}
node query(int x, int lx, int rx, int st, int dr) {
if (st <= lx && rx <= dr) {
return t[x];
}
int mid = (lx + rx) / 2;
if (st <= mid && mid < dr) {
return query(x * 2, lx, mid, st, dr) + query(x * 2 + 1, mid + 1, rx, st, dr);
}
if (st <= mid) {
return query(x * 2, lx, mid, st, dr);
}
return query(x * 2 + 1, mid + 1, rx, st, dr);
}
node query(int st, int dr) {
return query(1, 1, n, st, dr);
}
} t;
void dfs1(int u) {
sz[u] = 1;
chainTop[u] = u;
for (int v : g[u]) {
if (v != p[u]) {
p[v] = u;
dfs1(v);
if (sz[heavySon[u]] < sz[v]) {
heavySon[u] = v;
}
sz[u] += sz[v];
}
}
}
void dfs2(int u) {
label[u] = ++labels;
down[chainTop[u]] = u;
if (heavySon[u] == 0) {
return;
}
chainTop[heavySon[u]] = chainTop[u];
dfs2(heavySon[u]);
for (int v : g[u]) {
if (v != p[u] && v != heavySon[u]) {
dfs2(v);
}
}
}
int getDp(node x, int i) {
int best = INF;
for (int j = 0; j < 2; ++j) {
minSelf(best, x.dp[i][j]);
minSelf(best, x.dp[i ^ 1][j] + 1);
}
return best;
}
node update(int v) {
t.update(label[v], col[v]);
int root = chainTop[v];
node chain = t.query(label[root], label[down[root]]);
if (root == 1) {
return chain;
}
for (int i = 0; i < 2; ++i) {
sum[label[p[root]]][i] -= last[root][i];
last[root][i] = getDp(chain, i);
sum[label[p[root]]][i] += last[root][i];
}
return update(p[root]);
}
void initialize(int N, vector<int> A, vector<int> B) {
for (int i = 0; i < N - 1; ++i) {
g[A[i]].emplace_back(B[i]);
g[B[i]].emplace_back(A[i]);
}
for (int v = 1; v <= N; ++v) {
col[v] = 2;
}
dfs1(1);
dfs2(1);
t.init(N);
t.build(1, 1, N);
}
int cat(int v) {
col[v] = 0;
node ret = update(v);
return min(getDp(ret, 0), getDp(ret, 1));
}
int dog(int v) {
col[v] = 1;
node ret = update(v);
return min(getDp(ret, 0), getDp(ret, 1));
}
int neighbor(int v) {
col[v] = 2;
node ret = update(v);
return min(getDp(ret, 0), getDp(ret, 1));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2656 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2656 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
3 ms |
2764 KB |
Output is correct |
18 |
Correct |
3 ms |
2764 KB |
Output is correct |
19 |
Correct |
3 ms |
2764 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
3 ms |
2764 KB |
Output is correct |
24 |
Correct |
3 ms |
2764 KB |
Output is correct |
25 |
Correct |
3 ms |
2636 KB |
Output is correct |
26 |
Correct |
3 ms |
2636 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2764 KB |
Output is correct |
29 |
Correct |
3 ms |
2788 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
2 ms |
2764 KB |
Output is correct |
32 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2656 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
3 ms |
2764 KB |
Output is correct |
18 |
Correct |
3 ms |
2764 KB |
Output is correct |
19 |
Correct |
3 ms |
2764 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
3 ms |
2764 KB |
Output is correct |
24 |
Correct |
3 ms |
2764 KB |
Output is correct |
25 |
Correct |
3 ms |
2636 KB |
Output is correct |
26 |
Correct |
3 ms |
2636 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2764 KB |
Output is correct |
29 |
Correct |
3 ms |
2788 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
2 ms |
2764 KB |
Output is correct |
32 |
Correct |
2 ms |
2636 KB |
Output is correct |
33 |
Correct |
312 ms |
9928 KB |
Output is correct |
34 |
Correct |
102 ms |
10332 KB |
Output is correct |
35 |
Correct |
301 ms |
8912 KB |
Output is correct |
36 |
Correct |
489 ms |
15884 KB |
Output is correct |
37 |
Correct |
16 ms |
6480 KB |
Output is correct |
38 |
Correct |
582 ms |
16644 KB |
Output is correct |
39 |
Correct |
589 ms |
16648 KB |
Output is correct |
40 |
Correct |
520 ms |
16652 KB |
Output is correct |
41 |
Correct |
616 ms |
16652 KB |
Output is correct |
42 |
Correct |
495 ms |
16656 KB |
Output is correct |
43 |
Correct |
511 ms |
16612 KB |
Output is correct |
44 |
Correct |
492 ms |
16668 KB |
Output is correct |
45 |
Correct |
508 ms |
16656 KB |
Output is correct |
46 |
Correct |
502 ms |
16612 KB |
Output is correct |
47 |
Correct |
572 ms |
16644 KB |
Output is correct |
48 |
Correct |
122 ms |
13140 KB |
Output is correct |
49 |
Correct |
156 ms |
14700 KB |
Output is correct |
50 |
Correct |
51 ms |
5624 KB |
Output is correct |
51 |
Correct |
53 ms |
7852 KB |
Output is correct |
52 |
Correct |
24 ms |
5420 KB |
Output is correct |
53 |
Correct |
199 ms |
15460 KB |
Output is correct |
54 |
Correct |
178 ms |
8828 KB |
Output is correct |
55 |
Correct |
433 ms |
13884 KB |
Output is correct |
56 |
Correct |
221 ms |
9452 KB |
Output is correct |
57 |
Correct |
294 ms |
15000 KB |
Output is correct |
58 |
Correct |
22 ms |
7904 KB |
Output is correct |
59 |
Correct |
48 ms |
6740 KB |
Output is correct |
60 |
Correct |
123 ms |
13764 KB |
Output is correct |
61 |
Correct |
133 ms |
14092 KB |
Output is correct |
62 |
Correct |
75 ms |
12444 KB |
Output is correct |
63 |
Correct |
40 ms |
11240 KB |
Output is correct |
64 |
Correct |
45 ms |
12760 KB |
Output is correct |
65 |
Correct |
56 ms |
19268 KB |
Output is correct |
66 |
Correct |
59 ms |
6876 KB |
Output is correct |
67 |
Correct |
59 ms |
15556 KB |
Output is correct |
68 |
Correct |
122 ms |
19204 KB |
Output is correct |
69 |
Correct |
29 ms |
4264 KB |
Output is correct |
70 |
Correct |
7 ms |
2892 KB |
Output is correct |
71 |
Correct |
51 ms |
10784 KB |
Output is correct |
72 |
Correct |
82 ms |
17692 KB |
Output is correct |
73 |
Correct |
225 ms |
22136 KB |
Output is correct |
74 |
Correct |
217 ms |
18756 KB |
Output is correct |
75 |
Correct |
144 ms |
22128 KB |
Output is correct |
76 |
Correct |
135 ms |
20836 KB |
Output is correct |
77 |
Correct |
227 ms |
19064 KB |
Output is correct |