#include "catdog.h"
#include<bits/stdc++.h>
using namespace std;
const int inf=2e5;
const int maxn = 1e5+5;
#define pii pair<int,int>
#define fi first
#define se second
struct node{
int x[2][2];
node(int a=0,int b=0){
x[0][0]=a;
x[1][1]=b;
x[0][1]=x[1][0]=inf;
}
friend node operator+(node a,node b){
node res(inf,inf);
for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) for(int p=0;p<=1;p++) for(int q=0;q<=1;q++) res.x[i][j]=min(res.x[i][j],a.x[i][p]+b.x[q][j]+(p^q));
return res;
}
};
struct Segtree{
vector<node> tree;
void build(int l,int r,int id){
if(l==r) return;
int mid=(l+r)>>1;
build(l,mid,id<<1);build(mid+1,r,id<<1|1);
tree[id]=tree[id<<1]+tree[id<<1|1];
}
void build(int n){
tree.assign(4*n,node());
build(1,n,1);
}
void update(int l,int r,int id,int p,int va,int vb){
if(l==r){
tree[id].x[0][0]+=va;
tree[id].x[1][1]+=vb;
return;
}
int mid=(l+r)>>1;
if(p<=mid) update(l,mid,id<<1,p,va,vb);
else update(mid+1,r,id<<1|1,p,va,vb);
tree[id]=tree[id<<1]+tree[id<<1|1];
}
pii get(){
int a=min(tree[1].x[0][0],tree[1].x[0][1]),b=min(tree[1].x[1][0],tree[1].x[1][1]);
return {min(a,b+1),min(b,a+1)};
}
}ST[maxn];
int n;
vector<int> edge[maxn];
int child[maxn],son[maxn];
void pre_dfs(int u,int p){
child[u]=1;
for(int v:edge[u]){
if(v==p) continue;
pre_dfs(v,u);
child[u]+=child[v];
if(child[v]>child[son[u]]) son[u]=v;
}
}
int head[maxn],par[maxn],L[maxn],cnt[maxn],pos;
void dfs(int u,int p,int t){
if(t) head[u]=head[p];
else head[u]=u;
L[u]=++pos;
par[u]=p;
cnt[head[u]]++;
if(son[u]) dfs(son[u],u,1);
for(int v:edge[u]) if(v!=p && v!=son[u]) dfs(v,u,0);
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
n=N;
for(int i=0;i<n-1;i++){
edge[A[i]].push_back(B[i]);
edge[B[i]].push_back(A[i]);
}
pre_dfs(1,0);
dfs(1,0,0);
for(int i=1;i<=n;i++) if(cnt[i]) ST[i].build(cnt[i]);
}
void update(int v,int va,int vb){
while(v){
int u=head[v];
pii x=ST[u].get();
ST[u].update(1,cnt[u],1,L[v]-L[u]+1,va,vb);
pii y=ST[u].get();
va=y.fi-x.fi;
vb=y.se-x.se;
v=par[u];
}
}
int state[maxn];
int cat(int v) {
state[v]=1;
update(v,inf,0);
auto res = ST[1].get();
return min(res.fi,res.se);
}
int dog(int v) {
state[v]=2;
update(v,0,inf);
auto res = ST[1].get();
return min(res.fi,res.se);
}
int neighbor(int v) {
update(v,-(state[v]==1)*inf,-(state[v]==2)*inf);
state[v]=0;
auto res = ST[1].get();
return min(res.fi,res.se);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5468 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
2 ms |
5468 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
2 ms |
5468 KB |
Output is correct |
8 |
Correct |
2 ms |
5688 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
2 ms |
5468 KB |
Output is correct |
12 |
Correct |
2 ms |
5468 KB |
Output is correct |
13 |
Correct |
2 ms |
5468 KB |
Output is correct |
14 |
Correct |
2 ms |
5468 KB |
Output is correct |
15 |
Correct |
2 ms |
5468 KB |
Output is correct |
16 |
Correct |
2 ms |
5656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5468 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
2 ms |
5468 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
2 ms |
5468 KB |
Output is correct |
8 |
Correct |
2 ms |
5688 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
2 ms |
5468 KB |
Output is correct |
12 |
Correct |
2 ms |
5468 KB |
Output is correct |
13 |
Correct |
2 ms |
5468 KB |
Output is correct |
14 |
Correct |
2 ms |
5468 KB |
Output is correct |
15 |
Correct |
2 ms |
5468 KB |
Output is correct |
16 |
Correct |
2 ms |
5656 KB |
Output is correct |
17 |
Correct |
3 ms |
5724 KB |
Output is correct |
18 |
Correct |
4 ms |
5820 KB |
Output is correct |
19 |
Correct |
4 ms |
5724 KB |
Output is correct |
20 |
Correct |
3 ms |
5620 KB |
Output is correct |
21 |
Correct |
2 ms |
5724 KB |
Output is correct |
22 |
Correct |
3 ms |
5724 KB |
Output is correct |
23 |
Correct |
3 ms |
5724 KB |
Output is correct |
24 |
Correct |
3 ms |
5724 KB |
Output is correct |
25 |
Correct |
2 ms |
5928 KB |
Output is correct |
26 |
Correct |
3 ms |
5724 KB |
Output is correct |
27 |
Correct |
2 ms |
5724 KB |
Output is correct |
28 |
Correct |
3 ms |
5724 KB |
Output is correct |
29 |
Correct |
3 ms |
5724 KB |
Output is correct |
30 |
Correct |
3 ms |
5724 KB |
Output is correct |
31 |
Correct |
2 ms |
5724 KB |
Output is correct |
32 |
Correct |
3 ms |
5724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5468 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
2 ms |
5468 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
2 ms |
5468 KB |
Output is correct |
8 |
Correct |
2 ms |
5688 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
2 ms |
5468 KB |
Output is correct |
12 |
Correct |
2 ms |
5468 KB |
Output is correct |
13 |
Correct |
2 ms |
5468 KB |
Output is correct |
14 |
Correct |
2 ms |
5468 KB |
Output is correct |
15 |
Correct |
2 ms |
5468 KB |
Output is correct |
16 |
Correct |
2 ms |
5656 KB |
Output is correct |
17 |
Correct |
3 ms |
5724 KB |
Output is correct |
18 |
Correct |
4 ms |
5820 KB |
Output is correct |
19 |
Correct |
4 ms |
5724 KB |
Output is correct |
20 |
Correct |
3 ms |
5620 KB |
Output is correct |
21 |
Correct |
2 ms |
5724 KB |
Output is correct |
22 |
Correct |
3 ms |
5724 KB |
Output is correct |
23 |
Correct |
3 ms |
5724 KB |
Output is correct |
24 |
Correct |
3 ms |
5724 KB |
Output is correct |
25 |
Correct |
2 ms |
5928 KB |
Output is correct |
26 |
Correct |
3 ms |
5724 KB |
Output is correct |
27 |
Correct |
2 ms |
5724 KB |
Output is correct |
28 |
Correct |
3 ms |
5724 KB |
Output is correct |
29 |
Correct |
3 ms |
5724 KB |
Output is correct |
30 |
Correct |
3 ms |
5724 KB |
Output is correct |
31 |
Correct |
2 ms |
5724 KB |
Output is correct |
32 |
Correct |
3 ms |
5724 KB |
Output is correct |
33 |
Correct |
102 ms |
14532 KB |
Output is correct |
34 |
Correct |
57 ms |
15364 KB |
Output is correct |
35 |
Correct |
83 ms |
12368 KB |
Output is correct |
36 |
Correct |
141 ms |
20772 KB |
Output is correct |
37 |
Correct |
16 ms |
10324 KB |
Output is correct |
38 |
Correct |
177 ms |
22344 KB |
Output is correct |
39 |
Correct |
165 ms |
22360 KB |
Output is correct |
40 |
Correct |
176 ms |
22372 KB |
Output is correct |
41 |
Correct |
190 ms |
22336 KB |
Output is correct |
42 |
Correct |
148 ms |
22408 KB |
Output is correct |
43 |
Correct |
180 ms |
22340 KB |
Output is correct |
44 |
Correct |
158 ms |
22364 KB |
Output is correct |
45 |
Correct |
164 ms |
22280 KB |
Output is correct |
46 |
Correct |
154 ms |
22308 KB |
Output is correct |
47 |
Correct |
157 ms |
22188 KB |
Output is correct |
48 |
Correct |
46 ms |
18052 KB |
Output is correct |
49 |
Correct |
87 ms |
21100 KB |
Output is correct |
50 |
Correct |
18 ms |
8972 KB |
Output is correct |
51 |
Correct |
32 ms |
11644 KB |
Output is correct |
52 |
Correct |
11 ms |
8796 KB |
Output is correct |
53 |
Correct |
93 ms |
20620 KB |
Output is correct |
54 |
Correct |
58 ms |
12348 KB |
Output is correct |
55 |
Correct |
143 ms |
17412 KB |
Output is correct |
56 |
Correct |
86 ms |
13480 KB |
Output is correct |
57 |
Correct |
110 ms |
19864 KB |
Output is correct |
58 |
Correct |
19 ms |
12248 KB |
Output is correct |
59 |
Correct |
22 ms |
11348 KB |
Output is correct |
60 |
Correct |
60 ms |
19540 KB |
Output is correct |
61 |
Correct |
62 ms |
20336 KB |
Output is correct |
62 |
Correct |
42 ms |
17140 KB |
Output is correct |
63 |
Correct |
35 ms |
15700 KB |
Output is correct |
64 |
Correct |
38 ms |
17692 KB |
Output is correct |
65 |
Correct |
49 ms |
24660 KB |
Output is correct |
66 |
Correct |
45 ms |
10584 KB |
Output is correct |
67 |
Correct |
52 ms |
19136 KB |
Output is correct |
68 |
Correct |
104 ms |
24972 KB |
Output is correct |
69 |
Correct |
29 ms |
7516 KB |
Output is correct |
70 |
Correct |
6 ms |
5980 KB |
Output is correct |
71 |
Correct |
45 ms |
14420 KB |
Output is correct |
72 |
Correct |
66 ms |
21852 KB |
Output is correct |
73 |
Correct |
130 ms |
28504 KB |
Output is correct |
74 |
Correct |
160 ms |
24864 KB |
Output is correct |
75 |
Correct |
107 ms |
28244 KB |
Output is correct |
76 |
Correct |
99 ms |
26952 KB |
Output is correct |
77 |
Correct |
149 ms |
25172 KB |
Output is correct |