#include <bits/stdc++.h>
#include "catdog.h"
#define DIM 100010
#define INF 1000000000
using namespace std;
vector <int> L[DIM],a,b;
int v[DIM];
int n,i,q,tip,x,nr_chains;
vector <int> chains[DIM];
int whatChain[DIM],positionInChain[DIM],chainFatherNode[DIM],s[DIM];
struct segment_tree{
long long dp[2][2];
};
vector <segment_tree> aint[DIM];
void dfs (int nod, int tata){
s[nod] = 1;
int ok = 0, maxi = 0, heavy_son = 0;
for (auto vecin : L[nod])
if (vecin != tata){
ok = 1;
dfs (vecin,nod);
s[nod] += s[vecin];
if (s[vecin] > maxi)
maxi = s[vecin], heavy_son = vecin;
}
if (!ok){
nr_chains++;
chains[nr_chains].push_back(0);
chains[nr_chains].push_back(nod);
positionInChain[nod] = 1;
whatChain[nod] = nr_chains;
} else {
chains[whatChain[heavy_son]].push_back(nod);
whatChain[nod] = whatChain[heavy_son];
positionInChain[nod] = chains[whatChain[heavy_son]].size()-1;
for (auto vecin : L[nod]){
if (vecin != tata && vecin != heavy_son)
chainFatherNode[whatChain[vecin]] = nod;
}
}
}
void update_nod (int chain, int nod){
int fiu_st = nod<<1, fiu_dr = (nod<<1)|1;
for (int i=0;i<=1;i++)
for (int j=0;j<=1;j++){
long long mini = 1LL * INF * INF;
for (int i2=0;i2<=1;i2++)
for (int j2=0;j2<=1;j2++)
mini = min (mini,aint[chain][fiu_st].dp[i][i2] + aint[chain][fiu_dr].dp[j2][j] + (i2 != j2));
aint[chain][nod].dp[i][j] = mini;
}
}
void build (int chain, int nod, int st, int dr){
if (st == dr){
aint[chain][nod].dp[0][0] = aint[chain][nod].dp[1][1] = 0;
aint[chain][nod].dp[0][1] = aint[chain][nod].dp[1][0] = INF;
return;
}
int mid = (st+dr)>>1;
build (chain,nod<<1,st,mid);
build (chain,(nod<<1)|1,mid+1,dr);
update_nod (chain,nod);
}
void update_aint (int chain, int nod, int st, int dr, int poz, long long val, long long val2){
if (st == dr){
aint[chain][nod].dp[0][0] += val;
aint[chain][nod].dp[1][1] += val2;
return;
}
int mid = (st+dr)>>1;
if (poz <= mid)
update_aint(chain,nod<<1,st,mid,poz,val,val2);
else update_aint(chain,(nod<<1)|1,mid+1,dr,poz,val,val2);
update_nod(chain,nod);
}
int query (){
long long mini = 1LL * INF * INF;
for (int i=0;i<=1;i++)
for (int j=0;j<=1;j++)
mini = min (mini,aint[ whatChain[1] ][1].dp[i][j]);
return mini;
}
void initialize(int _n, vector<int> A, vector<int> B) {
n = _n;
for (int i=0;i<n-1;i++){
L[A[i]].push_back(B[i]);
L[B[i]].push_back(A[i]);
}
dfs (1,0);
for (int i=1;i<=nr_chains;i++){
aint[i].resize(chains[i].size()*4);
//for (int j=0;j<=chains[i].size()*4;j++)
// aint[i].push_back();
build (i,1,1,chains[i].size()-1);
}
}
void update (int x, long long val, long long val2){
int chain = whatChain[x];
long long old_val0 = min (aint[chain][1].dp[0][0],aint[chain][1].dp[1][0]);
long long old_val1 = min (aint[chain][1].dp[0][1],aint[chain][1].dp[1][1]);
update_aint(chain,1,1,chains[chain].size()-1,positionInChain[x],val,val2);
long long new_val0 = min (aint[chain][1].dp[0][0],aint[chain][1].dp[1][0]);
long long new_val1 = min (aint[chain][1].dp[0][1],aint[chain][1].dp[1][1]);
int new_x = chainFatherNode[chain];
if (new_x)
update (new_x, min(new_val0,new_val1+1) - min(old_val0,old_val1+1), min(new_val1,new_val0+1) - min(old_val1,old_val0+1));
}
int cat(int x) {
v[x] = 1;
update (x,0,INF);
return query();
}
int dog(int x) {
v[x] = 2;
update (x,INF,0);
return query();
}
int neighbor(int x) {
if (v[x] == 1)
update (x,0,-INF);
if (v[x] == 2)
update (x,-INF,0);
v[x] = 0;
return query();
}
/*
int main (){
ifstream cin ("date.in");
ofstream cout ("date.out");
cin>>n;
for (i=0;i<n-1;i++){
int x,y;
cin>>x>>y;
a.push_back(x);
b.push_back(y);
//cin>>a[i]>>b[i];
}
initialize(n,a,b);
cin>>q;
for (;q--;){
cin>>tip>>x;
if (tip == 1)
cout<<cat(x)<<"\n";
else {
if (tip == 2)
cout<<dog(x)<<"\n";
else cout<<neighbor(x)<<"\n";
}
}
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7352 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7356 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7356 KB |
Output is correct |
8 |
Correct |
5 ms |
7376 KB |
Output is correct |
9 |
Correct |
5 ms |
7372 KB |
Output is correct |
10 |
Correct |
5 ms |
7352 KB |
Output is correct |
11 |
Correct |
5 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7272 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
5 ms |
7244 KB |
Output is correct |
15 |
Correct |
4 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7352 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7356 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7356 KB |
Output is correct |
8 |
Correct |
5 ms |
7376 KB |
Output is correct |
9 |
Correct |
5 ms |
7372 KB |
Output is correct |
10 |
Correct |
5 ms |
7352 KB |
Output is correct |
11 |
Correct |
5 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7272 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
5 ms |
7244 KB |
Output is correct |
15 |
Correct |
4 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7372 KB |
Output is correct |
17 |
Correct |
6 ms |
7488 KB |
Output is correct |
18 |
Correct |
7 ms |
7628 KB |
Output is correct |
19 |
Correct |
5 ms |
7500 KB |
Output is correct |
20 |
Correct |
6 ms |
7372 KB |
Output is correct |
21 |
Correct |
6 ms |
7360 KB |
Output is correct |
22 |
Correct |
6 ms |
7372 KB |
Output is correct |
23 |
Correct |
6 ms |
7500 KB |
Output is correct |
24 |
Correct |
7 ms |
7500 KB |
Output is correct |
25 |
Correct |
5 ms |
7372 KB |
Output is correct |
26 |
Correct |
5 ms |
7372 KB |
Output is correct |
27 |
Correct |
5 ms |
7372 KB |
Output is correct |
28 |
Correct |
5 ms |
7500 KB |
Output is correct |
29 |
Correct |
6 ms |
7500 KB |
Output is correct |
30 |
Correct |
5 ms |
7356 KB |
Output is correct |
31 |
Correct |
5 ms |
7640 KB |
Output is correct |
32 |
Correct |
5 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7352 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7356 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7356 KB |
Output is correct |
8 |
Correct |
5 ms |
7376 KB |
Output is correct |
9 |
Correct |
5 ms |
7372 KB |
Output is correct |
10 |
Correct |
5 ms |
7352 KB |
Output is correct |
11 |
Correct |
5 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7272 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
5 ms |
7244 KB |
Output is correct |
15 |
Correct |
4 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7372 KB |
Output is correct |
17 |
Correct |
6 ms |
7488 KB |
Output is correct |
18 |
Correct |
7 ms |
7628 KB |
Output is correct |
19 |
Correct |
5 ms |
7500 KB |
Output is correct |
20 |
Correct |
6 ms |
7372 KB |
Output is correct |
21 |
Correct |
6 ms |
7360 KB |
Output is correct |
22 |
Correct |
6 ms |
7372 KB |
Output is correct |
23 |
Correct |
6 ms |
7500 KB |
Output is correct |
24 |
Correct |
7 ms |
7500 KB |
Output is correct |
25 |
Correct |
5 ms |
7372 KB |
Output is correct |
26 |
Correct |
5 ms |
7372 KB |
Output is correct |
27 |
Correct |
5 ms |
7372 KB |
Output is correct |
28 |
Correct |
5 ms |
7500 KB |
Output is correct |
29 |
Correct |
6 ms |
7500 KB |
Output is correct |
30 |
Correct |
5 ms |
7356 KB |
Output is correct |
31 |
Correct |
5 ms |
7640 KB |
Output is correct |
32 |
Correct |
5 ms |
7372 KB |
Output is correct |
33 |
Correct |
116 ms |
21488 KB |
Output is correct |
34 |
Correct |
67 ms |
25032 KB |
Output is correct |
35 |
Correct |
106 ms |
18108 KB |
Output is correct |
36 |
Correct |
180 ms |
33448 KB |
Output is correct |
37 |
Correct |
23 ms |
15568 KB |
Output is correct |
38 |
Correct |
201 ms |
36404 KB |
Output is correct |
39 |
Correct |
232 ms |
36424 KB |
Output is correct |
40 |
Correct |
210 ms |
36420 KB |
Output is correct |
41 |
Correct |
219 ms |
36440 KB |
Output is correct |
42 |
Correct |
196 ms |
36404 KB |
Output is correct |
43 |
Correct |
212 ms |
36392 KB |
Output is correct |
44 |
Correct |
202 ms |
36408 KB |
Output is correct |
45 |
Correct |
205 ms |
36452 KB |
Output is correct |
46 |
Correct |
200 ms |
36408 KB |
Output is correct |
47 |
Correct |
250 ms |
36428 KB |
Output is correct |
48 |
Correct |
81 ms |
35472 KB |
Output is correct |
49 |
Correct |
100 ms |
43052 KB |
Output is correct |
50 |
Correct |
29 ms |
14520 KB |
Output is correct |
51 |
Correct |
40 ms |
21164 KB |
Output is correct |
52 |
Correct |
22 ms |
14416 KB |
Output is correct |
53 |
Correct |
125 ms |
34632 KB |
Output is correct |
54 |
Correct |
77 ms |
19228 KB |
Output is correct |
55 |
Correct |
170 ms |
27356 KB |
Output is correct |
56 |
Correct |
100 ms |
20496 KB |
Output is correct |
57 |
Correct |
151 ms |
32820 KB |
Output is correct |
58 |
Correct |
33 ms |
23032 KB |
Output is correct |
59 |
Correct |
42 ms |
19852 KB |
Output is correct |
60 |
Correct |
90 ms |
38852 KB |
Output is correct |
61 |
Correct |
130 ms |
40112 KB |
Output is correct |
62 |
Correct |
70 ms |
33856 KB |
Output is correct |
63 |
Correct |
46 ms |
21576 KB |
Output is correct |
64 |
Correct |
59 ms |
23932 KB |
Output is correct |
65 |
Correct |
68 ms |
33940 KB |
Output is correct |
66 |
Correct |
53 ms |
13856 KB |
Output is correct |
67 |
Correct |
65 ms |
26048 KB |
Output is correct |
68 |
Correct |
138 ms |
34344 KB |
Output is correct |
69 |
Correct |
37 ms |
9636 KB |
Output is correct |
70 |
Correct |
13 ms |
7628 KB |
Output is correct |
71 |
Correct |
61 ms |
19396 KB |
Output is correct |
72 |
Correct |
79 ms |
29376 KB |
Output is correct |
73 |
Correct |
139 ms |
37972 KB |
Output is correct |
74 |
Correct |
173 ms |
33860 KB |
Output is correct |
75 |
Correct |
117 ms |
38084 KB |
Output is correct |
76 |
Correct |
115 ms |
36520 KB |
Output is correct |
77 |
Correct |
145 ms |
34392 KB |
Output is correct |