#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
#include "catdog.h"
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
const ll NMAX = 250002;
const int INF = 1e9;
const ll nrbits = 20;
const ll MOD = 998244353;
vector <int> v[NMAX];
int stamp;
int n;
pii timp[NMAX];
int sz[NMAX];
int maxi[NMAX];
int cpt[NMAX];
int last[NMAX];
int pa[NMAX];
int dp[NMAX][2]; /// suma vecinilor daca node e 0(cat)/1(dog)
int notAllowed[NMAX][2];
struct Node {
int dp[2][2];
} aint[NMAX * 4];
void DFS(int node, int p) {
sz[node] = 1;
maxi[node] = -1;
for(auto x : v[node]) {
if(x == p) continue;
DFS(x, node);
sz[node] += sz[x];
if(maxi[node] == -1 || sz[maxi[node]] < sz[x])
maxi[node] = x;
}
}
void hld(int node, int p, int capat) {
timp[node].first = ++stamp;
pa[node] = p;
cpt[node] = capat;
last[capat] = node;
if(maxi[node] != -1) {
hld(maxi[node], node, capat);
}
for(auto x : v[node]) {
if(x == p || maxi[node] == x)
continue;
hld(x, node, x);
}
timp[node].second = stamp;
}
Node combine(Node a, Node b) {
Node sol;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
sol.dp[i][j] = INF;
for(int t = 0; t < 2; t++) {
sol.dp[i][j] = min(sol.dp[i][j], a.dp[i][t] + min(b.dp[t][j], b.dp[(t ^ 1)][j] + 1));
}
}
}
return sol;
}
void update(int node, int st, int dr, int a, int realNode) {
if(st == dr) {
aint[node].dp[0][0] = dp[realNode][0] + notAllowed[realNode][0] * INF;
aint[node].dp[1][1] = dp[realNode][1] + notAllowed[realNode][1] * INF;
aint[node].dp[0][1] = aint[node].dp[1][0] = INF;
return;
}
int mid = (st + dr) / 2;
if(a <= mid)
update(node * 2, st, mid, a, realNode);
else
update(node * 2 + 1, mid + 1, dr, a, realNode);
aint[node] = combine(aint[node * 2], aint[node * 2 + 1]); /// numar mai mic => preordine primul => capatul lantului
}
Node query(int node, int st, int dr, int a, int b) {
if(a <= st && dr <= b)
return aint[node];
int mid = (st + dr) / 2;
if(b <= mid) {
return query(node * 2, st, mid, a, b);
}
if(a > mid) {
return query(node * 2 + 1, mid + 1, dr, a, b);
}
return combine(query(node * 2, st, mid, a, b), query(node * 2 + 1, mid + 1, dr, a, b));
}
pii realValue(int node) {
Node x = query(1, 1, n, timp[node].first, timp[last[cpt[node]]].first);
return {min(x.dp[0][1], x.dp[0][0]), min(x.dp[1][0], x.dp[1][1])};
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
n = N;
for(int i = 0; i < A.size(); i++) {
v[A[i]].push_back(B[i]);
v[B[i]].push_back(A[i]);
}
DFS(1, 0);
hld(1, 0, 1);
for(int i = 1; i <= n; i++) {
/// aici nu avem ce adauga
update(1, 1, n, timp[i].first, i); /// = {0, 0}
}
}
void adauga(int node, int sgn) {
vector <pii> perechi;
while(pa[cpt[node]] != 0) {
int unde = pa[cpt[node]];
int cine = cpt[node];
perechi.push_back({unde, cine});
node = unde;
}
if(sgn == -1) reverse(perechi.begin(), perechi.end());
for(auto x : perechi) {
int unde = x.first;
int cine = x.second;
pii am = realValue(cine);
dp[unde][0] += sgn * min(am.first, am.second + 1);
dp[unde][1] += sgn * min(am.first + 1, am.second);
update(1, 1, n, timp[unde].first, unde);
}
}
int cat(int v) {
adauga(v, -1);
notAllowed[v][1] = 1;
update(1, 1, n, timp[v].first, v);
adauga(v, 1);
pii sol = realValue(1);
return min(sol.first, sol.second);
}
int dog(int v) {
adauga(v, -1);
notAllowed[v][0] = 1;
update(1, 1, n, timp[v].first, v);
adauga(v, 1);
pii sol = realValue(1);
return min(sol.first, sol.second);
}
int neighbor(int v) {
adauga(v, -1);
notAllowed[v][0] = notAllowed[v][1] = 0;
update(1, 1, n, timp[v].first, v);
adauga(v, 1);
pii sol = realValue(1);
return min(sol.first, sol.second);
}
Compilation message
catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:112:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for(int i = 0; i < A.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13916 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13980 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
3 ms |
14168 KB |
Output is correct |
10 |
Correct |
3 ms |
13912 KB |
Output is correct |
11 |
Correct |
3 ms |
13916 KB |
Output is correct |
12 |
Correct |
4 ms |
13916 KB |
Output is correct |
13 |
Correct |
3 ms |
13916 KB |
Output is correct |
14 |
Correct |
4 ms |
13916 KB |
Output is correct |
15 |
Correct |
3 ms |
13916 KB |
Output is correct |
16 |
Correct |
3 ms |
13916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13916 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13980 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
3 ms |
14168 KB |
Output is correct |
10 |
Correct |
3 ms |
13912 KB |
Output is correct |
11 |
Correct |
3 ms |
13916 KB |
Output is correct |
12 |
Correct |
4 ms |
13916 KB |
Output is correct |
13 |
Correct |
3 ms |
13916 KB |
Output is correct |
14 |
Correct |
4 ms |
13916 KB |
Output is correct |
15 |
Correct |
3 ms |
13916 KB |
Output is correct |
16 |
Correct |
3 ms |
13916 KB |
Output is correct |
17 |
Correct |
4 ms |
13912 KB |
Output is correct |
18 |
Correct |
6 ms |
13916 KB |
Output is correct |
19 |
Correct |
4 ms |
13916 KB |
Output is correct |
20 |
Correct |
3 ms |
13916 KB |
Output is correct |
21 |
Correct |
4 ms |
13916 KB |
Output is correct |
22 |
Correct |
3 ms |
14004 KB |
Output is correct |
23 |
Correct |
4 ms |
13988 KB |
Output is correct |
24 |
Correct |
4 ms |
13912 KB |
Output is correct |
25 |
Correct |
4 ms |
13912 KB |
Output is correct |
26 |
Correct |
4 ms |
13916 KB |
Output is correct |
27 |
Correct |
3 ms |
13916 KB |
Output is correct |
28 |
Correct |
3 ms |
13916 KB |
Output is correct |
29 |
Correct |
5 ms |
13916 KB |
Output is correct |
30 |
Correct |
3 ms |
13916 KB |
Output is correct |
31 |
Correct |
3 ms |
13988 KB |
Output is correct |
32 |
Correct |
3 ms |
13916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13916 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13980 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
3 ms |
14168 KB |
Output is correct |
10 |
Correct |
3 ms |
13912 KB |
Output is correct |
11 |
Correct |
3 ms |
13916 KB |
Output is correct |
12 |
Correct |
4 ms |
13916 KB |
Output is correct |
13 |
Correct |
3 ms |
13916 KB |
Output is correct |
14 |
Correct |
4 ms |
13916 KB |
Output is correct |
15 |
Correct |
3 ms |
13916 KB |
Output is correct |
16 |
Correct |
3 ms |
13916 KB |
Output is correct |
17 |
Correct |
4 ms |
13912 KB |
Output is correct |
18 |
Correct |
6 ms |
13916 KB |
Output is correct |
19 |
Correct |
4 ms |
13916 KB |
Output is correct |
20 |
Correct |
3 ms |
13916 KB |
Output is correct |
21 |
Correct |
4 ms |
13916 KB |
Output is correct |
22 |
Correct |
3 ms |
14004 KB |
Output is correct |
23 |
Correct |
4 ms |
13988 KB |
Output is correct |
24 |
Correct |
4 ms |
13912 KB |
Output is correct |
25 |
Correct |
4 ms |
13912 KB |
Output is correct |
26 |
Correct |
4 ms |
13916 KB |
Output is correct |
27 |
Correct |
3 ms |
13916 KB |
Output is correct |
28 |
Correct |
3 ms |
13916 KB |
Output is correct |
29 |
Correct |
5 ms |
13916 KB |
Output is correct |
30 |
Correct |
3 ms |
13916 KB |
Output is correct |
31 |
Correct |
3 ms |
13988 KB |
Output is correct |
32 |
Correct |
3 ms |
13916 KB |
Output is correct |
33 |
Correct |
266 ms |
22968 KB |
Output is correct |
34 |
Correct |
93 ms |
22768 KB |
Output is correct |
35 |
Correct |
251 ms |
22360 KB |
Output is correct |
36 |
Correct |
395 ms |
28684 KB |
Output is correct |
37 |
Correct |
23 ms |
20412 KB |
Output is correct |
38 |
Correct |
436 ms |
29500 KB |
Output is correct |
39 |
Correct |
442 ms |
29480 KB |
Output is correct |
40 |
Correct |
429 ms |
29504 KB |
Output is correct |
41 |
Correct |
435 ms |
29676 KB |
Output is correct |
42 |
Correct |
402 ms |
29500 KB |
Output is correct |
43 |
Correct |
414 ms |
29476 KB |
Output is correct |
44 |
Correct |
415 ms |
29692 KB |
Output is correct |
45 |
Correct |
416 ms |
29312 KB |
Output is correct |
46 |
Correct |
427 ms |
29512 KB |
Output is correct |
47 |
Correct |
418 ms |
29472 KB |
Output is correct |
48 |
Correct |
126 ms |
26268 KB |
Output is correct |
49 |
Correct |
140 ms |
27616 KB |
Output is correct |
50 |
Correct |
49 ms |
18000 KB |
Output is correct |
51 |
Correct |
57 ms |
21096 KB |
Output is correct |
52 |
Correct |
28 ms |
17568 KB |
Output is correct |
53 |
Correct |
192 ms |
27724 KB |
Output is correct |
54 |
Correct |
137 ms |
21916 KB |
Output is correct |
55 |
Correct |
359 ms |
26952 KB |
Output is correct |
56 |
Correct |
222 ms |
22596 KB |
Output is correct |
57 |
Correct |
304 ms |
27792 KB |
Output is correct |
58 |
Correct |
29 ms |
20952 KB |
Output is correct |
59 |
Correct |
50 ms |
21072 KB |
Output is correct |
60 |
Correct |
113 ms |
26952 KB |
Output is correct |
61 |
Correct |
130 ms |
27192 KB |
Output is correct |
62 |
Correct |
82 ms |
25532 KB |
Output is correct |
63 |
Correct |
48 ms |
24144 KB |
Output is correct |
64 |
Correct |
51 ms |
25700 KB |
Output is correct |
65 |
Correct |
80 ms |
31584 KB |
Output is correct |
66 |
Correct |
54 ms |
19292 KB |
Output is correct |
67 |
Correct |
65 ms |
28496 KB |
Output is correct |
68 |
Correct |
124 ms |
32080 KB |
Output is correct |
69 |
Correct |
24 ms |
17616 KB |
Output is correct |
70 |
Correct |
7 ms |
14172 KB |
Output is correct |
71 |
Correct |
49 ms |
24092 KB |
Output is correct |
72 |
Correct |
75 ms |
30668 KB |
Output is correct |
73 |
Correct |
198 ms |
35376 KB |
Output is correct |
74 |
Correct |
247 ms |
31824 KB |
Output is correct |
75 |
Correct |
133 ms |
35484 KB |
Output is correct |
76 |
Correct |
126 ms |
33948 KB |
Output is correct |
77 |
Correct |
235 ms |
32076 KB |
Output is correct |