#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;
const int maxn = 1e5+5;
vector<int> adj[maxn];
int n;
int par[22][maxn];
int dep[maxn];
int cnt[maxn];
int pref[maxn];
int pos[maxn];
int head[maxn];
int tail[maxn];
ii per[maxn];
void dfs(int u = 1, int p = 0)
{
dep[u] = dep[p]+1;
par[0][u] = p;
cnt[u] = 1;
for(int i = 1; i<= 20; i++) par[i][u] = par[i-1][par[i-1][u]];
ii big = {0, -1};
for(int v : adj[u])
{
if(v == p) continue;
dfs(v, u);
cnt[u] += cnt[v];
big = max(big, {cnt[v], v});
}
pref[u] = big.Y;
}
void hld()
{
int tim = 1;
for(int i = 1; i<= n; i++)
{
if(pref[par[0][i]] == i) continue;
for(int j = i; j != -1; j = pref[j])
{
head[j] = i;
pos[j] = tim++;
tail[i] = j;
}
}
}
struct node
{
int val[4];
node()
{
memset(val, 0, sizeof val);
}
node operator + (node &other) const
{
node res;
for(int i = 0; i< 4; i++) res.val[i] = 1e9;
for(int i = 0; i< 4; i++)
{
int b1 = (i&2)> 0;
int b2 = (i&1)> 0;
for(int j = 0; j< 4; j++)
{
int b3 = (j&2)> 0;
int b4 = (j&1)> 0;
res.val[b1*2+b4] = min(res.val[b1*2+b4], val[i]+other.val[j]+(b2!= b3));
}
}
return res;
}
};
node st[4*maxn];
void build(int p = 1, int L = 1, int R = n)
{
if(L == R)
{
st[p] = node();
st[p].val[1] = st[p].val[2] = 1e9;
return;
}
int M = (L+R)>>1;
build(p<<1, L, M);
build(p<<1|1, M+1, R);
st[p] = st[p<<1]+st[p<<1|1];
}
void update(int x, ii v, int p = 1, int L = 1, int R = n)
{
if(L == R)
{
st[p].val[0] += v.X;
st[p].val[3] += v.Y;
return;
}
int M = (L+R)>>1;
if(x<= M) update(x, v, p<<1, L, M);
else update(x, v, p<<1|1, M+1, R);
st[p] = st[p<<1] + st[p<<1|1];
}
node ask(int i, int j, int p = 1, int L = 1, int R = n)
{
if(i> R || j< L)
{
node res;
res.val[1] = res.val[2] = 1e9;
return res;
}
if(i<= L && R<= j) return st[p];
int M = (L+R)>>1;
node x = ask(i, j, p<<1, L, M);
node y = ask(i, j, p<<1|1, M+1, R);
return x+y;
}
void change(int u, ii x)
{
// printf("change %d\n", u);
while(u)
{
// printf("go %d {%d, %d}\n", u, x.X, x.Y);
update(pos[u], x);
node tmp = ask(pos[head[u]], pos[tail[head[u]]]);
// for(int i = 0; i< 4; i++) printf("%d ", tmp.val[i]);
// printf("\n");
int v0 = min(tmp.val[0], tmp.val[1]);
int v1 = min(tmp.val[2], tmp.val[3]);
ii res = {min(v0, v1+1), min(v0+1, v1)};
x = ii(res.X-per[head[u]].X, res.Y-per[head[u]].Y);
per[head[u]] = res;
u = par[0][head[u]];
}
}
int gimme()
{
node res = ask(pos[1], pos[tail[1]]);
return *min_element(res.val, res.val+4);
}
void initialize(int N, vector<int> A, vector<int> B)
{
n = N;
for(int i = 0; i< n-1; i++)
{
adj[A[i]].pb(B[i]);
adj[B[i]].pb(A[i]);
}
dfs(); hld();
build();
}
int stat[maxn];
int cat(int u)
{
stat[u] = 1;
change(u, {0, 1e9});
return gimme();
}
int dog(int u)
{
stat[u] = 2;
change(u, {1e9, 0});
return gimme();
}
int neighbor(int u)
{
if(stat[u] == 1) change(u, {0, -1e9});
if(stat[u] == 2) change(u, {-1e9, 0});
stat[u] = 0;
return gimme();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9084 KB |
Output is correct |
2 |
Correct |
10 ms |
9080 KB |
Output is correct |
3 |
Correct |
10 ms |
9080 KB |
Output is correct |
4 |
Correct |
10 ms |
9080 KB |
Output is correct |
5 |
Correct |
10 ms |
9080 KB |
Output is correct |
6 |
Correct |
10 ms |
9080 KB |
Output is correct |
7 |
Correct |
10 ms |
9080 KB |
Output is correct |
8 |
Correct |
10 ms |
9080 KB |
Output is correct |
9 |
Correct |
10 ms |
9080 KB |
Output is correct |
10 |
Correct |
10 ms |
9080 KB |
Output is correct |
11 |
Correct |
10 ms |
9080 KB |
Output is correct |
12 |
Correct |
9 ms |
9080 KB |
Output is correct |
13 |
Correct |
9 ms |
9080 KB |
Output is correct |
14 |
Correct |
10 ms |
9080 KB |
Output is correct |
15 |
Correct |
11 ms |
9080 KB |
Output is correct |
16 |
Correct |
10 ms |
9084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9084 KB |
Output is correct |
2 |
Correct |
10 ms |
9080 KB |
Output is correct |
3 |
Correct |
10 ms |
9080 KB |
Output is correct |
4 |
Correct |
10 ms |
9080 KB |
Output is correct |
5 |
Correct |
10 ms |
9080 KB |
Output is correct |
6 |
Correct |
10 ms |
9080 KB |
Output is correct |
7 |
Correct |
10 ms |
9080 KB |
Output is correct |
8 |
Correct |
10 ms |
9080 KB |
Output is correct |
9 |
Correct |
10 ms |
9080 KB |
Output is correct |
10 |
Correct |
10 ms |
9080 KB |
Output is correct |
11 |
Correct |
10 ms |
9080 KB |
Output is correct |
12 |
Correct |
9 ms |
9080 KB |
Output is correct |
13 |
Correct |
9 ms |
9080 KB |
Output is correct |
14 |
Correct |
10 ms |
9080 KB |
Output is correct |
15 |
Correct |
11 ms |
9080 KB |
Output is correct |
16 |
Correct |
10 ms |
9084 KB |
Output is correct |
17 |
Correct |
12 ms |
9208 KB |
Output is correct |
18 |
Correct |
12 ms |
9208 KB |
Output is correct |
19 |
Correct |
11 ms |
9208 KB |
Output is correct |
20 |
Correct |
10 ms |
9080 KB |
Output is correct |
21 |
Correct |
13 ms |
9080 KB |
Output is correct |
22 |
Correct |
12 ms |
9160 KB |
Output is correct |
23 |
Correct |
12 ms |
9208 KB |
Output is correct |
24 |
Correct |
12 ms |
9208 KB |
Output is correct |
25 |
Correct |
11 ms |
9080 KB |
Output is correct |
26 |
Correct |
11 ms |
9208 KB |
Output is correct |
27 |
Correct |
10 ms |
9080 KB |
Output is correct |
28 |
Correct |
11 ms |
9208 KB |
Output is correct |
29 |
Correct |
11 ms |
9336 KB |
Output is correct |
30 |
Correct |
12 ms |
9080 KB |
Output is correct |
31 |
Correct |
10 ms |
9208 KB |
Output is correct |
32 |
Correct |
11 ms |
9080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9084 KB |
Output is correct |
2 |
Correct |
10 ms |
9080 KB |
Output is correct |
3 |
Correct |
10 ms |
9080 KB |
Output is correct |
4 |
Correct |
10 ms |
9080 KB |
Output is correct |
5 |
Correct |
10 ms |
9080 KB |
Output is correct |
6 |
Correct |
10 ms |
9080 KB |
Output is correct |
7 |
Correct |
10 ms |
9080 KB |
Output is correct |
8 |
Correct |
10 ms |
9080 KB |
Output is correct |
9 |
Correct |
10 ms |
9080 KB |
Output is correct |
10 |
Correct |
10 ms |
9080 KB |
Output is correct |
11 |
Correct |
10 ms |
9080 KB |
Output is correct |
12 |
Correct |
9 ms |
9080 KB |
Output is correct |
13 |
Correct |
9 ms |
9080 KB |
Output is correct |
14 |
Correct |
10 ms |
9080 KB |
Output is correct |
15 |
Correct |
11 ms |
9080 KB |
Output is correct |
16 |
Correct |
10 ms |
9084 KB |
Output is correct |
17 |
Correct |
12 ms |
9208 KB |
Output is correct |
18 |
Correct |
12 ms |
9208 KB |
Output is correct |
19 |
Correct |
11 ms |
9208 KB |
Output is correct |
20 |
Correct |
10 ms |
9080 KB |
Output is correct |
21 |
Correct |
13 ms |
9080 KB |
Output is correct |
22 |
Correct |
12 ms |
9160 KB |
Output is correct |
23 |
Correct |
12 ms |
9208 KB |
Output is correct |
24 |
Correct |
12 ms |
9208 KB |
Output is correct |
25 |
Correct |
11 ms |
9080 KB |
Output is correct |
26 |
Correct |
11 ms |
9208 KB |
Output is correct |
27 |
Correct |
10 ms |
9080 KB |
Output is correct |
28 |
Correct |
11 ms |
9208 KB |
Output is correct |
29 |
Correct |
11 ms |
9336 KB |
Output is correct |
30 |
Correct |
12 ms |
9080 KB |
Output is correct |
31 |
Correct |
10 ms |
9208 KB |
Output is correct |
32 |
Correct |
11 ms |
9080 KB |
Output is correct |
33 |
Correct |
490 ms |
19328 KB |
Output is correct |
34 |
Correct |
163 ms |
20344 KB |
Output is correct |
35 |
Correct |
440 ms |
16880 KB |
Output is correct |
36 |
Correct |
807 ms |
26560 KB |
Output is correct |
37 |
Correct |
36 ms |
14456 KB |
Output is correct |
38 |
Correct |
889 ms |
28392 KB |
Output is correct |
39 |
Correct |
836 ms |
28396 KB |
Output is correct |
40 |
Correct |
889 ms |
28516 KB |
Output is correct |
41 |
Correct |
890 ms |
28520 KB |
Output is correct |
42 |
Correct |
806 ms |
28432 KB |
Output is correct |
43 |
Correct |
814 ms |
28520 KB |
Output is correct |
44 |
Correct |
764 ms |
28516 KB |
Output is correct |
45 |
Correct |
767 ms |
28392 KB |
Output is correct |
46 |
Correct |
860 ms |
28520 KB |
Output is correct |
47 |
Correct |
852 ms |
28356 KB |
Output is correct |
48 |
Correct |
313 ms |
23008 KB |
Output is correct |
49 |
Correct |
311 ms |
26660 KB |
Output is correct |
50 |
Correct |
108 ms |
12792 KB |
Output is correct |
51 |
Correct |
121 ms |
15964 KB |
Output is correct |
52 |
Correct |
53 ms |
12664 KB |
Output is correct |
53 |
Correct |
363 ms |
26584 KB |
Output is correct |
54 |
Correct |
282 ms |
17068 KB |
Output is correct |
55 |
Correct |
741 ms |
22636 KB |
Output is correct |
56 |
Correct |
399 ms |
18080 KB |
Output is correct |
57 |
Correct |
549 ms |
25792 KB |
Output is correct |
58 |
Correct |
57 ms |
16408 KB |
Output is correct |
59 |
Correct |
118 ms |
15352 KB |
Output is correct |
60 |
Correct |
277 ms |
24696 KB |
Output is correct |
61 |
Correct |
296 ms |
25412 KB |
Output is correct |
62 |
Correct |
172 ms |
21968 KB |
Output is correct |
63 |
Correct |
90 ms |
20600 KB |
Output is correct |
64 |
Correct |
98 ms |
22648 KB |
Output is correct |
65 |
Correct |
142 ms |
30588 KB |
Output is correct |
66 |
Correct |
119 ms |
14496 KB |
Output is correct |
67 |
Correct |
134 ms |
24184 KB |
Output is correct |
68 |
Correct |
336 ms |
31096 KB |
Output is correct |
69 |
Correct |
54 ms |
11128 KB |
Output is correct |
70 |
Correct |
20 ms |
9464 KB |
Output is correct |
71 |
Correct |
111 ms |
18936 KB |
Output is correct |
72 |
Correct |
167 ms |
27128 KB |
Output is correct |
73 |
Correct |
405 ms |
34436 KB |
Output is correct |
74 |
Correct |
403 ms |
30840 KB |
Output is correct |
75 |
Correct |
293 ms |
34296 KB |
Output is correct |
76 |
Correct |
299 ms |
33016 KB |
Output is correct |
77 |
Correct |
378 ms |
31036 KB |
Output is correct |