#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
const int N = (int) 1e5 + 7;
const int INF = (int) 1e8;
int n, sub[N], child[N], color[N], par[N], y, dp1[N], dp2[N], cost1[N], cost2[N], theindex[N], curind, node[N], l1[N], l2[N];
vector<int> members[N];
bool isTheChild[N];
vector<int> g[N];
struct D {
bool nmc;
int cost[2][2];
};
D operator + (D a, D b) {
if (a.nmc) {
return b;
}
if (b.nmc) {
return a;
}
D c;
c.nmc = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
c.cost[i][j] = INF;
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
c.cost[i][l] = min(c.cost[i][l], a.cost[i][j] + b.cost[k][l] + (j != k));
}
}
}
}
return c;
}
D t[4 * N];
D wnmc;
void upd(int v, int tl, int tr, int i) {
if (tr < i || i < tl) {
return;
}
if (tl == tr) {
vector<int> K = {cost1[node[i]] + dp1[node[i]], cost2[node[i]] + dp2[node[i]]};
t[v].cost[0][1] = t[v].cost[1][0] = INF;
t[v].cost[0][0] = K[0];
t[v].cost[1][1] = K[1];
return;
}
int tm = (tl + tr) / 2;
upd(2 * v, tl, tm, i);
upd(2 * v + 1, tm + 1, tr, i);
t[v] = t[2 * v] + t[2 * v + 1];
}
D get(int v, int tl, int tr, int l, int r) {
if (tr < l || r < tl) {
return wnmc;
}
if (l <= tl && tr <= r) {
return t[v];
}
int tm = (tl + tr) / 2;
D x = get(2 * v, tl, tm, l, r);
D y = get(2 * v + 1, tm + 1, tr, l, r);
D z = x + y;
return x + y;
}
void buildtr(int v = 1, int tl = 0, int tr = n) {
if (tl == tr) {
t[v].cost[0][1] = t[v].cost[1][0] = INF;
t[v].cost[0][0] = t[v].cost[1][1] = 0;
} else {
int tm = (tl + tr) / 2;
buildtr(2 * v, tl, tm);
buildtr(2 * v + 1, tm + 1, tr);
t[v] = t[2 * v] + t[2 * v + 1];
}
}
int getdp1(int l, int r) {
D sol = get(1, 0, n, l, r);
return min(sol.cost[0][0], sol.cost[0][1]);
}
int getdp2(int l, int r) {
D sol = get(1, 0, n, l, r);
return min(sol.cost[1][0], sol.cost[1][1]);
}
void build(int a, int p = 0) {
vector<int> kids;
sub[a] = 1;
par[a] = p;
for (auto &b : g[a]) {
if (b != p) {
kids.push_back(b);
build(b, a);
sub[a] += sub[b];
if (sub[b] > sub[child[a]]) {
child[a] = b;
}
}
}
isTheChild[child[a]] = 1;
g[a] = kids;
}
void compute(int a) {
if (isTheChild[a]) {
color[a] = color[par[a]];
} else {
color[a] = ++y;
}
theindex[a] = ++curind;
node[theindex[a]] = a;
members[color[a]].push_back(a);
if (child[a]) {
compute(child[a]);
for (auto &b : g[a]) {
if (b != child[a]) {
compute(b);
}
}
}
}
void updVertex(int v) {
int p = par[members[color[v]][0]];
dp1[p] -= l1[color[v]];
dp2[p] -= l2[color[v]];
upd(1, 0, n, theindex[v]);
l1[color[v]] = getdp1(theindex[members[color[v]][0]], theindex[members[color[v]].back()]);
l2[color[v]] = getdp2(theindex[members[color[v]][0]], theindex[members[color[v]].back()]);
l1[color[v]] = min(l1[color[v]], l2[color[v]] + 1);
l2[color[v]] = min(l2[color[v]], l1[color[v]] + 1);
dp1[p] += l1[color[v]];
dp2[p] += l2[color[v]];
}
void update(int node) {
while (node) {
for (int j = 1; j < (int) members[color[node]].size(); j++) {
assert(theindex[members[color[node]][j - 1]] == theindex[members[color[node]][j]] - 1);
}
int p = par[members[color[node]][0]];
int i1 = getdp1(theindex[members[color[node]][0]], theindex[members[color[node]].back()]), i2 = getdp2(theindex[members[color[node]][0]], theindex[members[color[node]].back()]);
updVertex(node);
node = p;
}
}
void initialize(int nn, std::vector<int> edgesA, std::vector<int> edgesB) {
wnmc.nmc = 1;
n = nn;
buildtr();
for (int i = 0; i < n - 1; i++) {
int a = edgesA[i];
int b = edgesB[i];
g[a].push_back(b);
g[b].push_back(a);
}
build(1);
compute(1);
}
void changeCost(int v, int c1, int c2) {
dp1[v] -= cost1[v];
dp2[v] -= cost2[v];
cost1[v] = c1;
cost2[v] = c2;
dp1[v] += cost1[v];
dp2[v] += cost2[v];
updVertex(v);
update(v);
}
int eval() {
return min(dp1[0], dp2[0]);
}
int cat(int v) {
changeCost(v, 0, INF);
return eval();
}
int dog(int v) {
changeCost(v, INF, 0);
return eval();
}
int neighbor(int v) {
changeCost(v, 0, 0);
return eval();
}
Compilation message
catdog.cpp: In function 'D get(int, int, int, int, int)':
catdog.cpp:74:5: warning: variable 'z' set but not used [-Wunused-but-set-variable]
74 | D z = x + y;
| ^
catdog.cpp: In function 'void update(int)':
catdog.cpp:166:9: warning: unused variable 'i1' [-Wunused-variable]
166 | int i1 = getdp1(theindex[members[color[node]][0]], theindex[members[color[node]].back()]), i2 = getdp2(theindex[members[color[node]][0]], theindex[members[color[node]].back()]);
| ^~
catdog.cpp:166:96: warning: unused variable 'i2' [-Wunused-variable]
166 | int i1 = getdp1(theindex[members[color[node]][0]], theindex[members[color[node]].back()]), i2 = getdp2(theindex[members[color[node]][0]], theindex[members[color[node]].back()]);
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5008 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
5008 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5008 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
5008 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5012 KB |
Output is correct |
17 |
Correct |
8 ms |
5196 KB |
Output is correct |
18 |
Correct |
7 ms |
5196 KB |
Output is correct |
19 |
Correct |
8 ms |
5196 KB |
Output is correct |
20 |
Correct |
4 ms |
5068 KB |
Output is correct |
21 |
Correct |
5 ms |
5016 KB |
Output is correct |
22 |
Correct |
5 ms |
5012 KB |
Output is correct |
23 |
Correct |
9 ms |
5148 KB |
Output is correct |
24 |
Correct |
8 ms |
5196 KB |
Output is correct |
25 |
Correct |
7 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5060 KB |
Output is correct |
28 |
Correct |
4 ms |
5140 KB |
Output is correct |
29 |
Correct |
8 ms |
5196 KB |
Output is correct |
30 |
Correct |
4 ms |
5068 KB |
Output is correct |
31 |
Correct |
3 ms |
5196 KB |
Output is correct |
32 |
Correct |
5 ms |
5016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5008 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
5008 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5012 KB |
Output is correct |
17 |
Correct |
8 ms |
5196 KB |
Output is correct |
18 |
Correct |
7 ms |
5196 KB |
Output is correct |
19 |
Correct |
8 ms |
5196 KB |
Output is correct |
20 |
Correct |
4 ms |
5068 KB |
Output is correct |
21 |
Correct |
5 ms |
5016 KB |
Output is correct |
22 |
Correct |
5 ms |
5012 KB |
Output is correct |
23 |
Correct |
9 ms |
5148 KB |
Output is correct |
24 |
Correct |
8 ms |
5196 KB |
Output is correct |
25 |
Correct |
7 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5060 KB |
Output is correct |
28 |
Correct |
4 ms |
5140 KB |
Output is correct |
29 |
Correct |
8 ms |
5196 KB |
Output is correct |
30 |
Correct |
4 ms |
5068 KB |
Output is correct |
31 |
Correct |
3 ms |
5196 KB |
Output is correct |
32 |
Correct |
5 ms |
5016 KB |
Output is correct |
33 |
Correct |
1182 ms |
14752 KB |
Output is correct |
34 |
Correct |
288 ms |
14992 KB |
Output is correct |
35 |
Correct |
1037 ms |
12548 KB |
Output is correct |
36 |
Correct |
1661 ms |
22280 KB |
Output is correct |
37 |
Correct |
22 ms |
9784 KB |
Output is correct |
38 |
Correct |
1798 ms |
23480 KB |
Output is correct |
39 |
Correct |
1641 ms |
23496 KB |
Output is correct |
40 |
Correct |
1661 ms |
23556 KB |
Output is correct |
41 |
Correct |
1788 ms |
23488 KB |
Output is correct |
42 |
Correct |
1645 ms |
23588 KB |
Output is correct |
43 |
Correct |
1705 ms |
23496 KB |
Output is correct |
44 |
Correct |
1689 ms |
23500 KB |
Output is correct |
45 |
Correct |
1894 ms |
23520 KB |
Output is correct |
46 |
Correct |
1784 ms |
23496 KB |
Output is correct |
47 |
Correct |
1733 ms |
23488 KB |
Output is correct |
48 |
Correct |
436 ms |
21048 KB |
Output is correct |
49 |
Correct |
457 ms |
23560 KB |
Output is correct |
50 |
Correct |
171 ms |
9320 KB |
Output is correct |
51 |
Correct |
189 ms |
12896 KB |
Output is correct |
52 |
Correct |
129 ms |
9016 KB |
Output is correct |
53 |
Correct |
758 ms |
21780 KB |
Output is correct |
54 |
Correct |
558 ms |
12972 KB |
Output is correct |
55 |
Correct |
1498 ms |
19824 KB |
Output is correct |
56 |
Correct |
951 ms |
13892 KB |
Output is correct |
57 |
Correct |
1221 ms |
21444 KB |
Output is correct |
58 |
Correct |
56 ms |
13116 KB |
Output is correct |
59 |
Correct |
170 ms |
11212 KB |
Output is correct |
60 |
Correct |
433 ms |
22212 KB |
Output is correct |
61 |
Correct |
559 ms |
22784 KB |
Output is correct |
62 |
Correct |
252 ms |
19964 KB |
Output is correct |
63 |
Correct |
939 ms |
16808 KB |
Output is correct |
64 |
Correct |
1312 ms |
19544 KB |
Output is correct |
65 |
Correct |
1471 ms |
28432 KB |
Output is correct |
66 |
Correct |
1179 ms |
10828 KB |
Output is correct |
67 |
Correct |
2115 ms |
23140 KB |
Output is correct |
68 |
Execution timed out |
3062 ms |
28328 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |