#include "catdog.h"
#include <bits/stdc++.h>
#include <cstdio>
#include <cassert>
#include <vector>
#include <stdlib.h>
#define INF 100000005
using namespace std;
int x,tata[200005],niv[200005],nr,valoare[200005],mar[200005],max_fiu[200005];
vector <int> gr[200005];
void dfs(int x,int t,int nivel)
{
tata[x]=t;
niv[x]=nivel;
mar[x]=1;
for (int i=0; i<gr[x].size(); i++)
{
int nod=gr[x][i];
if (nod!=t)
{
dfs(nod,x,nivel+1);
mar[x]+=mar[nod];
if (mar[nod]>mar[max_fiu[x]])
{
max_fiu[x]=nod;
}
}
}
}struct Nod
{
long long cost[2][2];
};
class SegmentTree
{
Nod *arb;
public:
void inline Initeaza(int nr)
{
arb = new Nod [((nr<<4)+1)];
for (int i=1;i<=(nr<<2);i++)
{
arb[i].cost[0][0]=arb[i].cost[1][1]=0;
arb[i].cost[1][0]=arb[i].cost[0][1]=0;
}
}
void inline update(int st,int dr,int nod,int poz, Nod val)
{
int a,b,c,d;
if (st==dr)
{
arb[nod]=val;
return;
}
int mij=(st+dr)/2;
if (poz<=mij)
{
update(st,mij,2*nod,poz,val);
}
else
{
update(mij+1,dr,2*nod+1,poz,val);
}
for (a=0;a<=1;a++)
{
for (b=0;b<=1;b++)
{
arb[nod].cost[a][b]=INF;
}
}
for (a=0;a<=1;a++)
{
for (b=0;b<=1;b++)
{
for (c=0;c<=1;c++)
{
for (d=0;d<=1;d++)
{
arb[nod].cost[a][d]=min(arb[nod].cost[a][d],arb[2*nod].cost[a][b]+arb[2*nod+1].cost[c][d]+(b^c));
}
}
}
}
}
void add(int st,int dr,int nod,int poz,int val1,int val2)
{
int a,b,c,d;
if (st==dr)
{
arb[nod].cost[0][0]+=val1;
arb[nod].cost[1][1]+=val2;
return;
}
int mij=(st+dr)/2;
if (poz<=mij)
{
add(st,mij,2*nod,poz,val1,val2);
}
else
{
add(mij+1,dr,2*nod+1,poz,val1,val2);
}
for (a=0;a<=1;a++)
{
for (b=0;b<=1;b++)
{
arb[nod].cost[a][b]=INF;
}
}
for (a=0;a<=1;a++)
{
for (b=0;b<=1;b++)
{
for (c=0;c<=1;c++)
{
for (d=0;d<=1;d++)
{
arb[nod].cost[a][d]=min(arb[nod].cost[a][d],arb[2*nod].cost[a][b]+arb[2*nod+1].cost[c][d]+(b^c));
}
}
}
}
}
pair <int,int> get()
{
int a,b,v0,v1;
a=min(arb[1].cost[0][0],arb[1].cost[0][1]);
b=min(arb[1].cost[1][0],arb[1].cost[1][1]);
v0=min(a,b+1);
v1=min(b,a+1);
return {v0,v1};
}
}*chains;
int id[200005],cnt[200005],q,primul[200005],poz[200005];
void split (int x,int t)
{
id[x]=q;
cnt[q]++;
poz[x]=cnt[q];
if (primul[q]==0)
{
primul[q]=x;
}
if (max_fiu[x]==0)
{
return;
}
split(max_fiu[x],x);
for (int i=0;i<gr[x].size();i++)
{
int nod=gr[x][i];
if (nod!=t&&nod!=max_fiu[x])
{
q++;
split(nod,x);
}
}
}
int n2;
void initialize(int n, std::vector<int> A, std::vector<int> B)
{
n2=n;
int i;
for (i=0; i<n-1; i++)
{
gr[A[i]].push_back(B[i]);
gr[B[i]].push_back(A[i]);
}
nr=0;
dfs(1,0,1);
q=1;
split(1,0);
chains = new SegmentTree [(q+1)];
for (i=1;i<=q;i++)
{
chains[i].Initeaza(cnt[i]);
}
Nod valint;
valint.cost[0][0]=valint.cost[1][1]=0;
valint.cost[0][1]=valint.cost[1][0]=INF;
for (i=1;i<=n;i++)
{
chains[id[i]].update(1,cnt[id[i]],1,poz[i],valint);
}
}
pair <int,int> salut;
void update(int x,int v0,int v1)
{
while (x)
{
pair <int,int> inainte,acum;
inainte=chains[id[x]].get();
chains[id[x]].add(1,cnt[id[x]],1,poz[x],v0,v1);
acum=chains[id[x]].get();
v0=acum.first-inainte.first;
v1=acum.second-inainte.second;
x=tata[primul[id[x]]];
}
}
int cat(int v)
{
valoare[v]=1;
update(v,0,INF);
salut=chains[1].get();
return min(salut.first,salut.second);
}
int dog(int v)
{
valoare[v]=2;
update(v,INF,0);
salut=chains[1].get();
return min(salut.first,salut.second);
}
int neighbor(int v)
{
if (valoare[v]==1)
{
update(v,0,-INF);
salut=chains[1].get();
valoare[v]=0;
return min(salut.first,salut.second);
}
else
if (valoare[v]==2)
{
update(v,-INF,0);
salut=chains[1].get();
valoare[v]=0;
return min(salut.first,salut.second);
}
return 0;
}
/*int readInt(){
int i;
if(scanf("%d",&i)!=1){
fprintf(stderr,"Error while reading input\n");
exit(1);
}
return i;
}
int main(){
int N=readInt();
std::vector<int> A(N-1),B(N-1);
for(int i=0;i<N-1;i++)
{
A[i]=readInt();
B[i]=readInt();
}
int Q;
assert(scanf("%d",&Q)==1);
std::vector <int> T(Q),V(Q);
for(int i=0;i<Q;i++)
{
T[i]=readInt();
V[i]=readInt();
}
initialize(N,A,B);
std::vector<int> res(Q);
for(int j=0;j<Q;j++)
{
if(T[j]==1) res[j]=cat(V[j]);
else if(T[j]==2) res[j]=dog(V[j]);
else res[j]=neighbor(V[j]);
}
for(int j=0;j<Q;j++)
printf("%d\n",res[j]);
return 0;
}*/
Compilation message
catdog.cpp: In function 'void dfs(int, int, int)':
catdog.cpp:17:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (int i=0; i<gr[x].size(); i++)
| ~^~~~~~~~~~~~~
catdog.cpp: In function 'void split(int, int)':
catdog.cpp:149:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
149 | for (int i=0;i<gr[x].size();i++)
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4972 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4972 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
5452 KB |
Output is correct |
18 |
Correct |
5 ms |
5580 KB |
Output is correct |
19 |
Correct |
4 ms |
5452 KB |
Output is correct |
20 |
Correct |
3 ms |
5196 KB |
Output is correct |
21 |
Correct |
4 ms |
5196 KB |
Output is correct |
22 |
Correct |
3 ms |
5196 KB |
Output is correct |
23 |
Correct |
4 ms |
5452 KB |
Output is correct |
24 |
Correct |
6 ms |
5452 KB |
Output is correct |
25 |
Correct |
4 ms |
5196 KB |
Output is correct |
26 |
Correct |
4 ms |
5196 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5196 KB |
Output is correct |
29 |
Correct |
4 ms |
5196 KB |
Output is correct |
30 |
Correct |
3 ms |
5196 KB |
Output is correct |
31 |
Correct |
3 ms |
5452 KB |
Output is correct |
32 |
Correct |
3 ms |
5164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4972 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
5452 KB |
Output is correct |
18 |
Correct |
5 ms |
5580 KB |
Output is correct |
19 |
Correct |
4 ms |
5452 KB |
Output is correct |
20 |
Correct |
3 ms |
5196 KB |
Output is correct |
21 |
Correct |
4 ms |
5196 KB |
Output is correct |
22 |
Correct |
3 ms |
5196 KB |
Output is correct |
23 |
Correct |
4 ms |
5452 KB |
Output is correct |
24 |
Correct |
6 ms |
5452 KB |
Output is correct |
25 |
Correct |
4 ms |
5196 KB |
Output is correct |
26 |
Correct |
4 ms |
5196 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5196 KB |
Output is correct |
29 |
Correct |
4 ms |
5196 KB |
Output is correct |
30 |
Correct |
3 ms |
5196 KB |
Output is correct |
31 |
Correct |
3 ms |
5452 KB |
Output is correct |
32 |
Correct |
3 ms |
5164 KB |
Output is correct |
33 |
Correct |
161 ms |
34676 KB |
Output is correct |
34 |
Correct |
92 ms |
42564 KB |
Output is correct |
35 |
Correct |
119 ms |
25860 KB |
Output is correct |
36 |
Correct |
217 ms |
58592 KB |
Output is correct |
37 |
Correct |
29 ms |
22468 KB |
Output is correct |
38 |
Correct |
307 ms |
64904 KB |
Output is correct |
39 |
Correct |
250 ms |
64960 KB |
Output is correct |
40 |
Correct |
265 ms |
64984 KB |
Output is correct |
41 |
Correct |
247 ms |
64912 KB |
Output is correct |
42 |
Correct |
240 ms |
64768 KB |
Output is correct |
43 |
Correct |
240 ms |
64968 KB |
Output is correct |
44 |
Correct |
234 ms |
64896 KB |
Output is correct |
45 |
Correct |
259 ms |
65048 KB |
Output is correct |
46 |
Correct |
242 ms |
64792 KB |
Output is correct |
47 |
Correct |
253 ms |
64952 KB |
Output is correct |
48 |
Correct |
94 ms |
52492 KB |
Output is correct |
49 |
Correct |
114 ms |
65340 KB |
Output is correct |
50 |
Correct |
31 ms |
16784 KB |
Output is correct |
51 |
Correct |
46 ms |
28408 KB |
Output is correct |
52 |
Correct |
22 ms |
17100 KB |
Output is correct |
53 |
Correct |
180 ms |
63016 KB |
Output is correct |
54 |
Correct |
83 ms |
29732 KB |
Output is correct |
55 |
Correct |
197 ms |
45568 KB |
Output is correct |
56 |
Correct |
118 ms |
31736 KB |
Output is correct |
57 |
Correct |
189 ms |
58116 KB |
Output is correct |
58 |
Correct |
38 ms |
32076 KB |
Output is correct |
59 |
Correct |
42 ms |
26016 KB |
Output is correct |
60 |
Correct |
97 ms |
58208 KB |
Output is correct |
61 |
Correct |
102 ms |
60288 KB |
Output is correct |
62 |
Correct |
75 ms |
50052 KB |
Output is correct |
63 |
Correct |
75 ms |
19012 KB |
Output is correct |
64 |
Correct |
88 ms |
21292 KB |
Output is correct |
65 |
Correct |
128 ms |
31124 KB |
Output is correct |
66 |
Correct |
64 ms |
11332 KB |
Output is correct |
67 |
Correct |
100 ms |
23240 KB |
Output is correct |
68 |
Correct |
180 ms |
31740 KB |
Output is correct |
69 |
Correct |
30 ms |
7244 KB |
Output is correct |
70 |
Correct |
9 ms |
5356 KB |
Output is correct |
71 |
Correct |
76 ms |
16736 KB |
Output is correct |
72 |
Correct |
125 ms |
26436 KB |
Output is correct |
73 |
Correct |
237 ms |
34968 KB |
Output is correct |
74 |
Correct |
238 ms |
31516 KB |
Output is correct |
75 |
Correct |
203 ms |
34900 KB |
Output is correct |
76 |
Correct |
204 ms |
33668 KB |
Output is correct |
77 |
Correct |
240 ms |
31760 KB |
Output is correct |