#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e8;
const int N = 100002;
struct Table{
int a[2][2];
};
struct Sollution{
vector<Table> tree;
void build(int L, int R, int V)
{
if (L + 1 == R)
{
tree[V].a[0][0] = 0;
tree[V].a[0][1] = INF;
tree[V].a[1][0] = INF;
tree[V].a[1][1] = 0;
return;
}
tree[V].a[0][0] = 0;
tree[V].a[1][0] = 1;
tree[V].a[0][1] = 1;
tree[V].a[1][1] = 0;
int M = (L + R) / 2;
build(L, M, 2 * V + 1);
build(M, R, 2 * V + 2);
}
Sollution(int _n)
{
tree.resize(4 * _n);
build(0, _n, 0);
}
void __Change(int pos, int cost0, int cost1, int L, int R, int V)
{
if (L + 1 == R)
{
tree[V].a[0][0] += cost0;
tree[V].a[1][1] += cost1;
return;
}
int M = (L + R) / 2;
if (pos < M) __Change(pos, cost0, cost1, L, M, 2 * V + 1);
else __Change(pos, cost0, cost1, M, R, 2 * V + 2);
for (int le = 0; le < 2; le++)
{
for (int ri = 0; ri < 2; ri++)
{
tree[V].a[le][ri] = min(min(tree[2 * V + 1].a[le][0] + tree[2 * V + 2].a[0][ri],
tree[2 * V + 1].a[le][1] + tree[2 * V + 2].a[1][ri]),
min(tree[2 * V + 1].a[le][0] + tree[2 * V + 2].a[1][ri] + 1,
tree[2 * V + 1].a[le][1] + tree[2 * V + 2].a[0][ri] + 1));
}
}
}
void Change(int pos, int cost0, int cost1)
{
__Change(pos, cost0, cost1, 0, tree.size() / 4, 0);
}
Table Cost()
{
return tree[0];
}
};
ll n;
vector<int> graph[N];
ll x[N];
ll papa[N];
ll sz[N];
int main_child[N];
vector<int> other_child[N];
vector<vector<int> > route;
vector<Sollution> hld;
int route_pos[N];
int route_id[N];
void dfs1(int v, int p)
{
papa[v] = p;
sz[v] = 1;
for (auto u : graph[v]) if (u != p)
{
dfs1(u, v);
sz[v] += sz[u];
}
}
void dfs2(int v, int p)
{
int mxsz = 0, id = -1;
for (auto u : graph[v]) if (u != p)
{
if (sz[u] > mxsz)
{
mxsz = sz[u];
id = u;
}
}
main_child[v] = id;
for (auto u : graph[v]) if (u != p)
{
dfs2(u, v);
if (u != id) other_child[v].push_back(u);
}
}
int answer()
{
int x = INF;
Table T = hld[0].Cost();
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
x = min(x, T.a[i][j]);
}
}
return x;
}
void initialize(int n0, vector<int> a, vector<int> b)
{
n = n0;
for (int i = 0; i < n - 1; i++)
{
a[i]--, b[i]--;
graph[a[i]].push_back(b[i]);
graph[b[i]].push_back(a[i]);
}
for (int i = 0; i < n; i++)
{
x[i] = -1;
}
dfs1(0, 0);
dfs2(0, 0);
for (int i = 0; i < n; i++)
{
route_id[i] = -1;
route_pos[i] = -1;
}
for (int i = 0; i < n; i++)
{
if (route_id[i] == -1)
{
vector<int> r;
int x = i;
while (x != -1)
{
r.push_back(x);
x = main_child[x];
}
for (int j = 0; j < r.size(); j++)
{
route_id[r[j]] = route.size();
route_pos[r[j]] = j;
}
route.push_back(r);
hld.push_back(Sollution(r.size()));
}
}
}
void Change(int v, int cost0, int cost1)
{
int x = route_id[v];
int y = route_pos[v];
Table oldT = hld[x].Cost();
int oldminc0 = min(oldT.a[0][0], oldT.a[0][1]);
int oldminc1 = min(oldT.a[1][0], oldT.a[1][1]);
int oldd0 = min(oldminc0, oldminc1 + 1);
int oldd1 = min(oldminc0 + 1, oldminc1);
hld[x].Change(y, cost0, cost1);
Table T = hld[x].Cost();
int minc0 = min(T.a[0][0], T.a[0][1]);
int minc1 = min(T.a[1][0], T.a[1][1]);
int d0 = min(minc0, minc1 + 1);
int d1 = min(minc0 + 1, minc1);
v = route[x][0];
if (v == 0) return;
v = papa[v];
Change(v, d0 - oldd0, d1 - oldd1);
}
int cat(int v)
{
v--;
x[v] = 0;
Change(v, 0, INF);
return answer();
}
int dog(int v)
{
v--;
x[v] = 1;
Change(v, INF, 0);
return answer();
}
int neighbor(int v)
{
v--;
if (x[v] == 0) Change(v, 0, -INF);
if (x[v] == 1) Change(v, -INF, 0);
x[v] = -1;
return answer();
}
#ifdef LOCAL
int main()
{
int n;
cin >> n;
vector<int> a(n - 1), b(n - 1);
for (int i = 0; i < n - 1; i++)
{
cin >> a[i] >> b[i];
}
initialize(n, a, b);
int q;
cin >> q;
while (q--)
{
int t, v;
cin >> t >> v;
if (t == 1) cout << cat(v) << endl;
if (t == 2) cout << dog(v) << endl;
if (t == 3) cout << neighbor(v) << endl;
}
}
#endif // LOCAL
Compilation message
catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:159:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < r.size(); j++)
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
19 |
Correct |
4 ms |
5248 KB |
Output is correct |
20 |
Correct |
3 ms |
5120 KB |
Output is correct |
21 |
Correct |
3 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5248 KB |
Output is correct |
24 |
Correct |
4 ms |
5248 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
3 ms |
5120 KB |
Output is correct |
28 |
Correct |
3 ms |
5248 KB |
Output is correct |
29 |
Correct |
6 ms |
5248 KB |
Output is correct |
30 |
Correct |
3 ms |
5120 KB |
Output is correct |
31 |
Correct |
3 ms |
5248 KB |
Output is correct |
32 |
Correct |
3 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
19 |
Correct |
4 ms |
5248 KB |
Output is correct |
20 |
Correct |
3 ms |
5120 KB |
Output is correct |
21 |
Correct |
3 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5248 KB |
Output is correct |
24 |
Correct |
4 ms |
5248 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
3 ms |
5120 KB |
Output is correct |
28 |
Correct |
3 ms |
5248 KB |
Output is correct |
29 |
Correct |
6 ms |
5248 KB |
Output is correct |
30 |
Correct |
3 ms |
5120 KB |
Output is correct |
31 |
Correct |
3 ms |
5248 KB |
Output is correct |
32 |
Correct |
3 ms |
5120 KB |
Output is correct |
33 |
Correct |
151 ms |
18776 KB |
Output is correct |
34 |
Correct |
90 ms |
21600 KB |
Output is correct |
35 |
Correct |
133 ms |
14432 KB |
Output is correct |
36 |
Correct |
338 ms |
28728 KB |
Output is correct |
37 |
Correct |
32 ms |
12912 KB |
Output is correct |
38 |
Correct |
319 ms |
31436 KB |
Output is correct |
39 |
Correct |
287 ms |
31480 KB |
Output is correct |
40 |
Correct |
294 ms |
31436 KB |
Output is correct |
41 |
Correct |
293 ms |
31696 KB |
Output is correct |
42 |
Correct |
310 ms |
31748 KB |
Output is correct |
43 |
Correct |
320 ms |
31436 KB |
Output is correct |
44 |
Correct |
305 ms |
31308 KB |
Output is correct |
45 |
Correct |
338 ms |
31368 KB |
Output is correct |
46 |
Correct |
286 ms |
31436 KB |
Output is correct |
47 |
Correct |
290 ms |
31392 KB |
Output is correct |
48 |
Correct |
101 ms |
23776 KB |
Output is correct |
49 |
Correct |
127 ms |
28220 KB |
Output is correct |
50 |
Correct |
32 ms |
9848 KB |
Output is correct |
51 |
Correct |
46 ms |
14444 KB |
Output is correct |
52 |
Correct |
22 ms |
9720 KB |
Output is correct |
53 |
Correct |
162 ms |
30688 KB |
Output is correct |
54 |
Correct |
90 ms |
15904 KB |
Output is correct |
55 |
Correct |
213 ms |
22996 KB |
Output is correct |
56 |
Correct |
127 ms |
16976 KB |
Output is correct |
57 |
Correct |
199 ms |
28512 KB |
Output is correct |
58 |
Correct |
37 ms |
15460 KB |
Output is correct |
59 |
Correct |
47 ms |
13160 KB |
Output is correct |
60 |
Correct |
111 ms |
25432 KB |
Output is correct |
61 |
Correct |
121 ms |
26328 KB |
Output is correct |
62 |
Correct |
79 ms |
23392 KB |
Output is correct |
63 |
Correct |
62 ms |
26136 KB |
Output is correct |
64 |
Correct |
71 ms |
28212 KB |
Output is correct |
65 |
Correct |
113 ms |
46688 KB |
Output is correct |
66 |
Correct |
59 ms |
11256 KB |
Output is correct |
67 |
Correct |
89 ms |
32560 KB |
Output is correct |
68 |
Correct |
158 ms |
46592 KB |
Output is correct |
69 |
Correct |
29 ms |
6784 KB |
Output is correct |
70 |
Correct |
10 ms |
5248 KB |
Output is correct |
71 |
Correct |
59 ms |
15072 KB |
Output is correct |
72 |
Correct |
100 ms |
25056 KB |
Output is correct |
73 |
Correct |
171 ms |
30324 KB |
Output is correct |
74 |
Correct |
211 ms |
64684 KB |
Output is correct |
75 |
Correct |
153 ms |
31476 KB |
Output is correct |
76 |
Correct |
156 ms |
35764 KB |
Output is correct |
77 |
Correct |
204 ms |
54292 KB |
Output is correct |