#ifndef LOCAL
#include "catdog.h"
#endif
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define sz(v) int(std::size(v))
using i64 = long long;
const int MAXN = 1e5;
const int INF = 1e6;
int N;
vector<int> adj[MAXN];
bool has[MAXN][2];
int root[MAXN], depth[MAXN], child[MAXN], parent[MAXN], sz[MAXN];
ar<i64, 2> light[MAXN];
using info = ar<ar<int, 2>, 2>;
info operator+(const info one, const info two) {
info me;
for (int x : {0,1}) for (int y : {0,1})
me[x][y] = min(INF,
min(min(one[x][0] + two[0][y], one[x][1] + two[1][y]),
1 + min(one[x][0] + two[1][y], one[x][1] + two[0][y])));
return me;
}
struct segt {
int l, r;
segt *lc, *rc;
info x;
segt(int l, int r) : l(l), r(r) {
if (l < r) {
int m = (l + r) / 2;
lc = new segt(l, m);
rc = new segt(m + 1, r);
x = lc->x + rc->x;
} else x[1][0] = x[0][1] = INF, x[0][0] = x[1][1] = 0;
}
void upd(int i, const ar<int, 2> &v) {
if (l == r) x[0][0] = v[0], x[1][1] = v[1];
else {
i <= lc->r ? lc->upd(i, v) : rc->upd(i, v);
x = lc->x + rc->x;
}
}
ar<int, 2> pack() {
return {min(x[0][0], x[0][1]), min(x[1][0], x[1][1])};
}
} *tree[MAXN];
int dfs_sz(int i) {
int s = 1, x = 0;
child[i] = -1;
for (int j : adj[i]) if (parent[i] != j) {
depth[j] = depth[i] + 1;
parent[j] = i;
int y = dfs_sz(j);
if (y > x) {
x = y;
child[i] = j;
}
s += y;
}
return s;
}
void dfs_hvy(int r, int i) {
++sz[root[i] = r];
if (~child[i]) dfs_hvy(r, child[i]);
for (int j : adj[i]) if (parent[i] != j && child[i] != j) dfs_hvy(j, j);
}
void initialize(int N, vector<int> A, vector<int> B) {
for (int h = 0; h < N - 1; h++) {
auto [i, j] = make_pair(A[h], B[h]);
i--, j--;
adj[i].push_back(j);
adj[j].push_back(i);
}
parent[0] = -1;
dfs_sz(0);
dfs_hvy(0, 0);
for (int i = 0; i < N; i++) if (sz[i]) tree[i] = new segt(0, sz[i] - 1);
}
int pul(int i) {
while (~i) {
int r = root[i];
int p = parent[r];
if (~p) {
auto v = tree[r]->pack();
light[p][0] -= min(v[0], v[1] + 1), light[p][1] -= min(v[0] + 1, v[1]);
}
tree[r]->upd(depth[i] - depth[r], {int(min(light[i][0], i64(INF))), int(min(light[i][1], i64(INF)))});
if (~p) {
auto v = tree[r]->pack();
light[p][0] += min(v[0], v[1] + 1), light[p][1] += min(v[0] + 1, v[1]);
}
i = p;
}
auto v = tree[0]->pack();
return min(v[0], v[1]);
}
int cat(int i) {
i--;
has[i][0] = 1;
light[i][0] += INF;
return pul(i);
}
int dog(int i) {
i--;
has[i][1] = 1;
light[i][1] += INF;
return pul(i);
}
int neighbor(int i) {
i--;
if (has[i][0]) light[i][0] -= INF, has[i][0] = 0;
if (has[i][1]) light[i][1] -= INF, has[i][1] = 0;
return pul(i);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
1 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
7004 KB |
Output is correct |
6 |
Correct |
1 ms |
7004 KB |
Output is correct |
7 |
Correct |
1 ms |
7004 KB |
Output is correct |
8 |
Correct |
1 ms |
7004 KB |
Output is correct |
9 |
Correct |
1 ms |
7004 KB |
Output is correct |
10 |
Correct |
1 ms |
7004 KB |
Output is correct |
11 |
Correct |
1 ms |
7004 KB |
Output is correct |
12 |
Correct |
1 ms |
7000 KB |
Output is correct |
13 |
Correct |
1 ms |
7004 KB |
Output is correct |
14 |
Correct |
1 ms |
7004 KB |
Output is correct |
15 |
Correct |
1 ms |
7004 KB |
Output is correct |
16 |
Correct |
2 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
1 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
7004 KB |
Output is correct |
6 |
Correct |
1 ms |
7004 KB |
Output is correct |
7 |
Correct |
1 ms |
7004 KB |
Output is correct |
8 |
Correct |
1 ms |
7004 KB |
Output is correct |
9 |
Correct |
1 ms |
7004 KB |
Output is correct |
10 |
Correct |
1 ms |
7004 KB |
Output is correct |
11 |
Correct |
1 ms |
7004 KB |
Output is correct |
12 |
Correct |
1 ms |
7000 KB |
Output is correct |
13 |
Correct |
1 ms |
7004 KB |
Output is correct |
14 |
Correct |
1 ms |
7004 KB |
Output is correct |
15 |
Correct |
1 ms |
7004 KB |
Output is correct |
16 |
Correct |
2 ms |
7004 KB |
Output is correct |
17 |
Correct |
2 ms |
7000 KB |
Output is correct |
18 |
Correct |
2 ms |
7004 KB |
Output is correct |
19 |
Correct |
2 ms |
7004 KB |
Output is correct |
20 |
Correct |
1 ms |
7004 KB |
Output is correct |
21 |
Correct |
2 ms |
7004 KB |
Output is correct |
22 |
Correct |
1 ms |
7004 KB |
Output is correct |
23 |
Correct |
2 ms |
7004 KB |
Output is correct |
24 |
Correct |
2 ms |
7004 KB |
Output is correct |
25 |
Correct |
2 ms |
7004 KB |
Output is correct |
26 |
Correct |
2 ms |
7004 KB |
Output is correct |
27 |
Correct |
1 ms |
7004 KB |
Output is correct |
28 |
Correct |
2 ms |
7260 KB |
Output is correct |
29 |
Correct |
2 ms |
7260 KB |
Output is correct |
30 |
Correct |
1 ms |
7004 KB |
Output is correct |
31 |
Correct |
2 ms |
7004 KB |
Output is correct |
32 |
Correct |
1 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
1 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
7004 KB |
Output is correct |
6 |
Correct |
1 ms |
7004 KB |
Output is correct |
7 |
Correct |
1 ms |
7004 KB |
Output is correct |
8 |
Correct |
1 ms |
7004 KB |
Output is correct |
9 |
Correct |
1 ms |
7004 KB |
Output is correct |
10 |
Correct |
1 ms |
7004 KB |
Output is correct |
11 |
Correct |
1 ms |
7004 KB |
Output is correct |
12 |
Correct |
1 ms |
7000 KB |
Output is correct |
13 |
Correct |
1 ms |
7004 KB |
Output is correct |
14 |
Correct |
1 ms |
7004 KB |
Output is correct |
15 |
Correct |
1 ms |
7004 KB |
Output is correct |
16 |
Correct |
2 ms |
7004 KB |
Output is correct |
17 |
Correct |
2 ms |
7000 KB |
Output is correct |
18 |
Correct |
2 ms |
7004 KB |
Output is correct |
19 |
Correct |
2 ms |
7004 KB |
Output is correct |
20 |
Correct |
1 ms |
7004 KB |
Output is correct |
21 |
Correct |
2 ms |
7004 KB |
Output is correct |
22 |
Correct |
1 ms |
7004 KB |
Output is correct |
23 |
Correct |
2 ms |
7004 KB |
Output is correct |
24 |
Correct |
2 ms |
7004 KB |
Output is correct |
25 |
Correct |
2 ms |
7004 KB |
Output is correct |
26 |
Correct |
2 ms |
7004 KB |
Output is correct |
27 |
Correct |
1 ms |
7004 KB |
Output is correct |
28 |
Correct |
2 ms |
7260 KB |
Output is correct |
29 |
Correct |
2 ms |
7260 KB |
Output is correct |
30 |
Correct |
1 ms |
7004 KB |
Output is correct |
31 |
Correct |
2 ms |
7004 KB |
Output is correct |
32 |
Correct |
1 ms |
7004 KB |
Output is correct |
33 |
Correct |
81 ms |
14276 KB |
Output is correct |
34 |
Correct |
40 ms |
15204 KB |
Output is correct |
35 |
Correct |
75 ms |
12364 KB |
Output is correct |
36 |
Correct |
127 ms |
19128 KB |
Output is correct |
37 |
Correct |
12 ms |
10844 KB |
Output is correct |
38 |
Correct |
136 ms |
20300 KB |
Output is correct |
39 |
Correct |
150 ms |
20292 KB |
Output is correct |
40 |
Correct |
138 ms |
20296 KB |
Output is correct |
41 |
Correct |
149 ms |
20308 KB |
Output is correct |
42 |
Correct |
145 ms |
20588 KB |
Output is correct |
43 |
Correct |
141 ms |
20304 KB |
Output is correct |
44 |
Correct |
135 ms |
20308 KB |
Output is correct |
45 |
Correct |
131 ms |
20404 KB |
Output is correct |
46 |
Correct |
135 ms |
20292 KB |
Output is correct |
47 |
Correct |
133 ms |
20280 KB |
Output is correct |
48 |
Correct |
38 ms |
14824 KB |
Output is correct |
49 |
Correct |
44 ms |
16644 KB |
Output is correct |
50 |
Correct |
14 ms |
9304 KB |
Output is correct |
51 |
Correct |
18 ms |
10968 KB |
Output is correct |
52 |
Correct |
9 ms |
9052 KB |
Output is correct |
53 |
Correct |
76 ms |
19680 KB |
Output is correct |
54 |
Correct |
47 ms |
12636 KB |
Output is correct |
55 |
Correct |
109 ms |
16336 KB |
Output is correct |
56 |
Correct |
76 ms |
13316 KB |
Output is correct |
57 |
Correct |
98 ms |
18772 KB |
Output is correct |
58 |
Correct |
13 ms |
11224 KB |
Output is correct |
59 |
Correct |
19 ms |
10588 KB |
Output is correct |
60 |
Correct |
44 ms |
15540 KB |
Output is correct |
61 |
Correct |
45 ms |
16076 KB |
Output is correct |
62 |
Correct |
30 ms |
14288 KB |
Output is correct |
63 |
Correct |
30 ms |
17060 KB |
Output is correct |
64 |
Correct |
33 ms |
19280 KB |
Output is correct |
65 |
Correct |
46 ms |
25932 KB |
Output is correct |
66 |
Correct |
39 ms |
11608 KB |
Output is correct |
67 |
Correct |
47 ms |
20564 KB |
Output is correct |
68 |
Correct |
82 ms |
26532 KB |
Output is correct |
69 |
Correct |
22 ms |
8700 KB |
Output is correct |
70 |
Correct |
5 ms |
7260 KB |
Output is correct |
71 |
Correct |
37 ms |
15652 KB |
Output is correct |
72 |
Correct |
56 ms |
23132 KB |
Output is correct |
73 |
Correct |
114 ms |
29524 KB |
Output is correct |
74 |
Correct |
120 ms |
26088 KB |
Output is correct |
75 |
Correct |
97 ms |
29424 KB |
Output is correct |
76 |
Correct |
93 ms |
28576 KB |
Output is correct |
77 |
Correct |
119 ms |
26364 KB |
Output is correct |