/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 3e5+23, lg = 17;
ll Mod = 998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
struct node {
int dp[2][2];
node() {dp[0][0]=dp[0][1]=dp[1][0]=dp[1][1]=0; dp[0][1]=dp[1][0]=N;}
} seg[N];
node merge(node x, node y) {
node z;
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
z.dp[i][j] = min({x.dp[i][1]+y.dp[1][j], x.dp[i][0]+y.dp[0][j], x.dp[i][0]+y.dp[1][j]+1, 1+x.dp[i][1]+y.dp[0][j]});
}
}
return z;
}
int n, tim, dwn[N], tin[N], tout[N], par[N], head[N], h[N], subt[N], typ[N];
int ttmp[2][2][N];
vector<int> adj[N];
void init(int v, int p=0) {
par[v]=p, h[v]=h[p]+1, subt[v]=1;
for(int u : adj[v]) {
if(u == p) continue;
init(u, v); subt[v] += subt[u];
}
}
void dfs(int v, int p) {
int mx = 0;
tin[v] = ++tim, head[v] = p, dwn[p] = v;
for(int u : adj[v]) if(u!=par[v]) mx = (subt[mx] > subt[u] ? mx : u);
if(mx > 0) dfs(mx, p);
for(int u : adj[v]) if(u!=par[v] && u!=mx) dfs(u, u);
tout[v] = tim+1;
}
void update(int ind, node val) {
if(ind == 0) return;
if(ind >= (1<<lg)) {
seg[ind] = val;
} else {
seg[ind] = merge(seg[2*ind], seg[2*ind+1]);
}
update(ind/2, val);
}
node query(int l, int r, int ind=1, int lc=1, int rc=(1<<lg)+1) {
if(lc>=l && rc<=r) return seg[ind];
int mid = (lc+rc)/2;
if(r<=mid) return query(l, r, 2*ind, lc, mid);
if(l>=mid) return query(l, r, 2*ind+1, mid, rc);
return merge(query(l, r, 2*ind, lc, mid), query(l, r, 2*ind+1, mid, rc));
}
void initialize(int _n, vector<int> A, vector<int> B) {
n=_n;
for(int i=0; i<n-1; i++) adj[A[i]].pb(B[i]), adj[B[i]].pb(A[i]);
init(1);
dfs(1, 1);
}
int ftmp[2][2][N];
void reinit(int v) {
int u = v;
node ct;
while(head[v] != 1) {
ct = query(tin[head[v]], tin[dwn[head[v]]]+1);
node tmp = query(tin[par[head[v]]], tin[par[head[v]]]+1);
v=par[head[v]];
ftmp[0][0][tin[v]] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
ftmp[1][0][tin[v]] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
ftmp[0][1][tin[v]] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
ftmp[1][1][tin[v]] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
update(tin[v]+(1<<lg)-1, tmp);
}
v = u;
while(head[v] != 1) {
ct = query(tin[head[v]], tin[dwn[head[v]]]+1);
node tmp = query(tin[par[head[v]]], tin[par[head[v]]]+1);
v=par[head[v]];
tmp.dp[0][0] -= ftmp[0][0][tin[v]];
tmp.dp[1][0] -= ftmp[1][0][tin[v]];
tmp.dp[0][1] -= ftmp[0][1][tin[v]];
tmp.dp[1][1] -= ftmp[1][1][tin[v]];
ttmp[0][0][tin[v]] -= ftmp[0][0][tin[v]];
ttmp[1][0][tin[v]] -= ftmp[1][0][tin[v]];
ttmp[0][1][tin[v]] -= ftmp[0][1][tin[v]];
ttmp[1][1][tin[v]] -= ftmp[1][1][tin[v]];
update(tin[v]+(1<<lg)-1, tmp);
for(int i=0; i<2; i++) for(int j=0; j<2; j++) ftmp[i][j][tin[v]] = 0;
}
}
int cat(int v) {
reinit(v);
typ[v] = 1;
node ct;
ct.dp[1][0]=ct.dp[1][1]=ct.dp[0][1]=n;
if(v==1) ct.dp[1][0] = 0;
for(int i=0; i<2; i++) for(int j=0; j<2; j++) ct.dp[i][j] += ttmp[i][j][tin[v]];
update(tin[v]+(1<<lg)-1, ct);
while(head[v] != 1) {
ct = query(tin[head[v]], tin[dwn[head[v]]]+1);
node tmp = query(tin[par[head[v]]], tin[par[head[v]]]+1);
tmp.dp[0][0] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
tmp.dp[1][0] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
tmp.dp[0][1] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
tmp.dp[1][1] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
v=par[head[v]];
ttmp[0][0][tin[v]] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
ttmp[1][0][tin[v]] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
ttmp[0][1][tin[v]] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
ttmp[1][1][tin[v]] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
update(tin[v]+(1<<lg)-1, tmp);
}
ct = query(tin[1], tin[dwn[1]]+1);
return min({ct.dp[0][0], ct.dp[0][1], ct.dp[1][1], ct.dp[1][0]});
}
int dog(int v) {
reinit(v);
typ[v] = 2;
node ct;
ct.dp[1][0]=ct.dp[0][0]=ct.dp[0][1]=n;
if(v==1) ct.dp[0][1] = 0;
for(int i=0; i<2; i++) for(int j=0; j<2; j++) ct.dp[i][j] += ttmp[i][j][tin[v]];
update(tin[v]+(1<<lg)-1, ct);
while(head[v] != 1) {
ct = query(tin[head[v]], tin[dwn[head[v]]]+1);
node tmp = query(tin[par[head[v]]], tin[par[head[v]]]+1);
tmp.dp[0][0] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
tmp.dp[1][0] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
tmp.dp[0][1] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
tmp.dp[1][1] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
v=par[head[v]];
ttmp[0][0][tin[v]] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
ttmp[1][0][tin[v]] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
ttmp[0][1][tin[v]] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
ttmp[1][1][tin[v]] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
update(tin[v]+(1<<lg)-1, tmp);
}
ct = query(tin[1], tin[dwn[1]]+1);
return min({ct.dp[0][0], ct.dp[0][1], ct.dp[1][1], ct.dp[1][0]});
}
int neighbor(int v) {
reinit(v);
typ[v] = 0;
node ct;
for(int i=0; i<2; i++) for(int j=0; j<2; j++) ct.dp[i][j] += ttmp[i][j][tin[v]];
update(tin[v]+(1<<lg)-1, ct);
while(head[v] != 1) {
ct = query(tin[head[v]], tin[dwn[head[v]]]+1);
node tmp = query(tin[par[head[v]]], tin[par[head[v]]]+1);
tmp.dp[0][0] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
tmp.dp[1][0] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
tmp.dp[0][1] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
tmp.dp[1][1] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
v=par[head[v]];
ttmp[0][0][tin[v]] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
ttmp[1][0][tin[v]] += min({ct.dp[1][0]+1, ct.dp[1][1]+1, ct.dp[0][1], ct.dp[0][0]});
ttmp[0][1][tin[v]] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
ttmp[1][1][tin[v]] += min({ct.dp[0][1]+1, ct.dp[0][0]+1, ct.dp[1][0], ct.dp[1][1]});
update(tin[v]+(1<<lg)-1, tmp);
}
ct = query(tin[1], tin[dwn[1]]+1);
return min({ct.dp[0][0], ct.dp[0][1], ct.dp[1][1], ct.dp[1][0]});
}
/*
int main () {
initialize(7, {1, 2, 3, 1, 5, 1}, {2, 3, 4, 5, 6, 7});
cout<< cat(1)<<endl;
cout<< dog(5)<<endl;
cout<< dog(4)<<endl;
cout<< dog(2)<<endl;
cout<< dog(7)<<endl;
cout<< dog(3)<<endl;
cout<< dog(6)<<endl;
cout<< neighbor(1)<<endl;
// tmp2 = merge(tmp2, tmp1);
// fuck(tmp2.dp[0][0]);
// fuck(tmp2.dp[1][0]);
// fuck(tmp2.dp[0][1]);
// fuck(tmp2.dp[1][1]);
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22364 KB |
Output is correct |
2 |
Correct |
6 ms |
28508 KB |
Output is correct |
3 |
Correct |
6 ms |
28752 KB |
Output is correct |
4 |
Correct |
6 ms |
26716 KB |
Output is correct |
5 |
Correct |
6 ms |
28508 KB |
Output is correct |
6 |
Correct |
6 ms |
26548 KB |
Output is correct |
7 |
Correct |
6 ms |
28724 KB |
Output is correct |
8 |
Correct |
6 ms |
28592 KB |
Output is correct |
9 |
Correct |
6 ms |
26716 KB |
Output is correct |
10 |
Correct |
6 ms |
28508 KB |
Output is correct |
11 |
Correct |
6 ms |
26604 KB |
Output is correct |
12 |
Correct |
5 ms |
22364 KB |
Output is correct |
13 |
Correct |
5 ms |
22360 KB |
Output is correct |
14 |
Correct |
5 ms |
22360 KB |
Output is correct |
15 |
Correct |
7 ms |
28508 KB |
Output is correct |
16 |
Correct |
6 ms |
28508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22364 KB |
Output is correct |
2 |
Correct |
6 ms |
28508 KB |
Output is correct |
3 |
Correct |
6 ms |
28752 KB |
Output is correct |
4 |
Correct |
6 ms |
26716 KB |
Output is correct |
5 |
Correct |
6 ms |
28508 KB |
Output is correct |
6 |
Correct |
6 ms |
26548 KB |
Output is correct |
7 |
Correct |
6 ms |
28724 KB |
Output is correct |
8 |
Correct |
6 ms |
28592 KB |
Output is correct |
9 |
Correct |
6 ms |
26716 KB |
Output is correct |
10 |
Correct |
6 ms |
28508 KB |
Output is correct |
11 |
Correct |
6 ms |
26604 KB |
Output is correct |
12 |
Correct |
5 ms |
22364 KB |
Output is correct |
13 |
Correct |
5 ms |
22360 KB |
Output is correct |
14 |
Correct |
5 ms |
22360 KB |
Output is correct |
15 |
Correct |
7 ms |
28508 KB |
Output is correct |
16 |
Correct |
6 ms |
28508 KB |
Output is correct |
17 |
Correct |
8 ms |
26712 KB |
Output is correct |
18 |
Correct |
9 ms |
28764 KB |
Output is correct |
19 |
Correct |
7 ms |
28764 KB |
Output is correct |
20 |
Correct |
7 ms |
26712 KB |
Output is correct |
21 |
Correct |
8 ms |
28764 KB |
Output is correct |
22 |
Correct |
7 ms |
28764 KB |
Output is correct |
23 |
Correct |
8 ms |
26784 KB |
Output is correct |
24 |
Correct |
8 ms |
28764 KB |
Output is correct |
25 |
Correct |
9 ms |
28764 KB |
Output is correct |
26 |
Correct |
7 ms |
28724 KB |
Output is correct |
27 |
Correct |
6 ms |
26716 KB |
Output is correct |
28 |
Correct |
6 ms |
28760 KB |
Output is correct |
29 |
Correct |
7 ms |
26712 KB |
Output is correct |
30 |
Correct |
7 ms |
28764 KB |
Output is correct |
31 |
Correct |
6 ms |
28764 KB |
Output is correct |
32 |
Correct |
8 ms |
28748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22364 KB |
Output is correct |
2 |
Correct |
6 ms |
28508 KB |
Output is correct |
3 |
Correct |
6 ms |
28752 KB |
Output is correct |
4 |
Correct |
6 ms |
26716 KB |
Output is correct |
5 |
Correct |
6 ms |
28508 KB |
Output is correct |
6 |
Correct |
6 ms |
26548 KB |
Output is correct |
7 |
Correct |
6 ms |
28724 KB |
Output is correct |
8 |
Correct |
6 ms |
28592 KB |
Output is correct |
9 |
Correct |
6 ms |
26716 KB |
Output is correct |
10 |
Correct |
6 ms |
28508 KB |
Output is correct |
11 |
Correct |
6 ms |
26604 KB |
Output is correct |
12 |
Correct |
5 ms |
22364 KB |
Output is correct |
13 |
Correct |
5 ms |
22360 KB |
Output is correct |
14 |
Correct |
5 ms |
22360 KB |
Output is correct |
15 |
Correct |
7 ms |
28508 KB |
Output is correct |
16 |
Correct |
6 ms |
28508 KB |
Output is correct |
17 |
Correct |
8 ms |
26712 KB |
Output is correct |
18 |
Correct |
9 ms |
28764 KB |
Output is correct |
19 |
Correct |
7 ms |
28764 KB |
Output is correct |
20 |
Correct |
7 ms |
26712 KB |
Output is correct |
21 |
Correct |
8 ms |
28764 KB |
Output is correct |
22 |
Correct |
7 ms |
28764 KB |
Output is correct |
23 |
Correct |
8 ms |
26784 KB |
Output is correct |
24 |
Correct |
8 ms |
28764 KB |
Output is correct |
25 |
Correct |
9 ms |
28764 KB |
Output is correct |
26 |
Correct |
7 ms |
28724 KB |
Output is correct |
27 |
Correct |
6 ms |
26716 KB |
Output is correct |
28 |
Correct |
6 ms |
28760 KB |
Output is correct |
29 |
Correct |
7 ms |
26712 KB |
Output is correct |
30 |
Correct |
7 ms |
28764 KB |
Output is correct |
31 |
Correct |
6 ms |
28764 KB |
Output is correct |
32 |
Correct |
8 ms |
28748 KB |
Output is correct |
33 |
Correct |
435 ms |
33536 KB |
Output is correct |
34 |
Correct |
122 ms |
33108 KB |
Output is correct |
35 |
Correct |
403 ms |
32672 KB |
Output is correct |
36 |
Correct |
687 ms |
36576 KB |
Output is correct |
37 |
Correct |
17 ms |
30740 KB |
Output is correct |
38 |
Correct |
730 ms |
37272 KB |
Output is correct |
39 |
Correct |
681 ms |
37288 KB |
Output is correct |
40 |
Correct |
728 ms |
37280 KB |
Output is correct |
41 |
Correct |
732 ms |
37184 KB |
Output is correct |
42 |
Correct |
688 ms |
37296 KB |
Output is correct |
43 |
Correct |
705 ms |
37424 KB |
Output is correct |
44 |
Correct |
684 ms |
37300 KB |
Output is correct |
45 |
Correct |
682 ms |
37300 KB |
Output is correct |
46 |
Correct |
672 ms |
37304 KB |
Output is correct |
47 |
Correct |
668 ms |
37280 KB |
Output is correct |
48 |
Correct |
146 ms |
34760 KB |
Output is correct |
49 |
Correct |
156 ms |
36140 KB |
Output is correct |
50 |
Correct |
70 ms |
30512 KB |
Output is correct |
51 |
Correct |
72 ms |
31560 KB |
Output is correct |
52 |
Correct |
32 ms |
30040 KB |
Output is correct |
53 |
Correct |
266 ms |
35460 KB |
Output is correct |
54 |
Correct |
237 ms |
32324 KB |
Output is correct |
55 |
Correct |
587 ms |
35220 KB |
Output is correct |
56 |
Correct |
381 ms |
33020 KB |
Output is correct |
57 |
Correct |
477 ms |
35728 KB |
Output is correct |
58 |
Correct |
27 ms |
31376 KB |
Output is correct |
59 |
Correct |
59 ms |
31568 KB |
Output is correct |
60 |
Correct |
120 ms |
35300 KB |
Output is correct |
61 |
Correct |
130 ms |
35640 KB |
Output is correct |
62 |
Correct |
79 ms |
33964 KB |
Output is correct |
63 |
Correct |
47 ms |
30684 KB |
Output is correct |
64 |
Correct |
37 ms |
28496 KB |
Output is correct |
65 |
Correct |
50 ms |
32592 KB |
Output is correct |
66 |
Correct |
70 ms |
32336 KB |
Output is correct |
67 |
Correct |
56 ms |
33088 KB |
Output is correct |
68 |
Correct |
124 ms |
38952 KB |
Output is correct |
69 |
Correct |
28 ms |
24016 KB |
Output is correct |
70 |
Correct |
10 ms |
22616 KB |
Output is correct |
71 |
Correct |
43 ms |
28696 KB |
Output is correct |
72 |
Correct |
58 ms |
31568 KB |
Output is correct |
73 |
Correct |
243 ms |
44404 KB |
Output is correct |
74 |
Correct |
270 ms |
40732 KB |
Output is correct |
75 |
Correct |
109 ms |
36180 KB |
Output is correct |
76 |
Correct |
107 ms |
35012 KB |
Output is correct |
77 |
Correct |
274 ms |
41116 KB |
Output is correct |