#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
using vi=vector<int>;
#define PB push_back
using pi=pair<int,int>;
const ll inf64=LLONG_MAX/3;
template<class T,class U>
void chmin(T&t,const U&u){
if(t>u)t=u;
}
template<class T,class U>
void chmax(T&t,const U&u){
if(t<u)t=u;
}
struct SegTree{
struct Node{
ll cost[2][2];
};
vector<Node> buf;
int s;
void Init(int n){
s=1;
while(s<n)s*=2;
buf.resize(s*2);
FOR(i,s,s*2){
buf[i].cost[0][0]=0;
buf[i].cost[1][1]=0;
buf[i].cost[0][1]=inf64;
buf[i].cost[1][0]=inf64;
}
for(int i=s-1;i>=1;i--)
Update(i);
}
void Update(int i){
REP(a,2)REP(b,2)
buf[i].cost[a][b]=inf64;
REP(a,2)REP(b,2)REP(c,2)REP(d,2)
chmin(buf[i].cost[a][d],buf[i*2].cost[a][b]+(b^c)+buf[i*2+1].cost[c][d]);
}
void Add(int i,int v0,int v1){
i+=s;
buf[i].cost[0][0]+=v0;
buf[i].cost[1][1]+=v1;
while(i>>=1)
Update(i);
}
void Get(int&v0,int&v1){
int a=min(buf[1].cost[0][0],buf[1].cost[0][1]);
int b=min(buf[1].cost[1][0],buf[1].cost[1][1]);
v0=min(a,b+1);
v1=min(a+1,b);
}
};
const int Nmax=100010;
int n,par[Nmax],sub[Nmax],grp[Nmax],pos[Nmax],gsz[Nmax],gpar[Nmax],gord,state[Nmax];
vi tr[Nmax];
SegTree segs[Nmax];
int dfs1(int v,int p){
par[v]=p;
sub[v]=1;
for(auto to:tr[v])if(to!=p)
sub[v]+=dfs1(to,v);
return sub[v];
}
void dfs2(int v,int p,int g){
grp[v]=g;
if(gsz[g]==0)
gpar[g]=p;
pos[v]=gsz[g]++;
pi si(0,-1);
for(auto to:tr[v])if(to!=p)
chmax(si,pi(sub[to],to));
for(auto to:tr[v])if(to!=p){
if(to==si.second)
dfs2(to,v,g);
else
dfs2(to,v,gord++);
}
}
void initialize(int N,vi A,vi B){
n=N;
REP(i,n-1){
tr[A[i]-1].PB(B[i]-1);
tr[B[i]-1].PB(A[i]-1);
}
dfs1(0,-1);
dfs2(0,-1,gord++);
REP(i,gord)
segs[i].Init(gsz[i]);
}
int update(int v,int v0,int v1){
while(v!=-1){
int g=grp[v],cur0,cur1,nx0,nx1;
segs[g].Get(cur0,cur1);
segs[g].Add(pos[v],v0,v1);
segs[g].Get(nx0,nx1);
v0=nx0-cur0;
v1=nx1-cur1;
v=gpar[g];
}
int a,b;
segs[0].Get(a,b);
return min(a,b);
}
int cat(int v){
v--;
int ret=update(v,Nmax,0);
state[v]=1;
return ret;
}
int dog(int v){
v--;
int ret=update(v,0,Nmax);
state[v]=2;
return ret;
}
int neighbor(int v){
v--;
int ret;
if(state[v]==1)
ret=update(v,-Nmax,0);
else
ret=update(v,0,-Nmax);
state[v]=0;
return ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5724 KB |
Output is correct |
2 |
Correct |
3 ms |
5724 KB |
Output is correct |
3 |
Correct |
3 ms |
5928 KB |
Output is correct |
4 |
Correct |
3 ms |
5724 KB |
Output is correct |
5 |
Correct |
3 ms |
5724 KB |
Output is correct |
6 |
Correct |
3 ms |
5724 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
3 ms |
5724 KB |
Output is correct |
9 |
Correct |
3 ms |
5928 KB |
Output is correct |
10 |
Correct |
3 ms |
5724 KB |
Output is correct |
11 |
Correct |
3 ms |
5724 KB |
Output is correct |
12 |
Correct |
3 ms |
5724 KB |
Output is correct |
13 |
Correct |
3 ms |
5724 KB |
Output is correct |
14 |
Correct |
3 ms |
5920 KB |
Output is correct |
15 |
Correct |
2 ms |
5724 KB |
Output is correct |
16 |
Correct |
3 ms |
5720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5724 KB |
Output is correct |
2 |
Correct |
3 ms |
5724 KB |
Output is correct |
3 |
Correct |
3 ms |
5928 KB |
Output is correct |
4 |
Correct |
3 ms |
5724 KB |
Output is correct |
5 |
Correct |
3 ms |
5724 KB |
Output is correct |
6 |
Correct |
3 ms |
5724 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
3 ms |
5724 KB |
Output is correct |
9 |
Correct |
3 ms |
5928 KB |
Output is correct |
10 |
Correct |
3 ms |
5724 KB |
Output is correct |
11 |
Correct |
3 ms |
5724 KB |
Output is correct |
12 |
Correct |
3 ms |
5724 KB |
Output is correct |
13 |
Correct |
3 ms |
5724 KB |
Output is correct |
14 |
Correct |
3 ms |
5920 KB |
Output is correct |
15 |
Correct |
2 ms |
5724 KB |
Output is correct |
16 |
Correct |
3 ms |
5720 KB |
Output is correct |
17 |
Correct |
3 ms |
5980 KB |
Output is correct |
18 |
Correct |
3 ms |
6104 KB |
Output is correct |
19 |
Correct |
3 ms |
5976 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5980 KB |
Output is correct |
24 |
Correct |
4 ms |
5980 KB |
Output is correct |
25 |
Correct |
4 ms |
6236 KB |
Output is correct |
26 |
Correct |
3 ms |
5980 KB |
Output is correct |
27 |
Correct |
3 ms |
5980 KB |
Output is correct |
28 |
Correct |
3 ms |
5980 KB |
Output is correct |
29 |
Correct |
3 ms |
5980 KB |
Output is correct |
30 |
Correct |
3 ms |
5932 KB |
Output is correct |
31 |
Correct |
3 ms |
5980 KB |
Output is correct |
32 |
Correct |
3 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5724 KB |
Output is correct |
2 |
Correct |
3 ms |
5724 KB |
Output is correct |
3 |
Correct |
3 ms |
5928 KB |
Output is correct |
4 |
Correct |
3 ms |
5724 KB |
Output is correct |
5 |
Correct |
3 ms |
5724 KB |
Output is correct |
6 |
Correct |
3 ms |
5724 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
3 ms |
5724 KB |
Output is correct |
9 |
Correct |
3 ms |
5928 KB |
Output is correct |
10 |
Correct |
3 ms |
5724 KB |
Output is correct |
11 |
Correct |
3 ms |
5724 KB |
Output is correct |
12 |
Correct |
3 ms |
5724 KB |
Output is correct |
13 |
Correct |
3 ms |
5724 KB |
Output is correct |
14 |
Correct |
3 ms |
5920 KB |
Output is correct |
15 |
Correct |
2 ms |
5724 KB |
Output is correct |
16 |
Correct |
3 ms |
5720 KB |
Output is correct |
17 |
Correct |
3 ms |
5980 KB |
Output is correct |
18 |
Correct |
3 ms |
6104 KB |
Output is correct |
19 |
Correct |
3 ms |
5976 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5980 KB |
Output is correct |
24 |
Correct |
4 ms |
5980 KB |
Output is correct |
25 |
Correct |
4 ms |
6236 KB |
Output is correct |
26 |
Correct |
3 ms |
5980 KB |
Output is correct |
27 |
Correct |
3 ms |
5980 KB |
Output is correct |
28 |
Correct |
3 ms |
5980 KB |
Output is correct |
29 |
Correct |
3 ms |
5980 KB |
Output is correct |
30 |
Correct |
3 ms |
5932 KB |
Output is correct |
31 |
Correct |
3 ms |
5980 KB |
Output is correct |
32 |
Correct |
3 ms |
5980 KB |
Output is correct |
33 |
Correct |
91 ms |
15364 KB |
Output is correct |
34 |
Correct |
45 ms |
16464 KB |
Output is correct |
35 |
Correct |
83 ms |
12912 KB |
Output is correct |
36 |
Correct |
135 ms |
22208 KB |
Output is correct |
37 |
Correct |
17 ms |
10844 KB |
Output is correct |
38 |
Correct |
151 ms |
23872 KB |
Output is correct |
39 |
Correct |
146 ms |
23868 KB |
Output is correct |
40 |
Correct |
148 ms |
24004 KB |
Output is correct |
41 |
Correct |
150 ms |
23928 KB |
Output is correct |
42 |
Correct |
143 ms |
23872 KB |
Output is correct |
43 |
Correct |
154 ms |
23952 KB |
Output is correct |
44 |
Correct |
158 ms |
23948 KB |
Output is correct |
45 |
Correct |
146 ms |
23976 KB |
Output is correct |
46 |
Correct |
151 ms |
23972 KB |
Output is correct |
47 |
Correct |
145 ms |
23984 KB |
Output is correct |
48 |
Correct |
48 ms |
18840 KB |
Output is correct |
49 |
Correct |
61 ms |
22180 KB |
Output is correct |
50 |
Correct |
18 ms |
9308 KB |
Output is correct |
51 |
Correct |
24 ms |
12212 KB |
Output is correct |
52 |
Correct |
16 ms |
9048 KB |
Output is correct |
53 |
Correct |
87 ms |
22100 KB |
Output is correct |
54 |
Correct |
52 ms |
13448 KB |
Output is correct |
55 |
Correct |
117 ms |
18476 KB |
Output is correct |
56 |
Correct |
76 ms |
14332 KB |
Output is correct |
57 |
Correct |
103 ms |
21396 KB |
Output is correct |
58 |
Correct |
24 ms |
12764 KB |
Output is correct |
59 |
Correct |
23 ms |
11600 KB |
Output is correct |
60 |
Correct |
51 ms |
20304 KB |
Output is correct |
61 |
Correct |
56 ms |
21120 KB |
Output is correct |
62 |
Correct |
39 ms |
17876 KB |
Output is correct |
63 |
Correct |
34 ms |
16208 KB |
Output is correct |
64 |
Correct |
38 ms |
18772 KB |
Output is correct |
65 |
Correct |
51 ms |
28108 KB |
Output is correct |
66 |
Correct |
43 ms |
11408 KB |
Output is correct |
67 |
Correct |
47 ms |
20572 KB |
Output is correct |
68 |
Correct |
88 ms |
26432 KB |
Output is correct |
69 |
Correct |
22 ms |
7880 KB |
Output is correct |
70 |
Correct |
7 ms |
6236 KB |
Output is correct |
71 |
Correct |
43 ms |
16180 KB |
Output is correct |
72 |
Correct |
63 ms |
24868 KB |
Output is correct |
73 |
Correct |
143 ms |
29144 KB |
Output is correct |
74 |
Correct |
130 ms |
26280 KB |
Output is correct |
75 |
Correct |
103 ms |
29264 KB |
Output is correct |
76 |
Correct |
101 ms |
30036 KB |
Output is correct |
77 |
Correct |
127 ms |
26620 KB |
Output is correct |