#include "catdog.h"
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
#define maxn 200005
ll n;
vector<ll> g[maxn];
ll ls[maxn],rs[maxn],root[maxn];
ll tsz = 0;
ll lik[maxn];
ll up[maxn],sub[maxn],in[maxn],csz[maxn],dpt[maxn],parent[maxn];
ll timer = 0;
struct node{
ll a[2][2];
void init(){a[0][0] = a[1][1] = 0; a[0][1] = a[1][0] = llinf;}
node(){init();}
};
node t[4*maxn];
node mrg(node a,node b){
node c;
for(ll i = 0;i<=1;i++){
for(ll j = 0;j<=1;j++){
c.a[i][j] = llinf;
for(ll k = 0;k<=1;k++){
for(ll l = 0;l<=1;l++){
c.a[i][j] = min(c.a[i][j],a.a[i][k]+b.a[l][j] + (k^l));
}
}
}
}
return c;
}
void dfs_siz(ll u,ll par){
sub[u] = 1; dpt[u] = dpt[par] + 1; parent[u] = par;
for(ll s : g[u]) if(s!=par) dfs_siz(s,u),sub[u]+=sub[s];
}
void dfs(ll u,ll par){
if(!up[u]) up[u] = u;
ll smax = 0;
for(ll s : g[u]){
if(s==par) continue;
if(sub[smax]<sub[s]) smax = s;
}
if(smax==0) return;
up[smax] = up[u];
dfs(smax,u);
for(ll s : g[u]){
if(s==par||s==smax) continue;
dfs(s,u);
}
}
void upd(ll &v,ll tl,ll tr,ll i,ll a,ll b){
if(!v) v = ++tsz;
if(tl==tr){t[v].a[0][0]+=a;t[v].a[1][1]+=b;return;}
ll mid = (tl+tr)/2;
if(i<=mid) upd(ls[v],tl,mid,i,a,b);
else upd(rs[v],mid+1,tr,i,a,b);
t[v] = mrg(t[ls[v]],t[rs[v]]);
}
pll val(ll v){
ll a = min(t[v].a[0][0],t[v].a[0][1]);
ll b = min(t[v].a[1][0],t[v].a[1][1]);
a = min(a,b+1);
b = min(b,a+1);
return {a,b};
}
void upd(ll u,ll a,ll b){
ll lb,la,nb,na;
while(u){
pll p = val(root[up[u]]);
la = p.fi; lb = p.sc;
upd(root[up[u]],1,csz[up[u]],dpt[u]-dpt[up[u]]+1,a,b);
p = val(root[up[u]]);
na = p.fi; nb = p.sc;
u = parent[up[u]];
a = na - la; b = nb - lb;
}
}
ll rez(){pll p = val(root[1]); return min(p.fi,p.sc);}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
n = N;
for(ll i = 0;i<n-1;i++){
ll x = A[i],y = B[i];
g[x].pb(y);
g[y].pb(x);
}
dfs_siz(1,0); dfs(1,0);
for(ll i = 1;i<=n;i++) csz[up[i]]++;
}
int cat(int v) {
upd(v,n,0);
lik[v] = 1;
return rez();
}
int dog(int v) {
upd(v,0,n);
lik[v] = 2;
return rez();
}
int neighbor(int v) {
if(lik[v]==1) upd(v,-n,0);
else upd(v,0,-n);
lik[v] = 0;
return rez();
}
/*
5
1 2
2 3
2 4
4 5
5
2 3
1 5
2 2
1 1
3 2
out:
0
1
0
2
1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
30036 KB |
Output is correct |
2 |
Correct |
14 ms |
30140 KB |
Output is correct |
3 |
Correct |
15 ms |
30120 KB |
Output is correct |
4 |
Correct |
13 ms |
30096 KB |
Output is correct |
5 |
Correct |
13 ms |
30068 KB |
Output is correct |
6 |
Correct |
14 ms |
30068 KB |
Output is correct |
7 |
Correct |
14 ms |
30016 KB |
Output is correct |
8 |
Correct |
13 ms |
30052 KB |
Output is correct |
9 |
Correct |
14 ms |
30036 KB |
Output is correct |
10 |
Correct |
13 ms |
30080 KB |
Output is correct |
11 |
Correct |
16 ms |
30096 KB |
Output is correct |
12 |
Correct |
15 ms |
30060 KB |
Output is correct |
13 |
Correct |
13 ms |
30036 KB |
Output is correct |
14 |
Correct |
16 ms |
30084 KB |
Output is correct |
15 |
Correct |
14 ms |
30064 KB |
Output is correct |
16 |
Correct |
15 ms |
30036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
30036 KB |
Output is correct |
2 |
Correct |
14 ms |
30140 KB |
Output is correct |
3 |
Correct |
15 ms |
30120 KB |
Output is correct |
4 |
Correct |
13 ms |
30096 KB |
Output is correct |
5 |
Correct |
13 ms |
30068 KB |
Output is correct |
6 |
Correct |
14 ms |
30068 KB |
Output is correct |
7 |
Correct |
14 ms |
30016 KB |
Output is correct |
8 |
Correct |
13 ms |
30052 KB |
Output is correct |
9 |
Correct |
14 ms |
30036 KB |
Output is correct |
10 |
Correct |
13 ms |
30080 KB |
Output is correct |
11 |
Correct |
16 ms |
30096 KB |
Output is correct |
12 |
Correct |
15 ms |
30060 KB |
Output is correct |
13 |
Correct |
13 ms |
30036 KB |
Output is correct |
14 |
Correct |
16 ms |
30084 KB |
Output is correct |
15 |
Correct |
14 ms |
30064 KB |
Output is correct |
16 |
Correct |
15 ms |
30036 KB |
Output is correct |
17 |
Correct |
14 ms |
30112 KB |
Output is correct |
18 |
Correct |
14 ms |
30240 KB |
Output is correct |
19 |
Correct |
13 ms |
30164 KB |
Output is correct |
20 |
Correct |
13 ms |
30036 KB |
Output is correct |
21 |
Correct |
14 ms |
30164 KB |
Output is correct |
22 |
Correct |
14 ms |
30108 KB |
Output is correct |
23 |
Correct |
14 ms |
30208 KB |
Output is correct |
24 |
Correct |
14 ms |
30204 KB |
Output is correct |
25 |
Correct |
14 ms |
30164 KB |
Output is correct |
26 |
Correct |
14 ms |
30072 KB |
Output is correct |
27 |
Correct |
13 ms |
30168 KB |
Output is correct |
28 |
Correct |
14 ms |
30164 KB |
Output is correct |
29 |
Correct |
15 ms |
30164 KB |
Output is correct |
30 |
Correct |
13 ms |
30036 KB |
Output is correct |
31 |
Correct |
13 ms |
30088 KB |
Output is correct |
32 |
Correct |
13 ms |
30080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
30036 KB |
Output is correct |
2 |
Correct |
14 ms |
30140 KB |
Output is correct |
3 |
Correct |
15 ms |
30120 KB |
Output is correct |
4 |
Correct |
13 ms |
30096 KB |
Output is correct |
5 |
Correct |
13 ms |
30068 KB |
Output is correct |
6 |
Correct |
14 ms |
30068 KB |
Output is correct |
7 |
Correct |
14 ms |
30016 KB |
Output is correct |
8 |
Correct |
13 ms |
30052 KB |
Output is correct |
9 |
Correct |
14 ms |
30036 KB |
Output is correct |
10 |
Correct |
13 ms |
30080 KB |
Output is correct |
11 |
Correct |
16 ms |
30096 KB |
Output is correct |
12 |
Correct |
15 ms |
30060 KB |
Output is correct |
13 |
Correct |
13 ms |
30036 KB |
Output is correct |
14 |
Correct |
16 ms |
30084 KB |
Output is correct |
15 |
Correct |
14 ms |
30064 KB |
Output is correct |
16 |
Correct |
15 ms |
30036 KB |
Output is correct |
17 |
Correct |
14 ms |
30112 KB |
Output is correct |
18 |
Correct |
14 ms |
30240 KB |
Output is correct |
19 |
Correct |
13 ms |
30164 KB |
Output is correct |
20 |
Correct |
13 ms |
30036 KB |
Output is correct |
21 |
Correct |
14 ms |
30164 KB |
Output is correct |
22 |
Correct |
14 ms |
30108 KB |
Output is correct |
23 |
Correct |
14 ms |
30208 KB |
Output is correct |
24 |
Correct |
14 ms |
30204 KB |
Output is correct |
25 |
Correct |
14 ms |
30164 KB |
Output is correct |
26 |
Correct |
14 ms |
30072 KB |
Output is correct |
27 |
Correct |
13 ms |
30168 KB |
Output is correct |
28 |
Correct |
14 ms |
30164 KB |
Output is correct |
29 |
Correct |
15 ms |
30164 KB |
Output is correct |
30 |
Correct |
13 ms |
30036 KB |
Output is correct |
31 |
Correct |
13 ms |
30088 KB |
Output is correct |
32 |
Correct |
13 ms |
30080 KB |
Output is correct |
33 |
Correct |
120 ms |
38544 KB |
Output is correct |
34 |
Correct |
62 ms |
38348 KB |
Output is correct |
35 |
Correct |
124 ms |
36520 KB |
Output is correct |
36 |
Correct |
193 ms |
44132 KB |
Output is correct |
37 |
Correct |
27 ms |
33644 KB |
Output is correct |
38 |
Correct |
242 ms |
45576 KB |
Output is correct |
39 |
Correct |
235 ms |
45636 KB |
Output is correct |
40 |
Correct |
223 ms |
45636 KB |
Output is correct |
41 |
Correct |
228 ms |
45676 KB |
Output is correct |
42 |
Correct |
224 ms |
45580 KB |
Output is correct |
43 |
Correct |
224 ms |
45648 KB |
Output is correct |
44 |
Correct |
217 ms |
45676 KB |
Output is correct |
45 |
Correct |
220 ms |
45616 KB |
Output is correct |
46 |
Correct |
216 ms |
45588 KB |
Output is correct |
47 |
Correct |
234 ms |
45628 KB |
Output is correct |
48 |
Correct |
60 ms |
39868 KB |
Output is correct |
49 |
Correct |
67 ms |
42096 KB |
Output is correct |
50 |
Correct |
30 ms |
32756 KB |
Output is correct |
51 |
Correct |
35 ms |
34832 KB |
Output is correct |
52 |
Correct |
24 ms |
32492 KB |
Output is correct |
53 |
Correct |
117 ms |
42400 KB |
Output is correct |
54 |
Correct |
71 ms |
36000 KB |
Output is correct |
55 |
Correct |
166 ms |
41016 KB |
Output is correct |
56 |
Correct |
99 ms |
36836 KB |
Output is correct |
57 |
Correct |
128 ms |
41480 KB |
Output is correct |
58 |
Correct |
30 ms |
34856 KB |
Output is correct |
59 |
Correct |
35 ms |
34488 KB |
Output is correct |
60 |
Correct |
65 ms |
40812 KB |
Output is correct |
61 |
Correct |
67 ms |
41360 KB |
Output is correct |
62 |
Correct |
52 ms |
38852 KB |
Output is correct |
63 |
Correct |
46 ms |
37240 KB |
Output is correct |
64 |
Correct |
55 ms |
38692 KB |
Output is correct |
65 |
Correct |
64 ms |
43560 KB |
Output is correct |
66 |
Correct |
59 ms |
33872 KB |
Output is correct |
67 |
Correct |
64 ms |
39672 KB |
Output is correct |
68 |
Correct |
106 ms |
43980 KB |
Output is correct |
69 |
Correct |
34 ms |
31572 KB |
Output is correct |
70 |
Correct |
20 ms |
30276 KB |
Output is correct |
71 |
Correct |
56 ms |
36572 KB |
Output is correct |
72 |
Correct |
75 ms |
41760 KB |
Output is correct |
73 |
Correct |
141 ms |
46796 KB |
Output is correct |
74 |
Correct |
149 ms |
43880 KB |
Output is correct |
75 |
Correct |
123 ms |
46796 KB |
Output is correct |
76 |
Correct |
120 ms |
45672 KB |
Output is correct |
77 |
Correct |
151 ms |
44284 KB |
Output is correct |