#include "catdog.h"
#include <bits/stdc++.h>
#define MN 100071
using namespace std;
int n, Sz[MN];
vector<int> L[MN];
const int kInf = numeric_limits<int>::max() / 4;
struct mat {
int v[2][2];
mat() {
v[0][0] = v[1][1] = 0;
v[0][1] = v[1][0] = kInf;
}
mat(int a, int b, int c, int d) {
v[0][0] = a, v[0][1] = b, v[1][0] = c, v[1][1] = d;
}
} I;
void afis(mat a);
namespace AINT {
mat uni(const mat &a, const mat &b) {
mat re(kInf, kInf, kInf, kInf);
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 2; ++j)
for(int k = 0; k < 2; ++k)
for(int w = 0; w < 2; ++w)
re.v[i][w] = min(re.v[i][w], a.v[i][j] + b.v[k][w] + (j ^ k));
return re;
}
mat V[4 * MN];
int len;
void init() {
//for(int i = 0; i < 4 * MN; ++i) V[i] = r0;
}
void update(int p, mat v, int u = 1, int s = 1, int d = len) {
if(d < p || p < s) return;
if(s == d) {
V[u] = v;
//printf("Starea nodului %d(%d, %d) din AINT:\n", u, s, d);
afis(V[u]);
//printf("||||||||||||||\n");
return;
}
update(p, v, u << 1, s, (s + d) >> 1);
update(p, v, u << 1 | 1, ((s + d) >> 1) + 1, d);
V[u] = uni(V[u << 1], V[u << 1 | 1]);
//printf("Starea nodului %d(%d, %d) din AINT:\n", u, s, d);
afis(V[u]);
//printf("||||||||||||||\n");
}
mat query(int l, int r, int u = 1, int s = 1, int d = len) {
if(r < s || d < l) return I;
if(l <= s && d <= r) {
return V[u];
}
int mij = (s + d) >> 1;
if(r <= mij) return query(l, r, u << 1, s, (s + d) >> 1);
if(mij < l) return query(l, r, u << 1 | 1, ((s + d) >> 1) + 1, d);
return uni(query(l, r, u << 1, s, (s + d) >> 1), query(l, r, u << 1 | 1, ((s + d) >> 1) + 1, d));
}
}
int Par[MN], In[MN], Out[MN], tmr, Nxt[MN];
int CFD[MN][2]; //cost fii secundari
int TipHut[MN];
void recalc_mat(int u) {
mat re;
if(TipHut[u] == 0) {
re.v[0][0] = 0;
re.v[1][1] = kInf;
}
if(TipHut[u] == 1) {
re.v[0][0] = kInf;
re.v[1][1] = 0;
}
if(TipHut[u] == 2) {
re.v[0][0] = 0;
re.v[1][1] = 0;
}
re.v[0][1] = kInf;
re.v[1][0] = kInf;
re.v[0][0] += CFD[u][0];
re.v[1][1] += CFD[u][1];
//printf(" !!! Pt nodul %d am recalc matricea:\n", u);
afis(re);
//printf("\n\n");
AINT::update(In[u], re);
}
void afis(mat a) {
//printf("%d %d\n%d %d\n", a.v[0][0], a.v[0][1], a.v[1][0], a.v[1][1]);
}
void propag(int u, int factor) {
while(u) {
int head = Nxt[u];
int tail = Out[head];
int par = Par[head];
mat rez = AINT::query(In[head], In[tail]);
//printf("u %d head %d tail %d par %d mat:\n", u, head, tail, par);
afis(rez);
//printf("---\n");
CFD[par][0] += factor * min(min(rez.v[0][0], rez.v[0][1]), 1 + min(rez.v[1][0], rez.v[1][1]));
CFD[par][1] += factor * min(1 + min(rez.v[0][0], rez.v[0][1]), min(rez.v[1][0], rez.v[1][1]));
if(factor == 1) recalc_mat(par);
u = par;
}
}
void update(int u) {
//printf("Update la %d cu tip %d\n", u, TipHut[u]);
propag(u, -1);
recalc_mat(u);
propag(u, 1);
}
void dfs_sz(int u, int p) {
Sz[u] = 1;
for(auto &it : L[u]) if(it != p) {
dfs_sz(it, u);
Sz[u] += Sz[it];
if(L[u][0] == p || Sz[it] > Sz[L[u][0]]) swap(it, L[u][0]);
}
}
void dfs_hld(int u, int p) {
Par[u] = p;
In[u] = ++tmr;
if(p) {
Nxt[u] = (u == L[p][0] ? Nxt[p] : u);
} else Nxt[u] = u;
Out[Nxt[u]] = u;
for(auto it : L[u])
if(it != p) dfs_hld(it, u);
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
n = N;
for(int i = 0; i <= n; ++i)
TipHut[i] = 2;
for(int i = 0; i < int(A.size()); ++i)
L[A[i]].push_back(B[i]), L[B[i]].push_back(A[i]);
dfs_sz(1, 0);
AINT::init();
dfs_hld(1, 0);
AINT::len = tmr;
//printf("Nod %d S %d N %d P %d I %d\n", i, Sz[i], Nxt[i], Par[i], In[i]);
}
int rez() {
return min(CFD[0][0], CFD[0][1]);
}
int cat(int v) {
TipHut[v] = 0;
update(v);
return rez();
}
int dog(int v) {
TipHut[v] = 1;
update(v);
return rez();
}
int neighbor(int v) {
TipHut[v] = 2;
update(v);
return rez();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8924 KB |
Output is correct |
3 |
Correct |
5 ms |
8924 KB |
Output is correct |
4 |
Correct |
5 ms |
8860 KB |
Output is correct |
5 |
Correct |
5 ms |
8932 KB |
Output is correct |
6 |
Correct |
4 ms |
8924 KB |
Output is correct |
7 |
Correct |
5 ms |
8912 KB |
Output is correct |
8 |
Correct |
5 ms |
8928 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
7 ms |
8916 KB |
Output is correct |
11 |
Correct |
6 ms |
8924 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
6 ms |
8900 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8844 KB |
Output is correct |
16 |
Correct |
6 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8924 KB |
Output is correct |
3 |
Correct |
5 ms |
8924 KB |
Output is correct |
4 |
Correct |
5 ms |
8860 KB |
Output is correct |
5 |
Correct |
5 ms |
8932 KB |
Output is correct |
6 |
Correct |
4 ms |
8924 KB |
Output is correct |
7 |
Correct |
5 ms |
8912 KB |
Output is correct |
8 |
Correct |
5 ms |
8928 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
7 ms |
8916 KB |
Output is correct |
11 |
Correct |
6 ms |
8924 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
6 ms |
8900 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8844 KB |
Output is correct |
16 |
Correct |
6 ms |
8916 KB |
Output is correct |
17 |
Correct |
6 ms |
8956 KB |
Output is correct |
18 |
Correct |
6 ms |
8916 KB |
Output is correct |
19 |
Correct |
6 ms |
8916 KB |
Output is correct |
20 |
Correct |
5 ms |
8924 KB |
Output is correct |
21 |
Correct |
5 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
8932 KB |
Output is correct |
23 |
Correct |
6 ms |
8932 KB |
Output is correct |
24 |
Correct |
6 ms |
8916 KB |
Output is correct |
25 |
Correct |
8 ms |
8932 KB |
Output is correct |
26 |
Correct |
6 ms |
8916 KB |
Output is correct |
27 |
Correct |
6 ms |
8980 KB |
Output is correct |
28 |
Correct |
6 ms |
9044 KB |
Output is correct |
29 |
Correct |
7 ms |
9044 KB |
Output is correct |
30 |
Correct |
6 ms |
8916 KB |
Output is correct |
31 |
Correct |
5 ms |
8916 KB |
Output is correct |
32 |
Correct |
5 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8924 KB |
Output is correct |
3 |
Correct |
5 ms |
8924 KB |
Output is correct |
4 |
Correct |
5 ms |
8860 KB |
Output is correct |
5 |
Correct |
5 ms |
8932 KB |
Output is correct |
6 |
Correct |
4 ms |
8924 KB |
Output is correct |
7 |
Correct |
5 ms |
8912 KB |
Output is correct |
8 |
Correct |
5 ms |
8928 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
7 ms |
8916 KB |
Output is correct |
11 |
Correct |
6 ms |
8924 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
6 ms |
8900 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8844 KB |
Output is correct |
16 |
Correct |
6 ms |
8916 KB |
Output is correct |
17 |
Correct |
6 ms |
8956 KB |
Output is correct |
18 |
Correct |
6 ms |
8916 KB |
Output is correct |
19 |
Correct |
6 ms |
8916 KB |
Output is correct |
20 |
Correct |
5 ms |
8924 KB |
Output is correct |
21 |
Correct |
5 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
8932 KB |
Output is correct |
23 |
Correct |
6 ms |
8932 KB |
Output is correct |
24 |
Correct |
6 ms |
8916 KB |
Output is correct |
25 |
Correct |
8 ms |
8932 KB |
Output is correct |
26 |
Correct |
6 ms |
8916 KB |
Output is correct |
27 |
Correct |
6 ms |
8980 KB |
Output is correct |
28 |
Correct |
6 ms |
9044 KB |
Output is correct |
29 |
Correct |
7 ms |
9044 KB |
Output is correct |
30 |
Correct |
6 ms |
8916 KB |
Output is correct |
31 |
Correct |
5 ms |
8916 KB |
Output is correct |
32 |
Correct |
5 ms |
8916 KB |
Output is correct |
33 |
Correct |
299 ms |
14784 KB |
Output is correct |
34 |
Correct |
127 ms |
14636 KB |
Output is correct |
35 |
Correct |
293 ms |
13644 KB |
Output is correct |
36 |
Correct |
490 ms |
18696 KB |
Output is correct |
37 |
Correct |
18 ms |
11708 KB |
Output is correct |
38 |
Correct |
567 ms |
19628 KB |
Output is correct |
39 |
Correct |
639 ms |
19640 KB |
Output is correct |
40 |
Correct |
552 ms |
19644 KB |
Output is correct |
41 |
Correct |
519 ms |
19644 KB |
Output is correct |
42 |
Correct |
477 ms |
19648 KB |
Output is correct |
43 |
Correct |
560 ms |
19640 KB |
Output is correct |
44 |
Correct |
502 ms |
19772 KB |
Output is correct |
45 |
Correct |
508 ms |
19668 KB |
Output is correct |
46 |
Correct |
472 ms |
19636 KB |
Output is correct |
47 |
Correct |
517 ms |
19632 KB |
Output is correct |
48 |
Correct |
132 ms |
16108 KB |
Output is correct |
49 |
Correct |
175 ms |
17812 KB |
Output is correct |
50 |
Correct |
56 ms |
11072 KB |
Output is correct |
51 |
Correct |
78 ms |
12388 KB |
Output is correct |
52 |
Correct |
27 ms |
10708 KB |
Output is correct |
53 |
Correct |
211 ms |
17948 KB |
Output is correct |
54 |
Correct |
163 ms |
13264 KB |
Output is correct |
55 |
Correct |
456 ms |
16744 KB |
Output is correct |
56 |
Correct |
256 ms |
14068 KB |
Output is correct |
57 |
Correct |
379 ms |
17872 KB |
Output is correct |
58 |
Correct |
26 ms |
12624 KB |
Output is correct |
59 |
Correct |
57 ms |
12220 KB |
Output is correct |
60 |
Correct |
122 ms |
16956 KB |
Output is correct |
61 |
Correct |
145 ms |
17480 KB |
Output is correct |
62 |
Correct |
79 ms |
15464 KB |
Output is correct |
63 |
Correct |
51 ms |
15872 KB |
Output is correct |
64 |
Correct |
52 ms |
17576 KB |
Output is correct |
65 |
Correct |
59 ms |
22344 KB |
Output is correct |
66 |
Correct |
76 ms |
12544 KB |
Output is correct |
67 |
Correct |
71 ms |
18516 KB |
Output is correct |
68 |
Correct |
147 ms |
22636 KB |
Output is correct |
69 |
Correct |
39 ms |
10444 KB |
Output is correct |
70 |
Correct |
11 ms |
9172 KB |
Output is correct |
71 |
Correct |
64 ms |
15572 KB |
Output is correct |
72 |
Correct |
109 ms |
20864 KB |
Output is correct |
73 |
Correct |
225 ms |
25952 KB |
Output is correct |
74 |
Correct |
302 ms |
22380 KB |
Output is correct |
75 |
Correct |
180 ms |
25832 KB |
Output is correct |
76 |
Correct |
157 ms |
24624 KB |
Output is correct |
77 |
Correct |
289 ms |
22764 KB |
Output is correct |