#include "catdog.h"
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n;
vector<int> adj[N];
int t[N];
enum Type{Compress,Rake,AddEdge,AddVertex,Vertex};
struct StaticTopTree{
using P = pair<int,int>;
int root,node_id;
int hv[N],p[N];
int lch[4*N],rch[4*N],par[4*N];
Type type[4*N];
int dfs(int u){
int s=1,mx=0;
for(auto v:adj[u]){
if(v==p[u])continue;
p[v]=u;
int t=dfs(v);
if(t>mx)mx=t,hv[u]=v;
s+=t;
}
return s;
}
int add(int i,int l,int r,Type t){
if(!i)i=++node_id;
lch[i]=l,rch[i]=r,type[i]=t;
if(l)par[l]=i;
if(r)par[r]=i;
return i;
}
P merge(const vector<P> &a,Type t){
if(a.size()==1)return a[0];
int tot=0;
vector<P> b,c;
for(auto [i,s]:a)tot+=s;
for(auto [i,s]:a){
(tot>s?b:c).emplace_back(i,s);
tot-=s*2;
}
auto [i,si]=merge(b,t);
auto [j,sj]=merge(c,t);
return {add(0,i,j,t),si+sj};
}
P compress(int i){
vector<P> a{add_vertex(i)};
while(hv[i])a.emplace_back(add_vertex(i=hv[i]));
return merge(a,Compress);
}
P rake(int i){
vector<P> a;
for(auto j:adj[i])if(j!=p[i]&&j!=hv[i])a.emplace_back(add_edge(j));
return a.empty()?P(0,0):merge(a,Rake);
}
P add_edge(int i){
auto [j,s]=compress(i);
return {add(0,j,0,AddEdge),s};
}
P add_vertex(int i){
auto [j,s]=rake(i);
return {add(i,j,0,j?AddVertex:Vertex),s+1};
}
void build(){
node_id=n;
dfs(1);
root=compress(1).first;
}
}stt;
using Path = array<array<int,2>,2>;
using Point = array<int,2>;
Path path[4*N];
Point point[4*N];
Path compress(Path l,Path r){
Path res;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
res[i][j]=n;
for(int x=0;x<2;x++){
for(int y=0;y<2;y++){
res[i][j]=min(res[i][j],l[i][x]+r[y][j]+(x!=y));
}
}
}
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
for(int x=0;x<2;x++){
for(int y=0;y<2;y++){
res[i][j]=min(res[i][j],res[x][y]+(i!=x)+(j!=y));
}
}
}
}
return res;
}
Point rake(Point l,Point r){
return {l[0]+r[0],l[1]+r[1]};
}
Point add_edge(Path p){
return {min(p[0][0],p[0][1]),min(p[1][0],p[1][1])};
}
Path add_vertex(Point p,int u){
Path res;
for(auto &v:res)v.fill(n);
for(int v=0;v<2;v++){
if(t[u]==-1||t[u]==v){
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
res[i][j]=min(res[i][j],p[v]+(i!=v)+(j!=v));
}
}
}
}
return res;
}
Path vertex(int u){
Path res;
for(auto &v:res)v.fill(n);
for(int v=0;v<2;v++){
if(t[u]==-1||t[u]==v){
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
res[i][j]=min(res[i][j],(i!=v)+(j!=v));
}
}
}
}
return res;
}
void update(int u){
if(stt.type[u]==Compress){
path[u]=compress(path[stt.lch[u]],path[stt.rch[u]]);
}else if(stt.type[u]==Rake){
point[u]=rake(point[stt.lch[u]],point[stt.rch[u]]);
}else if(stt.type[u]==AddEdge){
point[u]=add_edge(path[stt.lch[u]]);
}else if(stt.type[u]==AddVertex){
path[u]=add_vertex(point[stt.lch[u]],u);
}else if(stt.type[u]==Vertex){
path[u]=vertex(u);
}
}
void dfs(int u){
if(!u)return;
dfs(stt.lch[u]);
dfs(stt.rch[u]);
update(u);
}
int solve(int u,int nt){
t[u]=nt;
for(;u;u=stt.par[u]){
update(u);
}
int ans=n;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
ans=min(ans,path[stt.root][i][j]);
}
}
return ans;
}
void initialize(int _n,vector<int> _u,vector<int> _v){
n=_n;
for(int i=0;i<n-1;i++){
int u=_u[i],v=_v[i];
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
fill(t+1,t+n+1,-1);
stt.build();
dfs(stt.root);
}
int cat(int v){
return solve(v,0);
}
int dog(int v){
return solve(v,1);
}
int neighbor(int v){
return solve(v,-1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13136 KB |
Output is correct |
2 |
Correct |
3 ms |
13136 KB |
Output is correct |
3 |
Correct |
2 ms |
13136 KB |
Output is correct |
4 |
Correct |
2 ms |
13136 KB |
Output is correct |
5 |
Correct |
3 ms |
13240 KB |
Output is correct |
6 |
Correct |
2 ms |
13136 KB |
Output is correct |
7 |
Correct |
2 ms |
13136 KB |
Output is correct |
8 |
Correct |
2 ms |
13136 KB |
Output is correct |
9 |
Correct |
3 ms |
13136 KB |
Output is correct |
10 |
Correct |
3 ms |
13104 KB |
Output is correct |
11 |
Correct |
3 ms |
13136 KB |
Output is correct |
12 |
Correct |
3 ms |
13136 KB |
Output is correct |
13 |
Correct |
3 ms |
13136 KB |
Output is correct |
14 |
Correct |
2 ms |
13136 KB |
Output is correct |
15 |
Correct |
2 ms |
13136 KB |
Output is correct |
16 |
Correct |
3 ms |
13136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13136 KB |
Output is correct |
2 |
Correct |
3 ms |
13136 KB |
Output is correct |
3 |
Correct |
2 ms |
13136 KB |
Output is correct |
4 |
Correct |
2 ms |
13136 KB |
Output is correct |
5 |
Correct |
3 ms |
13240 KB |
Output is correct |
6 |
Correct |
2 ms |
13136 KB |
Output is correct |
7 |
Correct |
2 ms |
13136 KB |
Output is correct |
8 |
Correct |
2 ms |
13136 KB |
Output is correct |
9 |
Correct |
3 ms |
13136 KB |
Output is correct |
10 |
Correct |
3 ms |
13104 KB |
Output is correct |
11 |
Correct |
3 ms |
13136 KB |
Output is correct |
12 |
Correct |
3 ms |
13136 KB |
Output is correct |
13 |
Correct |
3 ms |
13136 KB |
Output is correct |
14 |
Correct |
2 ms |
13136 KB |
Output is correct |
15 |
Correct |
2 ms |
13136 KB |
Output is correct |
16 |
Correct |
3 ms |
13136 KB |
Output is correct |
17 |
Correct |
3 ms |
13136 KB |
Output is correct |
18 |
Correct |
3 ms |
13136 KB |
Output is correct |
19 |
Correct |
3 ms |
13136 KB |
Output is correct |
20 |
Correct |
3 ms |
13308 KB |
Output is correct |
21 |
Correct |
3 ms |
13136 KB |
Output is correct |
22 |
Correct |
3 ms |
13136 KB |
Output is correct |
23 |
Correct |
4 ms |
13136 KB |
Output is correct |
24 |
Correct |
3 ms |
13136 KB |
Output is correct |
25 |
Correct |
3 ms |
13136 KB |
Output is correct |
26 |
Correct |
3 ms |
13136 KB |
Output is correct |
27 |
Correct |
3 ms |
13136 KB |
Output is correct |
28 |
Correct |
3 ms |
13388 KB |
Output is correct |
29 |
Correct |
4 ms |
13392 KB |
Output is correct |
30 |
Correct |
3 ms |
13136 KB |
Output is correct |
31 |
Correct |
3 ms |
13392 KB |
Output is correct |
32 |
Correct |
3 ms |
13300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13136 KB |
Output is correct |
2 |
Correct |
3 ms |
13136 KB |
Output is correct |
3 |
Correct |
2 ms |
13136 KB |
Output is correct |
4 |
Correct |
2 ms |
13136 KB |
Output is correct |
5 |
Correct |
3 ms |
13240 KB |
Output is correct |
6 |
Correct |
2 ms |
13136 KB |
Output is correct |
7 |
Correct |
2 ms |
13136 KB |
Output is correct |
8 |
Correct |
2 ms |
13136 KB |
Output is correct |
9 |
Correct |
3 ms |
13136 KB |
Output is correct |
10 |
Correct |
3 ms |
13104 KB |
Output is correct |
11 |
Correct |
3 ms |
13136 KB |
Output is correct |
12 |
Correct |
3 ms |
13136 KB |
Output is correct |
13 |
Correct |
3 ms |
13136 KB |
Output is correct |
14 |
Correct |
2 ms |
13136 KB |
Output is correct |
15 |
Correct |
2 ms |
13136 KB |
Output is correct |
16 |
Correct |
3 ms |
13136 KB |
Output is correct |
17 |
Correct |
3 ms |
13136 KB |
Output is correct |
18 |
Correct |
3 ms |
13136 KB |
Output is correct |
19 |
Correct |
3 ms |
13136 KB |
Output is correct |
20 |
Correct |
3 ms |
13308 KB |
Output is correct |
21 |
Correct |
3 ms |
13136 KB |
Output is correct |
22 |
Correct |
3 ms |
13136 KB |
Output is correct |
23 |
Correct |
4 ms |
13136 KB |
Output is correct |
24 |
Correct |
3 ms |
13136 KB |
Output is correct |
25 |
Correct |
3 ms |
13136 KB |
Output is correct |
26 |
Correct |
3 ms |
13136 KB |
Output is correct |
27 |
Correct |
3 ms |
13136 KB |
Output is correct |
28 |
Correct |
3 ms |
13388 KB |
Output is correct |
29 |
Correct |
4 ms |
13392 KB |
Output is correct |
30 |
Correct |
3 ms |
13136 KB |
Output is correct |
31 |
Correct |
3 ms |
13392 KB |
Output is correct |
32 |
Correct |
3 ms |
13300 KB |
Output is correct |
33 |
Correct |
102 ms |
21688 KB |
Output is correct |
34 |
Correct |
60 ms |
21328 KB |
Output is correct |
35 |
Correct |
111 ms |
19008 KB |
Output is correct |
36 |
Correct |
169 ms |
24204 KB |
Output is correct |
37 |
Correct |
18 ms |
16976 KB |
Output is correct |
38 |
Correct |
216 ms |
26988 KB |
Output is correct |
39 |
Correct |
192 ms |
26936 KB |
Output is correct |
40 |
Correct |
177 ms |
26940 KB |
Output is correct |
41 |
Correct |
181 ms |
26932 KB |
Output is correct |
42 |
Correct |
193 ms |
26932 KB |
Output is correct |
43 |
Correct |
177 ms |
27076 KB |
Output is correct |
44 |
Correct |
163 ms |
26936 KB |
Output is correct |
45 |
Correct |
171 ms |
26936 KB |
Output is correct |
46 |
Correct |
183 ms |
26932 KB |
Output is correct |
47 |
Correct |
184 ms |
26964 KB |
Output is correct |
48 |
Correct |
62 ms |
24080 KB |
Output is correct |
49 |
Correct |
74 ms |
27760 KB |
Output is correct |
50 |
Correct |
27 ms |
17288 KB |
Output is correct |
51 |
Correct |
34 ms |
20660 KB |
Output is correct |
52 |
Correct |
16 ms |
16660 KB |
Output is correct |
53 |
Correct |
102 ms |
25672 KB |
Output is correct |
54 |
Correct |
69 ms |
20404 KB |
Output is correct |
55 |
Correct |
155 ms |
22984 KB |
Output is correct |
56 |
Correct |
111 ms |
21140 KB |
Output is correct |
57 |
Correct |
149 ms |
23624 KB |
Output is correct |
58 |
Correct |
26 ms |
20736 KB |
Output is correct |
59 |
Correct |
29 ms |
20196 KB |
Output is correct |
60 |
Correct |
79 ms |
27044 KB |
Output is correct |
61 |
Correct |
86 ms |
27452 KB |
Output is correct |
62 |
Correct |
49 ms |
23588 KB |
Output is correct |
63 |
Correct |
45 ms |
22888 KB |
Output is correct |
64 |
Correct |
52 ms |
23540 KB |
Output is correct |
65 |
Correct |
69 ms |
26968 KB |
Output is correct |
66 |
Correct |
62 ms |
18172 KB |
Output is correct |
67 |
Correct |
67 ms |
24264 KB |
Output is correct |
68 |
Correct |
123 ms |
27976 KB |
Output is correct |
69 |
Correct |
32 ms |
14600 KB |
Output is correct |
70 |
Correct |
9 ms |
13392 KB |
Output is correct |
71 |
Correct |
61 ms |
21924 KB |
Output is correct |
72 |
Correct |
91 ms |
26140 KB |
Output is correct |
73 |
Correct |
149 ms |
28924 KB |
Output is correct |
74 |
Correct |
161 ms |
28012 KB |
Output is correct |
75 |
Correct |
152 ms |
29896 KB |
Output is correct |
76 |
Correct |
151 ms |
28896 KB |
Output is correct |
77 |
Correct |
146 ms |
28176 KB |
Output is correct |