#include "catdog.h"
#include <bits/stdc++.h>
#define eb emplace_back
#define INF (0x3f3f3f3f)
using namespace std;
const int MAXN = 100055;
static int _int[MAXN<<5], *_intp=_int;
inline int* newint(const unsigned int SZ) {
_intp += SZ;
return _intp - SZ;
}
struct HLD {
int *DCC, *DCD, *DDC, *DDD;
int *CC, *CD;
int n, MX, pc, pd;
void init(int _n) {
n = _n;
for(MX = 1; MX < n; MX <<= 1);
CC = newint(n);
CD = newint(n);
DCC = newint(MX<<1);
DCD = newint(MX<<1);
DDC = newint(MX<<1);
DDD = newint(MX<<1);
fill(DCD+1, DCD+MX, 1);
fill(DDC+1, DDC+MX, 1);
fill(DCD+MX, DCD+(MX<<1), INF);
fill(DDC+MX, DDC+(MX<<1), INF);
}
void upd(int i) {
DCC[i+MX] = CC[i];
DDD[i+MX] = CD[i];
for(i = (i+MX)>>1; i; i >>= 1) {
int l = i<<1, r = l|1;
DCC[i] = min({INF, DCC[l]+DCC[r], DCD[l]+DDC[r]
, DCC[l]+DDC[r]+1, DCD[l]+DCC[r]+1});
DDD[i] = min({INF, DDD[l]+DDD[r], DDC[l]+DCD[r]
, DDD[l]+DCD[r]+1, DDC[l]+DDD[r]+1});
DCD[i] = min({INF, DCC[l]+DCD[r], DCD[l]+DDD[r]
, DCC[l]+DDD[r]+1, DCD[l]+DCD[r]+1});
DDC[i] = min({INF, DDD[l]+DDC[r], DDC[l]+DCC[r]
, DDD[l]+DCC[r]+1, DDC[l]+DDC[r]+1});
if(!DCD[i]) DCD[i] = 1;
if(!DDC[i]) DDC[i] = 1;
}
}
int get() { return min({DCC[1], DCD[1], DDC[1], DDD[1]}); }
} hld[MAXN];
vector<int> G[MAXN];
int SC[MAXN], SD[MAXN];
int HI[MAXN], HJ[MAXN], HC[MAXN], HR[MAXN], Hn;
int prt[MAXN], dep[MAXN], cnt[MAXN];
bitset<MAXN> chk, isc;
int N, Q;
void g(int v) {
auto &V = hld[HI[v]];
int j = HJ[v];
V.CC[j] = SC[v];
V.CD[j] = SD[v];
if(chk[v]) (isc[v] ? V.CD[j] : V.CC[j]) = INF;
}
void f(int v) {
for(int p, j; v; v = p) {
g(v);
auto &V = hld[HI[v]];
j = HJ[v]; p = prt[HR[HI[v]]];
V.upd(j);
SC[p] -= min(V.pc, V.pd+1);
SD[p] -= min(V.pd, V.pc+1);
V.pc = min(V.DCC[1], V.DCD[1]);
V.pd = min(V.DDC[1], V.DDD[1]);
SC[p] += min(V.pc, V.pd+1);
SD[p] += min(V.pd, V.pc+1);
}
}
void hfs(int i) {
HC[HI[i]]++;
int hi = -1, hc = 0;
for(int v : G[i])
if(v != prt[i] && hc < cnt[v]) {
hi = v; hc = cnt[v];
}
if(!hc) return;
HI[hi] = HI[i];
HJ[hi] = HJ[i]+1;
for(int v : G[i]) if(v != prt[i]) {
if(v != hi) {
HI[v] = ++Hn;
HR[Hn] = v;
}
hfs(v);
}
}
void dfs(int i) {
cnt[i] = 1;
for(int v : G[i]) if(!dep[v]) {
dep[v] = dep[i] + 1;
prt[v] = i;
dfs(v);
cnt[i] += cnt[v];
}
}
void initialize(int N, std::vector<int> _A, std::vector<int> _B) {
::N = N;
for(int i = 1, a, b; i < N; i++) {
a = _A[i-1]; b = _B[i-1];
G[a].eb(b);
G[b].eb(a);
}
dep[1] = 1; dfs(1);
HI[1] = HR[1] = Hn = 1; hfs(1);
for(int i = 1; i <= Hn; i++) hld[i].init(HC[i]);
}
int cat(int v) {
chk[v] = isc[v] = true;
f(v);
return hld[1].get();
}
int dog(int v) {
chk[v] = true; isc[v] = false;
f(v);
return hld[1].get();
}
int neighbor(int v) {
chk[v] = isc[v] = false;
f(v);
return hld[1].get();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2816 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
5 ms |
2688 KB |
Output is correct |
4 |
Correct |
4 ms |
2816 KB |
Output is correct |
5 |
Correct |
4 ms |
2816 KB |
Output is correct |
6 |
Correct |
5 ms |
2788 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
5 ms |
2688 KB |
Output is correct |
9 |
Correct |
5 ms |
2816 KB |
Output is correct |
10 |
Correct |
5 ms |
2816 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
4 ms |
2816 KB |
Output is correct |
13 |
Correct |
5 ms |
2816 KB |
Output is correct |
14 |
Correct |
4 ms |
2816 KB |
Output is correct |
15 |
Correct |
5 ms |
2688 KB |
Output is correct |
16 |
Correct |
4 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2816 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
5 ms |
2688 KB |
Output is correct |
4 |
Correct |
4 ms |
2816 KB |
Output is correct |
5 |
Correct |
4 ms |
2816 KB |
Output is correct |
6 |
Correct |
5 ms |
2788 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
5 ms |
2688 KB |
Output is correct |
9 |
Correct |
5 ms |
2816 KB |
Output is correct |
10 |
Correct |
5 ms |
2816 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
4 ms |
2816 KB |
Output is correct |
13 |
Correct |
5 ms |
2816 KB |
Output is correct |
14 |
Correct |
4 ms |
2816 KB |
Output is correct |
15 |
Correct |
5 ms |
2688 KB |
Output is correct |
16 |
Correct |
4 ms |
2816 KB |
Output is correct |
17 |
Correct |
5 ms |
2944 KB |
Output is correct |
18 |
Correct |
5 ms |
2944 KB |
Output is correct |
19 |
Correct |
5 ms |
2816 KB |
Output is correct |
20 |
Correct |
6 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
6 ms |
2816 KB |
Output is correct |
23 |
Correct |
5 ms |
2944 KB |
Output is correct |
24 |
Correct |
5 ms |
2816 KB |
Output is correct |
25 |
Correct |
5 ms |
2816 KB |
Output is correct |
26 |
Correct |
1 ms |
2816 KB |
Output is correct |
27 |
Correct |
5 ms |
2816 KB |
Output is correct |
28 |
Correct |
5 ms |
2944 KB |
Output is correct |
29 |
Correct |
6 ms |
2944 KB |
Output is correct |
30 |
Correct |
6 ms |
2816 KB |
Output is correct |
31 |
Correct |
5 ms |
2944 KB |
Output is correct |
32 |
Correct |
5 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2816 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
5 ms |
2688 KB |
Output is correct |
4 |
Correct |
4 ms |
2816 KB |
Output is correct |
5 |
Correct |
4 ms |
2816 KB |
Output is correct |
6 |
Correct |
5 ms |
2788 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
5 ms |
2688 KB |
Output is correct |
9 |
Correct |
5 ms |
2816 KB |
Output is correct |
10 |
Correct |
5 ms |
2816 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
4 ms |
2816 KB |
Output is correct |
13 |
Correct |
5 ms |
2816 KB |
Output is correct |
14 |
Correct |
4 ms |
2816 KB |
Output is correct |
15 |
Correct |
5 ms |
2688 KB |
Output is correct |
16 |
Correct |
4 ms |
2816 KB |
Output is correct |
17 |
Correct |
5 ms |
2944 KB |
Output is correct |
18 |
Correct |
5 ms |
2944 KB |
Output is correct |
19 |
Correct |
5 ms |
2816 KB |
Output is correct |
20 |
Correct |
6 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
6 ms |
2816 KB |
Output is correct |
23 |
Correct |
5 ms |
2944 KB |
Output is correct |
24 |
Correct |
5 ms |
2816 KB |
Output is correct |
25 |
Correct |
5 ms |
2816 KB |
Output is correct |
26 |
Correct |
1 ms |
2816 KB |
Output is correct |
27 |
Correct |
5 ms |
2816 KB |
Output is correct |
28 |
Correct |
5 ms |
2944 KB |
Output is correct |
29 |
Correct |
6 ms |
2944 KB |
Output is correct |
30 |
Correct |
6 ms |
2816 KB |
Output is correct |
31 |
Correct |
5 ms |
2944 KB |
Output is correct |
32 |
Correct |
5 ms |
2816 KB |
Output is correct |
33 |
Correct |
233 ms |
12236 KB |
Output is correct |
34 |
Correct |
81 ms |
12920 KB |
Output is correct |
35 |
Correct |
148 ms |
9792 KB |
Output is correct |
36 |
Correct |
323 ms |
18880 KB |
Output is correct |
37 |
Correct |
29 ms |
7544 KB |
Output is correct |
38 |
Correct |
365 ms |
20584 KB |
Output is correct |
39 |
Correct |
350 ms |
20580 KB |
Output is correct |
40 |
Correct |
332 ms |
20456 KB |
Output is correct |
41 |
Correct |
367 ms |
20504 KB |
Output is correct |
42 |
Correct |
355 ms |
20580 KB |
Output is correct |
43 |
Correct |
322 ms |
20724 KB |
Output is correct |
44 |
Correct |
341 ms |
20452 KB |
Output is correct |
45 |
Correct |
380 ms |
20456 KB |
Output is correct |
46 |
Correct |
306 ms |
20592 KB |
Output is correct |
47 |
Correct |
340 ms |
20512 KB |
Output is correct |
48 |
Correct |
118 ms |
17284 KB |
Output is correct |
49 |
Correct |
109 ms |
20916 KB |
Output is correct |
50 |
Correct |
39 ms |
6648 KB |
Output is correct |
51 |
Correct |
51 ms |
9844 KB |
Output is correct |
52 |
Correct |
20 ms |
6400 KB |
Output is correct |
53 |
Correct |
195 ms |
18848 KB |
Output is correct |
54 |
Correct |
86 ms |
10032 KB |
Output is correct |
55 |
Correct |
354 ms |
15340 KB |
Output is correct |
56 |
Correct |
129 ms |
11040 KB |
Output is correct |
57 |
Correct |
204 ms |
18008 KB |
Output is correct |
58 |
Correct |
30 ms |
10740 KB |
Output is correct |
59 |
Correct |
35 ms |
9216 KB |
Output is correct |
60 |
Correct |
100 ms |
19056 KB |
Output is correct |
61 |
Correct |
109 ms |
19696 KB |
Output is correct |
62 |
Correct |
67 ms |
16368 KB |
Output is correct |
63 |
Correct |
62 ms |
9848 KB |
Output is correct |
64 |
Correct |
67 ms |
11204 KB |
Output is correct |
65 |
Correct |
115 ms |
16416 KB |
Output is correct |
66 |
Correct |
69 ms |
6776 KB |
Output is correct |
67 |
Correct |
88 ms |
12280 KB |
Output is correct |
68 |
Correct |
180 ms |
16712 KB |
Output is correct |
69 |
Correct |
42 ms |
4276 KB |
Output is correct |
70 |
Correct |
10 ms |
3072 KB |
Output is correct |
71 |
Correct |
67 ms |
9536 KB |
Output is correct |
72 |
Correct |
106 ms |
14776 KB |
Output is correct |
73 |
Correct |
234 ms |
18756 KB |
Output is correct |
74 |
Correct |
219 ms |
16632 KB |
Output is correct |
75 |
Correct |
189 ms |
18820 KB |
Output is correct |
76 |
Correct |
177 ms |
18468 KB |
Output is correct |
77 |
Correct |
233 ms |
16760 KB |
Output is correct |