#include <bits/stdc++.h>
void initialize(int N, std::vector<int> A, std::vector<int> B);
int cat(int v);
int dog(int v);
int neighbor(int v);
struct segTree {
int n;
using node = std::array<std::array<int, 2>, 2>;
std::vector<node> t;
node merge(node a, node b) {
node c;
for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) c[i][j] = std::min({a[i][0] + b[0][j], a[i][1] + b[1][j]});
return c;
}
segTree() = default;
segTree(int n) {
while (__builtin_popcount(n) != 1) ++n;
this->n = n, t = std::vector<node>(n * 2);
for (auto&f: t) f[0][1] = f[1][0] = 1;
}
void modify(int i, int x, int y) {
i += n;
t[i][0][0] += x, t[i][0][1] += x;
t[i][1][0] += y, t[i][1][1] += y;
while (i /= 2) t[i] = merge(t[i * 2], t[i * 2 + 1]);
}
std::pair<int, int> get() {
int x = std::min(t[1][0][0], t[1][0][1]);
int y = std::min(t[1][1][0], t[1][1][1]);
return {std::min(x, y + 1), std::min(x + 1, y)};
}
};
segTree ok[100005];
int pos[100005], p[100005], f[100005], sz[100005], len[100005], tp[100005];
std::vector<int> g[100005];
void cfs(int v = 1, int p = -1) {
for (int i = 0; i < g[v].size(); ++i) if (g[v][i] == p) g[v].erase(g[v].begin() + i);
sz[v] = 1;
for (auto u: g[v]) if (u != p) cfs(u, v), sz[v] += sz[u], ::p[u] = v;
std::sort(g[v].begin(), g[v].end(), [](const int&v, const int&u) { return sz[v] > sz[u]; });
}
void dfs(int v = 1, int s = 1) {
f[v] = s, pos[v] = len[s]++;
for (auto u: g[v]) dfs(u, u == g[v].front() ? s : u);
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
for (int i = 0; i < A.size(); ++i) g[A[i]].push_back(B[i]), g[B[i]].push_back(A[i]);
cfs(), dfs();
for (int v = 1; v <= N; ++v) if (!pos[v]) ok[v] = len[v];
}
void modify(int v, int x, int y) {
while (v) {
auto [w0, w1] = ok[f[v]].get();
ok[f[v]].modify(pos[v], x, y);
auto [n0, n1] = ok[f[v]].get();
x = n0 - w0, y = n1 - w1, v = p[f[v]];
}
}
int query() {
auto [a, b] = ok[1].get();
return std::min(a, b);
}
int cat(int v) { return tp[v] = 1, modify(v, 1e9, 0), query(); }
int dog(int v) { return tp[v] = 2, modify(v, 0, 1e9), query(); }
int neighbor(int v) { return modify(v, -1e9 * (tp[v] == 1), -1e9 * (tp[v] == 2)), tp[v] = 0, query(); }
#ifdef __APPLE__
#include <cstdio>
#include <cassert>
#include <vector>
#include <cstdlib>
int readInt() {
int i;
if (scanf("%d", &i) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
return i;
}
int main() {
int N = readInt();
std::vector<int> A(N - 1), B(N - 1);
for (int i = 0; i < N - 1; i++) {
A[i] = readInt();
B[i] = readInt();
}
int Q;
assert(scanf("%d",&Q)==1);
std::vector<int> T(Q), V(Q);
for (int i = 0; i < Q; i++) {
T[i] = readInt();
V[i] = readInt();
}
initialize(N, A, B);
std::vector<int> res(Q);
for (int j = 0; j < Q; j++) {
if (T[j] == 1) res[j] = cat(V[j]);
else if (T[j] == 2) res[j] = dog(V[j]);
else res[j] = neighbor(V[j]);
}
for (int j = 0; j < Q; j++)
printf("%d\n", res[j]);
return 0;
}
#endif
Compilation message
catdog.cpp: In function 'void cfs(int, int)':
catdog.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 0; i < g[v].size(); ++i) if (g[v][i] == p) g[v].erase(g[v].begin() + i);
| ~~^~~~~~~~~~~~~
catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:63:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i = 0; i < A.size(); ++i) g[A[i]].push_back(B[i]), g[B[i]].push_back(A[i]);
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
3 ms |
8024 KB |
Output is correct |
8 |
Correct |
2 ms |
8024 KB |
Output is correct |
9 |
Correct |
3 ms |
8244 KB |
Output is correct |
10 |
Correct |
3 ms |
8028 KB |
Output is correct |
11 |
Correct |
3 ms |
8028 KB |
Output is correct |
12 |
Correct |
3 ms |
8028 KB |
Output is correct |
13 |
Correct |
2 ms |
8028 KB |
Output is correct |
14 |
Correct |
2 ms |
8028 KB |
Output is correct |
15 |
Correct |
3 ms |
8028 KB |
Output is correct |
16 |
Correct |
2 ms |
8024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
3 ms |
8024 KB |
Output is correct |
8 |
Correct |
2 ms |
8024 KB |
Output is correct |
9 |
Correct |
3 ms |
8244 KB |
Output is correct |
10 |
Correct |
3 ms |
8028 KB |
Output is correct |
11 |
Correct |
3 ms |
8028 KB |
Output is correct |
12 |
Correct |
3 ms |
8028 KB |
Output is correct |
13 |
Correct |
2 ms |
8028 KB |
Output is correct |
14 |
Correct |
2 ms |
8028 KB |
Output is correct |
15 |
Correct |
3 ms |
8028 KB |
Output is correct |
16 |
Correct |
2 ms |
8024 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8272 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
2 ms |
8284 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
3 ms |
8272 KB |
Output is correct |
24 |
Correct |
2 ms |
8284 KB |
Output is correct |
25 |
Correct |
3 ms |
8284 KB |
Output is correct |
26 |
Correct |
2 ms |
8284 KB |
Output is correct |
27 |
Correct |
2 ms |
8236 KB |
Output is correct |
28 |
Correct |
2 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
30 |
Correct |
3 ms |
8116 KB |
Output is correct |
31 |
Correct |
2 ms |
8284 KB |
Output is correct |
32 |
Correct |
2 ms |
8292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
3 ms |
8024 KB |
Output is correct |
8 |
Correct |
2 ms |
8024 KB |
Output is correct |
9 |
Correct |
3 ms |
8244 KB |
Output is correct |
10 |
Correct |
3 ms |
8028 KB |
Output is correct |
11 |
Correct |
3 ms |
8028 KB |
Output is correct |
12 |
Correct |
3 ms |
8028 KB |
Output is correct |
13 |
Correct |
2 ms |
8028 KB |
Output is correct |
14 |
Correct |
2 ms |
8028 KB |
Output is correct |
15 |
Correct |
3 ms |
8028 KB |
Output is correct |
16 |
Correct |
2 ms |
8024 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8272 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
2 ms |
8284 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
3 ms |
8272 KB |
Output is correct |
24 |
Correct |
2 ms |
8284 KB |
Output is correct |
25 |
Correct |
3 ms |
8284 KB |
Output is correct |
26 |
Correct |
2 ms |
8284 KB |
Output is correct |
27 |
Correct |
2 ms |
8236 KB |
Output is correct |
28 |
Correct |
2 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
30 |
Correct |
3 ms |
8116 KB |
Output is correct |
31 |
Correct |
2 ms |
8284 KB |
Output is correct |
32 |
Correct |
2 ms |
8292 KB |
Output is correct |
33 |
Correct |
68 ms |
14444 KB |
Output is correct |
34 |
Correct |
35 ms |
14940 KB |
Output is correct |
35 |
Correct |
64 ms |
13028 KB |
Output is correct |
36 |
Correct |
108 ms |
18236 KB |
Output is correct |
37 |
Correct |
13 ms |
11352 KB |
Output is correct |
38 |
Correct |
113 ms |
19600 KB |
Output is correct |
39 |
Correct |
111 ms |
19516 KB |
Output is correct |
40 |
Correct |
117 ms |
19428 KB |
Output is correct |
41 |
Correct |
127 ms |
19516 KB |
Output is correct |
42 |
Correct |
112 ms |
19520 KB |
Output is correct |
43 |
Correct |
109 ms |
19520 KB |
Output is correct |
44 |
Correct |
125 ms |
19508 KB |
Output is correct |
45 |
Correct |
111 ms |
19620 KB |
Output is correct |
46 |
Correct |
120 ms |
19548 KB |
Output is correct |
47 |
Correct |
119 ms |
19424 KB |
Output is correct |
48 |
Correct |
45 ms |
16844 KB |
Output is correct |
49 |
Correct |
59 ms |
18636 KB |
Output is correct |
50 |
Correct |
17 ms |
10588 KB |
Output is correct |
51 |
Correct |
28 ms |
12248 KB |
Output is correct |
52 |
Correct |
14 ms |
10304 KB |
Output is correct |
53 |
Correct |
74 ms |
18516 KB |
Output is correct |
54 |
Correct |
41 ms |
12812 KB |
Output is correct |
55 |
Correct |
93 ms |
16276 KB |
Output is correct |
56 |
Correct |
62 ms |
13728 KB |
Output is correct |
57 |
Correct |
84 ms |
17960 KB |
Output is correct |
58 |
Correct |
19 ms |
12764 KB |
Output is correct |
59 |
Correct |
22 ms |
12124 KB |
Output is correct |
60 |
Correct |
51 ms |
17708 KB |
Output is correct |
61 |
Correct |
52 ms |
18132 KB |
Output is correct |
62 |
Correct |
34 ms |
16060 KB |
Output is correct |
63 |
Correct |
29 ms |
15940 KB |
Output is correct |
64 |
Correct |
29 ms |
18256 KB |
Output is correct |
65 |
Correct |
52 ms |
24868 KB |
Output is correct |
66 |
Correct |
36 ms |
12588 KB |
Output is correct |
67 |
Correct |
39 ms |
19548 KB |
Output is correct |
68 |
Correct |
76 ms |
24236 KB |
Output is correct |
69 |
Correct |
18 ms |
9820 KB |
Output is correct |
70 |
Correct |
8 ms |
8540 KB |
Output is correct |
71 |
Correct |
34 ms |
16220 KB |
Output is correct |
72 |
Correct |
51 ms |
22868 KB |
Output is correct |
73 |
Correct |
103 ms |
27476 KB |
Output is correct |
74 |
Correct |
106 ms |
23768 KB |
Output is correct |
75 |
Correct |
84 ms |
27480 KB |
Output is correct |
76 |
Correct |
88 ms |
27104 KB |
Output is correct |
77 |
Correct |
107 ms |
24144 KB |
Output is correct |