#include"catdog.h"
#include<iostream>
#include<vector>
#include<algorithm>
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define semicolon ;
#define ryan bear
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
struct co
{
int v[2][2];
co(int x=0,int y=0,int z=0,int w=0)
{
v[0][0]=x;
v[0][1]=y;
v[1][0]=z;
v[1][1]=w;
}
inline co operator^(const co&x)const
{
if(v[0][0]==-inf)
return x;
if(x.v[0][0]==-inf)
return*this;
co ret;
int i,j,k,l;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
ret.v[i][j]=inf;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<2;k++)
for(l=0;l<2;l++)
ret.v[i][l]=min(ret.v[i][l],v[i][j]+x.v[k][l]+(j==k?0:1));
return ret;
}
};
struct seg
{
co t[400010];
void init(int n,int s,int e)
{
if(s==e)
{
t[n]=co(0,inf,inf,0);
return;
}
int m=s+e>>1;
init(n*2,s,m);
init(n*2+1,m+1,e);
t[n]=t[n*2]^t[n*2+1];
return;
}
void si(int n,int s,int e,int x,pi p)
{
if(s==e)
{
t[n].v[0][0]+=p.fi;
t[n].v[1][1]+=p.se;
return;
}
int m=s+e>>1;
if(x>m)
si(n*2+1,m+1,e,x,p);
else
si(n*2,s,m,x,p);
t[n]=t[n*2]^t[n*2+1];
return;
}
co sq(int n,int s,int e,int S,int E)
{
if(s>E||S>e)
return co(-inf);
if(S<=s&&e<=E)
return t[n];
int m=s+e>>1;
return sq(n*2,s,m,S,E)^sq(n*2+1,m+1,e,S,E);
}
}st;
vector<int>adj[100010];
int pa[100010];
int sz[100010];
void dfs(int x)
{
sz[x]=1;
for(int t:adj[x])
{
adj[t].erase(find(all(adj[t]),x));
pa[t]=x;
dfs(t);
sz[x]+=sz[t];
}
sort(all(adj[x]),[=](const int&x,const int&y){return sz[x]>sz[y];});
return;
}
int in[100010];
int cn;
int chn[100010];
int top[100010],bot[100010];
int cct;
void hld(int x)
{
in[x]=++cn;
if(adj[x].empty())
{
bot[chn[x]]=x;
return;
}
int&f=adj[x][0];
chn[f]=chn[x];
hld(f);
for(int i=1;i<adj[x].size();i++)
{
int&t=adj[x][i];
chn[t]=++cct;
top[cct]=t;
hld(t);
}
return;
}
int n;
inline void upd(int x,pi p)
{
while(x>0)
{
st.si(1,1,n,in[x],p);
co rt=st.sq(1,1,n,top[chn[x]],bot[chn[x]]);
int t0=min(rt.v[0][0],rt.v[0][1]);
int t1=min(rt.v[1][0],rt.v[1][1]);
p=pi(min(t0,t1+1),min(t1,t0+1));
x=pa[top[chn[x]]];
}
return;
}
inline int ans()
{
co a=st.sq(1,1,n,1,in[bot[0]]);
return min({a.v[0][0],a.v[0][1],a.v[1][0],a.v[1][1]});
}
void initialize(int N,vector<int>A,vector<int>B)
{
n=N;
for(int i=0;i<n-1;i++)
adj[A[i]].eb(B[i]),adj[B[i]].eb(A[i]);
dfs(1);
top[0]=1;
hld(1);
st.init(1,1,n);
return;
}
bool tp[100010];
int cat(int v)
{
tp[v]=0;
upd(v,pi(0,inf));
return ans();
}
int dog(int v)
{
tp[v]=1;
upd(v,pi(inf,0));
return ans();
}
int neighbor(int v)
{
upd(v,tp[v]?pi(-inf,0):pi(0,-inf));
return ans();
}
Compilation message
catdog.cpp: In member function 'void seg::init(int, int, int)':
catdog.cpp:55:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m=s+e>>1;
~^~
catdog.cpp: In member function 'void seg::si(int, int, int, int, pi)':
catdog.cpp:69:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m=s+e>>1;
~^~
catdog.cpp: In member function 'co seg::sq(int, int, int, int, int)':
catdog.cpp:83:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m=s+e>>1;
~^~
catdog.cpp: In function 'void hld(int)':
catdog.cpp:119:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<adj[x].size();i++)
~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8952 KB |
Output is correct |
2 |
Incorrect |
10 ms |
8956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8952 KB |
Output is correct |
2 |
Incorrect |
10 ms |
8956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8952 KB |
Output is correct |
2 |
Incorrect |
10 ms |
8956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |