#include <bits/stdc++.h>
using namespace std;
const int DIM = 100005;
const int INF = 1000000005;
struct Matrix {
long long dp[2][2];
Matrix operator *(const Matrix &other) {
Matrix result;
for (int i1 = 0; i1 <= 1; ++i1) {
for (int j2 = 0; j2 <= 1; ++j2) {
long long aux = 1LL * INF * INF;
for (int j1 = 0; j1 <= 1; ++j1) {
for (int i2 = 0; i2 <= 1; ++i2) {
aux = min(aux, (*this).dp[i1][j1] + other.dp[i2][j2] + (j1 != i2)); } }
result.dp[i1][j2] = aux; } }
return result; }
};
int szt[DIM], msk[DIM], wtc[DIM], pic[DIM], ftc[DIM];
vector<Matrix> sgt[DIM]; vector<int> chn[DIM], edg[DIM];
void preDfs(int x, int f, int &m) {
int big = 0; szt[x] = 1;
for (int y : edg[x]) if (y != f) {
preDfs(y, x, m); szt[x] += szt[y];
if (!big or szt[big] < szt[y]) {
big = y; } }
if (!big) {
wtc[x] = ++m; }
else {
wtc[x] = wtc[big];
for (int y : edg[x])
if (y != f and y != big) {
ftc[wtc[y]] = x; } }
chn[wtc[x]].push_back(x); }
void build(int ch, int nd, int le, int ri) {
if (le == ri) {
sgt[ch][nd].dp[0][0] = sgt[ch][nd].dp[1][1] = 0;
sgt[ch][nd].dp[0][1] = sgt[ch][nd].dp[1][0] = INF; }
else {
int md = (le + ri) >> 1;
build(ch, nd << 1, le, md);
build(ch, nd << 1 | 1, md + 1, ri);
sgt[ch][nd] = sgt[ch][nd << 1] * sgt[ch][nd << 1 | 1]; } }
void update(int ch, int nd, int le, int ri, int ps, long long vl, long long vr) {
if (le == ri) {
sgt[ch][nd].dp[0][0] += vl; sgt[ch][nd].dp[1][1] += vr; }
else {
int md = (le + ri) >> 1;
if (ps <= md) {
update(ch, nd << 1, le, md, ps, vl, vr); }
else {
update(ch, nd << 1 | 1, md + 1, ri, ps, vl, vr); }
sgt[ch][nd] = sgt[ch][nd << 1] * sgt[ch][nd << 1 | 1]; } }
void updateChain(int x, long long vl, long long vr) {
int ch = wtc[x];
long long vl1 = min(sgt[ch][1].dp[0][0], sgt[ch][1].dp[0][1]),
vr1 = min(sgt[ch][1].dp[1][0], sgt[ch][1].dp[1][1]);
update(ch, 1, 1, chn[ch][0], pic[x], vl, vr);
long long vl2 = min(sgt[ch][1].dp[0][0], sgt[ch][1].dp[0][1]),
vr2 = min(sgt[ch][1].dp[1][0], sgt[ch][1].dp[1][1]);
if (ftc[ch]) {
updateChain(ftc[ch], min(vl2, vr2 + 1) - min(vl1, vr1 + 1),
min(vr2, vl2 + 1) - min(vr1, vl1 + 1)); } }
void initialize(int n, vector<int> end1, vector<int> end2) {
for (int i = 1; i < n; ++i) {
int x = end1[i - 1], y = end2[i - 1];
edg[x].push_back(y); edg[y].push_back(x); }
int m = 0; preDfs(1, 0, m);
for (int i = 1; i <= m; ++i) {
chn[i].push_back(chn[i].size());
reverse(chn[i].begin(), chn[i].end());
sgt[i].resize(chn[i][0] << 2);
for (int j = 1; j <= chn[i][0]; ++j) {
pic[chn[i][j]] = j; }
build(i, 1, 1, chn[i][0]); } }
inline int answer(void) {
int ch = wtc[1]; long long mn = 1LL * INF * INF;
for (int i = 0; i <= 1; ++i) {
for (int j = 0; j <= 1; ++j) {
mn = min(mn, sgt[ch][1].dp[i][j]); } }
return mn; }
int cat(int x) {
updateChain(x, INF, 0); msk[x] = 1;
return answer(); }
int dog(int x) {
updateChain(x, 0, INF); msk[x] = 2;
return answer(); }
int neighbor(int x) {
if (msk[x] == 1) {
updateChain(x, -INF, 0); }
else {
updateChain(x, 0, -INF); }
return answer(); }
#ifdef HOME
int main(void) {
freopen("catdog.in", "r", stdin);
freopen("catdog.out", "w", stdout);
int n; cin >> n;
vector<int> a(n - 1), b(n - 1);
for (int i = 0; i <= n - 2; ++i) {
cin >> a[i] >> b[i]; }
initialize(n, a, b);
int q; cin >> q;
while (q--) {
int t, x; cin >> t >> x;
if (t == 1) { cout << cat(x) << endl; }
if (t == 2) { cout << dog(x) << endl; }
if (t == 3) { cout << neighbor(x) << endl; } }
return 0; }
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
8 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7396 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
9 ms |
7424 KB |
Output is correct |
8 |
Correct |
8 ms |
7424 KB |
Output is correct |
9 |
Correct |
10 ms |
7424 KB |
Output is correct |
10 |
Correct |
9 ms |
7424 KB |
Output is correct |
11 |
Correct |
10 ms |
7424 KB |
Output is correct |
12 |
Correct |
9 ms |
7424 KB |
Output is correct |
13 |
Correct |
11 ms |
7424 KB |
Output is correct |
14 |
Correct |
10 ms |
7356 KB |
Output is correct |
15 |
Correct |
10 ms |
7424 KB |
Output is correct |
16 |
Correct |
10 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
8 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7396 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
9 ms |
7424 KB |
Output is correct |
8 |
Correct |
8 ms |
7424 KB |
Output is correct |
9 |
Correct |
10 ms |
7424 KB |
Output is correct |
10 |
Correct |
9 ms |
7424 KB |
Output is correct |
11 |
Correct |
10 ms |
7424 KB |
Output is correct |
12 |
Correct |
9 ms |
7424 KB |
Output is correct |
13 |
Correct |
11 ms |
7424 KB |
Output is correct |
14 |
Correct |
10 ms |
7356 KB |
Output is correct |
15 |
Correct |
10 ms |
7424 KB |
Output is correct |
16 |
Correct |
10 ms |
7424 KB |
Output is correct |
17 |
Correct |
10 ms |
7680 KB |
Output is correct |
18 |
Correct |
11 ms |
7680 KB |
Output is correct |
19 |
Correct |
10 ms |
7552 KB |
Output is correct |
20 |
Correct |
9 ms |
7424 KB |
Output is correct |
21 |
Correct |
10 ms |
7468 KB |
Output is correct |
22 |
Correct |
10 ms |
7552 KB |
Output is correct |
23 |
Correct |
10 ms |
7552 KB |
Output is correct |
24 |
Correct |
10 ms |
7552 KB |
Output is correct |
25 |
Correct |
10 ms |
7552 KB |
Output is correct |
26 |
Correct |
12 ms |
7552 KB |
Output is correct |
27 |
Correct |
10 ms |
7484 KB |
Output is correct |
28 |
Correct |
11 ms |
7552 KB |
Output is correct |
29 |
Correct |
10 ms |
7552 KB |
Output is correct |
30 |
Correct |
10 ms |
7424 KB |
Output is correct |
31 |
Correct |
10 ms |
7580 KB |
Output is correct |
32 |
Correct |
10 ms |
7552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
8 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7396 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
9 ms |
7424 KB |
Output is correct |
8 |
Correct |
8 ms |
7424 KB |
Output is correct |
9 |
Correct |
10 ms |
7424 KB |
Output is correct |
10 |
Correct |
9 ms |
7424 KB |
Output is correct |
11 |
Correct |
10 ms |
7424 KB |
Output is correct |
12 |
Correct |
9 ms |
7424 KB |
Output is correct |
13 |
Correct |
11 ms |
7424 KB |
Output is correct |
14 |
Correct |
10 ms |
7356 KB |
Output is correct |
15 |
Correct |
10 ms |
7424 KB |
Output is correct |
16 |
Correct |
10 ms |
7424 KB |
Output is correct |
17 |
Correct |
10 ms |
7680 KB |
Output is correct |
18 |
Correct |
11 ms |
7680 KB |
Output is correct |
19 |
Correct |
10 ms |
7552 KB |
Output is correct |
20 |
Correct |
9 ms |
7424 KB |
Output is correct |
21 |
Correct |
10 ms |
7468 KB |
Output is correct |
22 |
Correct |
10 ms |
7552 KB |
Output is correct |
23 |
Correct |
10 ms |
7552 KB |
Output is correct |
24 |
Correct |
10 ms |
7552 KB |
Output is correct |
25 |
Correct |
10 ms |
7552 KB |
Output is correct |
26 |
Correct |
12 ms |
7552 KB |
Output is correct |
27 |
Correct |
10 ms |
7484 KB |
Output is correct |
28 |
Correct |
11 ms |
7552 KB |
Output is correct |
29 |
Correct |
10 ms |
7552 KB |
Output is correct |
30 |
Correct |
10 ms |
7424 KB |
Output is correct |
31 |
Correct |
10 ms |
7580 KB |
Output is correct |
32 |
Correct |
10 ms |
7552 KB |
Output is correct |
33 |
Correct |
188 ms |
20080 KB |
Output is correct |
34 |
Correct |
130 ms |
22008 KB |
Output is correct |
35 |
Correct |
155 ms |
16544 KB |
Output is correct |
36 |
Correct |
300 ms |
29108 KB |
Output is correct |
37 |
Correct |
33 ms |
14072 KB |
Output is correct |
38 |
Correct |
333 ms |
31592 KB |
Output is correct |
39 |
Correct |
326 ms |
31592 KB |
Output is correct |
40 |
Correct |
354 ms |
31460 KB |
Output is correct |
41 |
Correct |
319 ms |
31588 KB |
Output is correct |
42 |
Correct |
355 ms |
31604 KB |
Output is correct |
43 |
Correct |
326 ms |
31588 KB |
Output is correct |
44 |
Correct |
329 ms |
31464 KB |
Output is correct |
45 |
Correct |
387 ms |
31592 KB |
Output is correct |
46 |
Correct |
379 ms |
31488 KB |
Output is correct |
47 |
Correct |
359 ms |
31520 KB |
Output is correct |
48 |
Correct |
125 ms |
26816 KB |
Output is correct |
49 |
Correct |
166 ms |
31780 KB |
Output is correct |
50 |
Correct |
37 ms |
12536 KB |
Output is correct |
51 |
Correct |
68 ms |
16880 KB |
Output is correct |
52 |
Correct |
28 ms |
12416 KB |
Output is correct |
53 |
Correct |
257 ms |
29788 KB |
Output is correct |
54 |
Correct |
113 ms |
17200 KB |
Output is correct |
55 |
Correct |
261 ms |
24172 KB |
Output is correct |
56 |
Correct |
157 ms |
18464 KB |
Output is correct |
57 |
Correct |
210 ms |
28336 KB |
Output is correct |
58 |
Correct |
49 ms |
17908 KB |
Output is correct |
59 |
Correct |
62 ms |
16120 KB |
Output is correct |
60 |
Correct |
156 ms |
28940 KB |
Output is correct |
61 |
Correct |
172 ms |
29820 KB |
Output is correct |
62 |
Correct |
103 ms |
25328 KB |
Output is correct |
63 |
Correct |
66 ms |
21240 KB |
Output is correct |
64 |
Correct |
75 ms |
23284 KB |
Output is correct |
65 |
Correct |
130 ms |
33008 KB |
Output is correct |
66 |
Correct |
60 ms |
13688 KB |
Output is correct |
67 |
Correct |
141 ms |
25332 KB |
Output is correct |
68 |
Correct |
158 ms |
33548 KB |
Output is correct |
69 |
Correct |
35 ms |
9584 KB |
Output is correct |
70 |
Correct |
15 ms |
7680 KB |
Output is correct |
71 |
Correct |
61 ms |
18932 KB |
Output is correct |
72 |
Correct |
111 ms |
28376 KB |
Output is correct |
73 |
Correct |
209 ms |
36732 KB |
Output is correct |
74 |
Correct |
203 ms |
33268 KB |
Output is correct |
75 |
Correct |
217 ms |
36668 KB |
Output is correct |
76 |
Correct |
166 ms |
35444 KB |
Output is correct |
77 |
Correct |
185 ms |
33524 KB |
Output is correct |