#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5, inf=1e9;
int n, pa[nx], hd[nx], hv[nx], in[nx], out[nx], t, sz[nx], type[nx];
vector<int> d[nx];
struct segtree
{
struct node
{
int dp[2][2];
node() {dp[0][1]=dp[1][0]=inf, dp[0][0]=dp[1][1]=0;}
friend node operator+ (const node &lhs, const node &rhs)
{
node tmp;
tmp.dp[0][0]=tmp.dp[0][1]=tmp.dp[1][0]=tmp.dp[1][1]=inf;
for (int i=0; i<2; i++) for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) tmp.dp[i][j]=min(tmp.dp[i][j], lhs.dp[i][k]+rhs.dp[l][j]+(k!=l));
return tmp;
}
} d[4*nx];
void update(int l, int r, int i, int idx, int x, int y)
{
if (idx<l||r<idx) return;
if (l==r) return d[i].dp[0][0]+=x, d[i].dp[1][1]+=y, void();
int md=(l+r)/2;
update(l, md, 2*i, idx, x, y);
update(md+1, r, 2*i+1, idx, x, y);
d[i]=d[2*i]+d[2*i+1];
}
node query(int l, int r, int i, int ql, int qr)
{
if (qr<l||r<ql) return node();
if (ql<=l&&r<=qr) return d[i];
int md=(l+r)/2;
return query(l, md, 2*i, ql, qr)+query(md+1, r, 2*i+1, ql, qr);
}
pair<int, int> getrange(int u)
{
node tmp=query(1, n, 1, in[u], out[u]);
int cat=min(tmp.dp[0][0], tmp.dp[0][1]), dog=min(tmp.dp[1][0], tmp.dp[1][1]);
return {min(cat, dog+1), min(cat+1, dog)};
}
} s;
void dfssz(int u, int p)
{
sz[u]=1;
pa[u]=p;
for (auto v:d[u]) if (v!=p) dfssz(v, u), sz[u]+=sz[v], hv[u]=(sz[hv[u]]>sz[v])?hv[u]:v;
}
void hld(int u, int p, int g)
{
hd[u]=g;
in[u]=++t;
out[g]=t;
if (hv[u]) hld(hv[u], u, g);
for (auto v:d[u]) if (v!=p&&v!=hv[u]) hld(v, u, v);
}
void update(int u, int x, int y)
{
while (1)
{
//cout<<"debug "<<u<<' '<<hd[u]<<'\n';
auto prev=s.getrange(hd[u]);
//cout<<"euler "<<in[hd[u]]<<' '<<out[hd[u]]<<'\n';
//cout<<"prev "<<prev.first<<' '<<prev.second<<'\n';
s.update(1, n, 1, in[u], x, y);
auto cur=s.getrange(hd[u]);
//cout<<"after "<<cur.first<<' '<<cur.second<<'\n';
x=cur.first-prev.first, y=cur.second-prev.second;
if (pa[hd[u]]==-1) break;
u=pa[hd[u]];
}
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
n=N;
for (int i=0; i<n-1; i++) d[A[i]].push_back(B[i]), d[B[i]].push_back(A[i]);
dfssz(1, -1);
//for (int i=1; i<=n; i++) cout<<"here "<<i<<' '<<hv[i]<<' '<<pa[i]<<'\n';
hld(1, 1, 1);
//for (int i=1; i<=n; i++) cout<<"hd "<<i<<' '<<hd[i]<<'\n';
}
int cat(int v) {
type[v]=1;
update(v, 0, inf);
auto res=s.getrange(1);
return min(res.first, res.second);
}
int dog(int v) {
type[v]=2;
update(v, inf, 0);
auto res=s.getrange(1);
return min(res.first, res.second);
}
int neighbor(int v) {
if (type[v]==1) update(v, 0, -inf);
else update(v, -inf, 0);
auto res=s.getrange(1);
return min(res.first, res.second);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9040 KB |
Output is correct |
2 |
Correct |
6 ms |
9040 KB |
Output is correct |
3 |
Correct |
6 ms |
9040 KB |
Output is correct |
4 |
Correct |
6 ms |
8872 KB |
Output is correct |
5 |
Correct |
7 ms |
9052 KB |
Output is correct |
6 |
Correct |
6 ms |
9040 KB |
Output is correct |
7 |
Correct |
7 ms |
9040 KB |
Output is correct |
8 |
Correct |
6 ms |
9040 KB |
Output is correct |
9 |
Correct |
6 ms |
9040 KB |
Output is correct |
10 |
Correct |
6 ms |
9040 KB |
Output is correct |
11 |
Correct |
6 ms |
9040 KB |
Output is correct |
12 |
Correct |
6 ms |
9208 KB |
Output is correct |
13 |
Correct |
6 ms |
8876 KB |
Output is correct |
14 |
Correct |
8 ms |
9040 KB |
Output is correct |
15 |
Correct |
6 ms |
9040 KB |
Output is correct |
16 |
Correct |
6 ms |
8880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9040 KB |
Output is correct |
2 |
Correct |
6 ms |
9040 KB |
Output is correct |
3 |
Correct |
6 ms |
9040 KB |
Output is correct |
4 |
Correct |
6 ms |
8872 KB |
Output is correct |
5 |
Correct |
7 ms |
9052 KB |
Output is correct |
6 |
Correct |
6 ms |
9040 KB |
Output is correct |
7 |
Correct |
7 ms |
9040 KB |
Output is correct |
8 |
Correct |
6 ms |
9040 KB |
Output is correct |
9 |
Correct |
6 ms |
9040 KB |
Output is correct |
10 |
Correct |
6 ms |
9040 KB |
Output is correct |
11 |
Correct |
6 ms |
9040 KB |
Output is correct |
12 |
Correct |
6 ms |
9208 KB |
Output is correct |
13 |
Correct |
6 ms |
8876 KB |
Output is correct |
14 |
Correct |
8 ms |
9040 KB |
Output is correct |
15 |
Correct |
6 ms |
9040 KB |
Output is correct |
16 |
Correct |
6 ms |
8880 KB |
Output is correct |
17 |
Correct |
9 ms |
9040 KB |
Output is correct |
18 |
Correct |
8 ms |
9040 KB |
Output is correct |
19 |
Correct |
7 ms |
9040 KB |
Output is correct |
20 |
Correct |
7 ms |
8960 KB |
Output is correct |
21 |
Correct |
7 ms |
9040 KB |
Output is correct |
22 |
Correct |
12 ms |
9040 KB |
Output is correct |
23 |
Correct |
10 ms |
9040 KB |
Output is correct |
24 |
Correct |
10 ms |
9040 KB |
Output is correct |
25 |
Correct |
13 ms |
9040 KB |
Output is correct |
26 |
Correct |
9 ms |
9040 KB |
Output is correct |
27 |
Correct |
7 ms |
9040 KB |
Output is correct |
28 |
Correct |
8 ms |
9040 KB |
Output is correct |
29 |
Correct |
8 ms |
9040 KB |
Output is correct |
30 |
Correct |
8 ms |
9000 KB |
Output is correct |
31 |
Correct |
7 ms |
9040 KB |
Output is correct |
32 |
Correct |
7 ms |
9040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9040 KB |
Output is correct |
2 |
Correct |
6 ms |
9040 KB |
Output is correct |
3 |
Correct |
6 ms |
9040 KB |
Output is correct |
4 |
Correct |
6 ms |
8872 KB |
Output is correct |
5 |
Correct |
7 ms |
9052 KB |
Output is correct |
6 |
Correct |
6 ms |
9040 KB |
Output is correct |
7 |
Correct |
7 ms |
9040 KB |
Output is correct |
8 |
Correct |
6 ms |
9040 KB |
Output is correct |
9 |
Correct |
6 ms |
9040 KB |
Output is correct |
10 |
Correct |
6 ms |
9040 KB |
Output is correct |
11 |
Correct |
6 ms |
9040 KB |
Output is correct |
12 |
Correct |
6 ms |
9208 KB |
Output is correct |
13 |
Correct |
6 ms |
8876 KB |
Output is correct |
14 |
Correct |
8 ms |
9040 KB |
Output is correct |
15 |
Correct |
6 ms |
9040 KB |
Output is correct |
16 |
Correct |
6 ms |
8880 KB |
Output is correct |
17 |
Correct |
9 ms |
9040 KB |
Output is correct |
18 |
Correct |
8 ms |
9040 KB |
Output is correct |
19 |
Correct |
7 ms |
9040 KB |
Output is correct |
20 |
Correct |
7 ms |
8960 KB |
Output is correct |
21 |
Correct |
7 ms |
9040 KB |
Output is correct |
22 |
Correct |
12 ms |
9040 KB |
Output is correct |
23 |
Correct |
10 ms |
9040 KB |
Output is correct |
24 |
Correct |
10 ms |
9040 KB |
Output is correct |
25 |
Correct |
13 ms |
9040 KB |
Output is correct |
26 |
Correct |
9 ms |
9040 KB |
Output is correct |
27 |
Correct |
7 ms |
9040 KB |
Output is correct |
28 |
Correct |
8 ms |
9040 KB |
Output is correct |
29 |
Correct |
8 ms |
9040 KB |
Output is correct |
30 |
Correct |
8 ms |
9000 KB |
Output is correct |
31 |
Correct |
7 ms |
9040 KB |
Output is correct |
32 |
Correct |
7 ms |
9040 KB |
Output is correct |
33 |
Correct |
511 ms |
14784 KB |
Output is correct |
34 |
Correct |
139 ms |
14664 KB |
Output is correct |
35 |
Correct |
456 ms |
13632 KB |
Output is correct |
36 |
Correct |
748 ms |
18244 KB |
Output is correct |
37 |
Correct |
19 ms |
11600 KB |
Output is correct |
38 |
Correct |
902 ms |
19212 KB |
Output is correct |
39 |
Correct |
853 ms |
19368 KB |
Output is correct |
40 |
Correct |
880 ms |
19376 KB |
Output is correct |
41 |
Correct |
892 ms |
19268 KB |
Output is correct |
42 |
Correct |
821 ms |
19376 KB |
Output is correct |
43 |
Correct |
768 ms |
19256 KB |
Output is correct |
44 |
Correct |
691 ms |
19380 KB |
Output is correct |
45 |
Correct |
733 ms |
19380 KB |
Output is correct |
46 |
Correct |
780 ms |
19368 KB |
Output is correct |
47 |
Correct |
856 ms |
19512 KB |
Output is correct |
48 |
Correct |
259 ms |
16272 KB |
Output is correct |
49 |
Correct |
267 ms |
17836 KB |
Output is correct |
50 |
Correct |
115 ms |
11088 KB |
Output is correct |
51 |
Correct |
111 ms |
12460 KB |
Output is correct |
52 |
Correct |
45 ms |
10832 KB |
Output is correct |
53 |
Correct |
314 ms |
17736 KB |
Output is correct |
54 |
Correct |
269 ms |
13052 KB |
Output is correct |
55 |
Correct |
709 ms |
16664 KB |
Output is correct |
56 |
Correct |
421 ms |
14016 KB |
Output is correct |
57 |
Correct |
564 ms |
17540 KB |
Output is correct |
58 |
Correct |
36 ms |
12492 KB |
Output is correct |
59 |
Correct |
102 ms |
12360 KB |
Output is correct |
60 |
Correct |
220 ms |
16976 KB |
Output is correct |
61 |
Correct |
240 ms |
17456 KB |
Output is correct |
62 |
Correct |
141 ms |
15312 KB |
Output is correct |
63 |
Correct |
61 ms |
15952 KB |
Output is correct |
64 |
Correct |
75 ms |
17736 KB |
Output is correct |
65 |
Correct |
83 ms |
22344 KB |
Output is correct |
66 |
Correct |
106 ms |
12684 KB |
Output is correct |
67 |
Correct |
89 ms |
18504 KB |
Output is correct |
68 |
Correct |
185 ms |
22856 KB |
Output is correct |
69 |
Correct |
53 ms |
10568 KB |
Output is correct |
70 |
Correct |
16 ms |
9348 KB |
Output is correct |
71 |
Correct |
89 ms |
15576 KB |
Output is correct |
72 |
Correct |
118 ms |
20872 KB |
Output is correct |
73 |
Correct |
337 ms |
26092 KB |
Output is correct |
74 |
Correct |
355 ms |
22620 KB |
Output is correct |
75 |
Correct |
222 ms |
25928 KB |
Output is correct |
76 |
Correct |
227 ms |
24648 KB |
Output is correct |
77 |
Correct |
379 ms |
22860 KB |
Output is correct |