#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5 + 5, mod = 1e9 + 7, inf = 2e5;
int n, m;
vector<int> g[nmax], path[nmax];
int sz[nmax], heavy[nmax], depth[nmax], top[nmax], parent[nmax], pos[nmax], crtpos, bottom[nmax];
struct T {
int cc, cd, dc, dd;
};
T aint[nmax * 4];
T combine(T x, T y) {
T ans = {inf, inf, inf, inf};
ans.cc = min({x.cc + y.cc, x.cc + y.dc + 1, x.cd + y.dc, x.cd + y.cc + 1});
ans.cd = min({x.cc + y.cd, x.cc + y.dd + 1, x.cd + y.dd, x.cd + y.cd + 1});
ans.dd = min({x.dd + y.dd, x.dd + y.cd + 1, x.dc + y.cd, x.dc + y.dd + 1});
ans.dc = min({x.dd + y.dc, x.dd + y.cc + 1, x.dc + y.cc, x.dc + y.dc + 1});
return ans;
}
void build(int node, int left, int right) {
if (left == right) {
aint[node].cc = aint[node].dd = 0;
aint[node].cd = aint[node].dc = inf;
return;
}
int mid = (left + right) / 2;
build(node * 2, left, mid);
build(node * 2 + 1, mid + 1, right);
// aint[node] = combine(aint[node * 2], aint[node * 2 + 1]);
aint[node].cc = aint[node].dd = 0;
aint[node].cd = aint[node].dc = 0;
}
void update(int node, int left, int right, int x, pair<int, int> y) {
if (left == right) {
aint[node].cc += y.first;
aint[node].dd += y.second;
return;
}
int mid = (left + right) / 2;
if (x <= mid) update(node * 2, left, mid, x, y);
else update(node * 2 + 1, mid + 1, right, x, y);
aint[node] = combine(aint[node * 2], aint[node * 2 + 1]);
}
void update(int x, pair<int, int> y) {
update(1, 1, n, x, y);
}
T query(int node, int left, int right, int x, int y) {
if (x <= left && right <= y) {
return aint[node];
}
int mid = (left + right) / 2;
if (x <= mid && y > mid) {
return combine(query(node * 2, left, mid, x, y), query(node * 2 + 1, mid + 1, right, x, y));
}
if (x <= mid) {
return query(node * 2, left, mid, x, y);
}
if (y > mid) {
return query(node * 2 + 1, mid + 1, right, x, y);
}
}
T query(int x, int y) {
return query(1, 1, n, x, y);
}
void dfs1(int x, int par = 0) {
sz[x] = 1;
parent[x] = par;
depth[x] = depth[par] + 1;
int mx = 0;
for (auto y: g[x]) {
if (y == par) continue;
dfs1(y, x);
sz[x] += sz[y];
if (sz[y] > mx) {
heavy[x] = y;
mx = sz[y];
}
}
}
void decompose(int x, int crttop) {
path[crttop].push_back(x);
top[x] = crttop;
pos[x] = ++crtpos;
if (heavy[x]) {
decompose(heavy[x], crttop);
}
for (auto y: g[x]) {
if (y == parent[x]) continue;
if (heavy[x] != y) {
decompose(y, y);
}
}
}
pair<int, int> getval(int x) {
pair<int, int> ans; // first - cc ; second - dd
for (auto y: g[x]) {
if (y == parent[x] || y == heavy[x]) continue;
T crt = query(pos[top[y]], pos[bottom[y]]);
ans.first += min({crt.cc, crt.cd, crt.dc + 1, crt.dd + 1});
ans.second += min({crt.cc + 1, crt.cd + 1, crt.dc, crt.dd});
}
T tmp = query(pos[x], pos[x]);
if (tmp.cc == inf) ans.first = inf;
if (tmp.dd == inf) ans.second = inf;
return ans;
}
void dfs(int x) {
for (auto y: g[x]) {
if (y == parent[x]) continue;
dfs(y);
pair<int, int> ans;
T crt = query(pos[top[y]], pos[bottom[y]]);
ans.first += min({crt.cc, crt.cd, crt.dc + 1, crt.dd + 1});
ans.second += min({crt.cc + 1, crt.cd + 1, crt.dc, crt.dd});
update(pos[x], ans);
}
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
n = N; m = A.size();
for (int i = 0; i < m; i++) {
g[A[i]].push_back(B[i]);
g[B[i]].push_back(A[i]);
}
dfs1(1);
decompose(1, 1);
build(1, 1, n);
for (int i = 1; i <= n; i++) {
for (auto x: path[i]) {
bottom[x] = path[i].back();
}
}
dfs(1);
}
int animal[nmax];
int cat(int x) {
animal[x] = 1;
vector<int> v;
int cx = x;
while (x) {
v.push_back(x);
x = parent[top[x]];
}
for (int i = (int)v.size() - 2; i >= 0; i--) {
x = v[i];
T crtpos = query(pos[top[x]], pos[bottom[x]]);
pair<int, int> tmp;
tmp.first = -min({crtpos.cc, crtpos.cd, crtpos.dc + 1, crtpos.dd + 1});
tmp.second = -min({crtpos.cc + 1, crtpos.cd + 1, crtpos.dc, crtpos.dd});
update(pos[v[i + 1]], tmp);
}
x = cx;
update(pos[x], {0, inf});
for (int i = 0; i + 1 < v.size(); i++) {
x = v[i];
T crtpos = query(pos[top[x]], pos[bottom[x]]);
pair<int, int> tmp;
tmp.first = min({crtpos.cc, crtpos.cd, crtpos.dc + 1, crtpos.dd + 1});
tmp.second = min({crtpos.cc + 1, crtpos.cd + 1, crtpos.dc, crtpos.dd});
update(pos[v[i + 1]], tmp);
}
T ans = query(pos[top[1]], pos[bottom[1]]);
return min({ans.cc, ans.cd, ans.dc, ans.dd});
}
int dog(int x) {
// if (x == 9) {
// int node = 14;
// while (node != 0) {
// T tmp = query(pos[node], pos[node]);
// cout << node << ": " << tmp.cc << ' ' << tmp.dd << '\n';
// node = parent[node];
// }
// cout << '\n';
// }
animal[x] = 2;
vector<int> v;
int cx = x;
while (x) {
v.push_back(x);
x = parent[top[x]];
}
for (int i = (int)v.size() - 2; i >= 0; i--) {
x = v[i];
T crtpos = query(pos[top[x]], pos[bottom[x]]);
pair<int, int> tmp;
tmp.first = -min({crtpos.cc, crtpos.cd, crtpos.dc + 1, crtpos.dd + 1});
tmp.second = -min({crtpos.cc + 1, crtpos.cd + 1, crtpos.dc, crtpos.dd});
update(pos[v[i + 1]], tmp);
}
x = cx;
T aux = query(pos[x], pos[x]);
update(pos[x], {inf, 0});
aux = query(pos[x], pos[x]);
// x = parent[top[x]];
for (int i = 0; i + 1 < v.size(); i++) {
x = v[i];
T crtpos = query(pos[top[x]], pos[bottom[x]]);
pair<int, int> tmp;
tmp.first = min({crtpos.cc, crtpos.cd, crtpos.dc + 1, crtpos.dd + 1});
tmp.second = min({crtpos.cc + 1, crtpos.cd + 1, crtpos.dc, crtpos.dd});
update(pos[v[i + 1]], tmp);
}
T ans = query(pos[top[1]], pos[bottom[1]]);
// int node = 8;
// while (node != 0) {
// aux = query(pos[node], pos[node]);
// cout << node << ": " << aux.cc << ' ' << aux.dd << '\n';
// node = parent[node];
// }
return min({ans.cc, ans.cd, ans.dc, ans.dd});
}
int neighbor(int x) {
// if (x == 6 || x == 14) {
// int node = 14;
// while (node != 0) {
// T tmp = query(pos[node], pos[node]);
// cout << node << ": " << tmp.cc << ' ' << tmp.dd << '\n';
// node = parent[node];
// }
// cout << '\n';
// }
if (x == 6) {
int auix = 0;
}
vector<int> v;
int cx = x;
while (x) {
v.push_back(x);
x = parent[top[x]];
}
for (int i = (int)v.size() - 2; i >= 0; i--) {
x = v[i];
T crtpos = query(pos[top[x]], pos[bottom[x]]);
pair<int, int> tmp;
tmp.first = -min({crtpos.cc, crtpos.cd, crtpos.dc + 1, crtpos.dd + 1});
tmp.second = -min({crtpos.cc + 1, crtpos.cd + 1, crtpos.dc, crtpos.dd});
update(pos[v[i + 1]], tmp);
}
x = cx;
if (animal[x] == 1) {
update(pos[x], {0, -inf});
} else if (animal[x] == 2) {
update(pos[x], {-inf, 0});
}
// x = parent[top[x]];
for (int i = 0; i + 1 < v.size(); i++) {
x = v[i];
T crtpos = query(pos[top[x]], pos[bottom[x]]);
pair<int, int> tmp;
tmp.first = min({crtpos.cc, crtpos.cd, crtpos.dc + 1, crtpos.dd + 1});
tmp.second = min({crtpos.cc + 1, crtpos.cd + 1, crtpos.dc, crtpos.dd});
update(pos[v[i + 1]], tmp);
}
T ans = query(pos[top[1]], pos[bottom[1]]);
return min({ans.cc, ans.cd, ans.dc, ans.dd});
}
Compilation message
catdog.cpp: In function 'int cat(int)':
catdog.cpp:173:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
173 | for (int i = 0; i + 1 < v.size(); i++) {
| ~~~~~~^~~~~~~~~~
catdog.cpp: In function 'int dog(int)':
catdog.cpp:216:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
216 | for (int i = 0; i + 1 < v.size(); i++) {
| ~~~~~~^~~~~~~~~~
catdog.cpp: In function 'int neighbor(int)':
catdog.cpp:248:13: warning: unused variable 'auix' [-Wunused-variable]
248 | int auix = 0;
| ^~~~
catdog.cpp:272:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
272 | for (int i = 0; i + 1 < v.size(); i++) {
| ~~~~~~^~~~~~~~~~
catdog.cpp: In function 'T query(int, int, int, int, int)':
catdog.cpp:71:1: warning: control reaches end of non-void function [-Wreturn-type]
71 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10072 KB |
Output is correct |
2 |
Correct |
3 ms |
10076 KB |
Output is correct |
3 |
Correct |
3 ms |
10156 KB |
Output is correct |
4 |
Correct |
2 ms |
10072 KB |
Output is correct |
5 |
Correct |
2 ms |
10076 KB |
Output is correct |
6 |
Correct |
2 ms |
10076 KB |
Output is correct |
7 |
Correct |
2 ms |
10076 KB |
Output is correct |
8 |
Correct |
3 ms |
10076 KB |
Output is correct |
9 |
Correct |
2 ms |
10076 KB |
Output is correct |
10 |
Correct |
3 ms |
10076 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
3 ms |
10076 KB |
Output is correct |
13 |
Correct |
2 ms |
10076 KB |
Output is correct |
14 |
Correct |
2 ms |
10076 KB |
Output is correct |
15 |
Correct |
2 ms |
10076 KB |
Output is correct |
16 |
Correct |
2 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10072 KB |
Output is correct |
2 |
Correct |
3 ms |
10076 KB |
Output is correct |
3 |
Correct |
3 ms |
10156 KB |
Output is correct |
4 |
Correct |
2 ms |
10072 KB |
Output is correct |
5 |
Correct |
2 ms |
10076 KB |
Output is correct |
6 |
Correct |
2 ms |
10076 KB |
Output is correct |
7 |
Correct |
2 ms |
10076 KB |
Output is correct |
8 |
Correct |
3 ms |
10076 KB |
Output is correct |
9 |
Correct |
2 ms |
10076 KB |
Output is correct |
10 |
Correct |
3 ms |
10076 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
3 ms |
10076 KB |
Output is correct |
13 |
Correct |
2 ms |
10076 KB |
Output is correct |
14 |
Correct |
2 ms |
10076 KB |
Output is correct |
15 |
Correct |
2 ms |
10076 KB |
Output is correct |
16 |
Correct |
2 ms |
10076 KB |
Output is correct |
17 |
Correct |
3 ms |
10076 KB |
Output is correct |
18 |
Correct |
4 ms |
10076 KB |
Output is correct |
19 |
Correct |
3 ms |
10076 KB |
Output is correct |
20 |
Correct |
2 ms |
10072 KB |
Output is correct |
21 |
Correct |
3 ms |
10076 KB |
Output is correct |
22 |
Correct |
3 ms |
10076 KB |
Output is correct |
23 |
Correct |
4 ms |
10072 KB |
Output is correct |
24 |
Correct |
4 ms |
10072 KB |
Output is correct |
25 |
Correct |
3 ms |
10072 KB |
Output is correct |
26 |
Correct |
3 ms |
10076 KB |
Output is correct |
27 |
Correct |
2 ms |
10076 KB |
Output is correct |
28 |
Correct |
3 ms |
10332 KB |
Output is correct |
29 |
Correct |
4 ms |
10332 KB |
Output is correct |
30 |
Correct |
3 ms |
10076 KB |
Output is correct |
31 |
Correct |
3 ms |
10076 KB |
Output is correct |
32 |
Correct |
3 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10072 KB |
Output is correct |
2 |
Correct |
3 ms |
10076 KB |
Output is correct |
3 |
Correct |
3 ms |
10156 KB |
Output is correct |
4 |
Correct |
2 ms |
10072 KB |
Output is correct |
5 |
Correct |
2 ms |
10076 KB |
Output is correct |
6 |
Correct |
2 ms |
10076 KB |
Output is correct |
7 |
Correct |
2 ms |
10076 KB |
Output is correct |
8 |
Correct |
3 ms |
10076 KB |
Output is correct |
9 |
Correct |
2 ms |
10076 KB |
Output is correct |
10 |
Correct |
3 ms |
10076 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
3 ms |
10076 KB |
Output is correct |
13 |
Correct |
2 ms |
10076 KB |
Output is correct |
14 |
Correct |
2 ms |
10076 KB |
Output is correct |
15 |
Correct |
2 ms |
10076 KB |
Output is correct |
16 |
Correct |
2 ms |
10076 KB |
Output is correct |
17 |
Correct |
3 ms |
10076 KB |
Output is correct |
18 |
Correct |
4 ms |
10076 KB |
Output is correct |
19 |
Correct |
3 ms |
10076 KB |
Output is correct |
20 |
Correct |
2 ms |
10072 KB |
Output is correct |
21 |
Correct |
3 ms |
10076 KB |
Output is correct |
22 |
Correct |
3 ms |
10076 KB |
Output is correct |
23 |
Correct |
4 ms |
10072 KB |
Output is correct |
24 |
Correct |
4 ms |
10072 KB |
Output is correct |
25 |
Correct |
3 ms |
10072 KB |
Output is correct |
26 |
Correct |
3 ms |
10076 KB |
Output is correct |
27 |
Correct |
2 ms |
10076 KB |
Output is correct |
28 |
Correct |
3 ms |
10332 KB |
Output is correct |
29 |
Correct |
4 ms |
10332 KB |
Output is correct |
30 |
Correct |
3 ms |
10076 KB |
Output is correct |
31 |
Correct |
3 ms |
10076 KB |
Output is correct |
32 |
Correct |
3 ms |
10076 KB |
Output is correct |
33 |
Correct |
260 ms |
16300 KB |
Output is correct |
34 |
Correct |
105 ms |
16212 KB |
Output is correct |
35 |
Correct |
227 ms |
15452 KB |
Output is correct |
36 |
Correct |
393 ms |
21144 KB |
Output is correct |
37 |
Correct |
22 ms |
12124 KB |
Output is correct |
38 |
Correct |
451 ms |
21552 KB |
Output is correct |
39 |
Correct |
428 ms |
21572 KB |
Output is correct |
40 |
Correct |
422 ms |
21608 KB |
Output is correct |
41 |
Correct |
420 ms |
21612 KB |
Output is correct |
42 |
Correct |
400 ms |
21620 KB |
Output is correct |
43 |
Correct |
413 ms |
21572 KB |
Output is correct |
44 |
Correct |
403 ms |
21824 KB |
Output is correct |
45 |
Correct |
405 ms |
21632 KB |
Output is correct |
46 |
Correct |
416 ms |
21572 KB |
Output is correct |
47 |
Correct |
433 ms |
21600 KB |
Output is correct |
48 |
Correct |
134 ms |
20888 KB |
Output is correct |
49 |
Correct |
137 ms |
24060 KB |
Output is correct |
50 |
Correct |
45 ms |
12428 KB |
Output is correct |
51 |
Correct |
55 ms |
16084 KB |
Output is correct |
52 |
Correct |
28 ms |
12220 KB |
Output is correct |
53 |
Correct |
215 ms |
22100 KB |
Output is correct |
54 |
Correct |
137 ms |
15896 KB |
Output is correct |
55 |
Correct |
360 ms |
21320 KB |
Output is correct |
56 |
Correct |
212 ms |
16892 KB |
Output is correct |
57 |
Correct |
312 ms |
21904 KB |
Output is correct |
58 |
Correct |
29 ms |
16088 KB |
Output is correct |
59 |
Correct |
46 ms |
13916 KB |
Output is correct |
60 |
Correct |
112 ms |
23120 KB |
Output is correct |
61 |
Correct |
115 ms |
23452 KB |
Output is correct |
62 |
Correct |
89 ms |
21672 KB |
Output is correct |
63 |
Correct |
48 ms |
18524 KB |
Output is correct |
64 |
Correct |
53 ms |
20184 KB |
Output is correct |
65 |
Correct |
79 ms |
26320 KB |
Output is correct |
66 |
Correct |
50 ms |
13652 KB |
Output is correct |
67 |
Correct |
76 ms |
23068 KB |
Output is correct |
68 |
Correct |
125 ms |
26600 KB |
Output is correct |
69 |
Correct |
22 ms |
11696 KB |
Output is correct |
70 |
Correct |
6 ms |
10332 KB |
Output is correct |
71 |
Correct |
47 ms |
18388 KB |
Output is correct |
72 |
Correct |
79 ms |
25288 KB |
Output is correct |
73 |
Correct |
192 ms |
29904 KB |
Output is correct |
74 |
Correct |
218 ms |
26196 KB |
Output is correct |
75 |
Correct |
136 ms |
29648 KB |
Output is correct |
76 |
Correct |
146 ms |
28612 KB |
Output is correct |
77 |
Correct |
214 ms |
26808 KB |
Output is correct |