답안 #921017

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
921017 2024-02-03T08:57:12 Z KiaRez Cats or Dogs (JOI18_catdog) C++17
100 / 100
956 ms 42308 KB
/*
    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;
	z.dp[0][0]=z.dp[1][1]=N;
	for(int i=0; i<2; i++) {
	for(int j=0; j<2; j++) { 
	for(int l=0; l<2; l++) {
	for(int k=0; k<2; k++) {
z.dp[i][j] = min({z.dp[i][j], x.dp[i][l]+y.dp[k][j]+(l!=k)});
	}
	}
	}
	}
	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]});
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22364 KB Output is correct
2 Correct 6 ms 28508 KB Output is correct
3 Correct 6 ms 28508 KB Output is correct
4 Correct 6 ms 26652 KB Output is correct
5 Correct 6 ms 28508 KB Output is correct
6 Correct 6 ms 26652 KB Output is correct
7 Correct 6 ms 28508 KB Output is correct
8 Correct 6 ms 28508 KB Output is correct
9 Correct 5 ms 26716 KB Output is correct
10 Correct 6 ms 28508 KB Output is correct
11 Correct 6 ms 26968 KB Output is correct
12 Correct 5 ms 22364 KB Output is correct
13 Correct 5 ms 22364 KB Output is correct
14 Correct 5 ms 22364 KB Output is correct
15 Correct 6 ms 28592 KB Output is correct
16 Correct 7 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 28508 KB Output is correct
4 Correct 6 ms 26652 KB Output is correct
5 Correct 6 ms 28508 KB Output is correct
6 Correct 6 ms 26652 KB Output is correct
7 Correct 6 ms 28508 KB Output is correct
8 Correct 6 ms 28508 KB Output is correct
9 Correct 5 ms 26716 KB Output is correct
10 Correct 6 ms 28508 KB Output is correct
11 Correct 6 ms 26968 KB Output is correct
12 Correct 5 ms 22364 KB Output is correct
13 Correct 5 ms 22364 KB Output is correct
14 Correct 5 ms 22364 KB Output is correct
15 Correct 6 ms 28592 KB Output is correct
16 Correct 7 ms 28508 KB Output is correct
17 Correct 9 ms 26716 KB Output is correct
18 Correct 9 ms 28764 KB Output is correct
19 Correct 9 ms 28800 KB Output is correct
20 Correct 6 ms 26712 KB Output is correct
21 Correct 7 ms 28756 KB Output is correct
22 Correct 9 ms 28764 KB Output is correct
23 Correct 9 ms 26716 KB Output is correct
24 Correct 8 ms 28804 KB Output is correct
25 Correct 8 ms 28764 KB Output is correct
26 Correct 7 ms 28764 KB Output is correct
27 Correct 6 ms 26736 KB Output is correct
28 Correct 6 ms 28764 KB Output is correct
29 Correct 8 ms 26716 KB Output is correct
30 Correct 6 ms 28752 KB Output is correct
31 Correct 6 ms 28764 KB Output is correct
32 Correct 8 ms 28760 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 28508 KB Output is correct
4 Correct 6 ms 26652 KB Output is correct
5 Correct 6 ms 28508 KB Output is correct
6 Correct 6 ms 26652 KB Output is correct
7 Correct 6 ms 28508 KB Output is correct
8 Correct 6 ms 28508 KB Output is correct
9 Correct 5 ms 26716 KB Output is correct
10 Correct 6 ms 28508 KB Output is correct
11 Correct 6 ms 26968 KB Output is correct
12 Correct 5 ms 22364 KB Output is correct
13 Correct 5 ms 22364 KB Output is correct
14 Correct 5 ms 22364 KB Output is correct
15 Correct 6 ms 28592 KB Output is correct
16 Correct 7 ms 28508 KB Output is correct
17 Correct 9 ms 26716 KB Output is correct
18 Correct 9 ms 28764 KB Output is correct
19 Correct 9 ms 28800 KB Output is correct
20 Correct 6 ms 26712 KB Output is correct
21 Correct 7 ms 28756 KB Output is correct
22 Correct 9 ms 28764 KB Output is correct
23 Correct 9 ms 26716 KB Output is correct
24 Correct 8 ms 28804 KB Output is correct
25 Correct 8 ms 28764 KB Output is correct
26 Correct 7 ms 28764 KB Output is correct
27 Correct 6 ms 26736 KB Output is correct
28 Correct 6 ms 28764 KB Output is correct
29 Correct 8 ms 26716 KB Output is correct
30 Correct 6 ms 28752 KB Output is correct
31 Correct 6 ms 28764 KB Output is correct
32 Correct 8 ms 28760 KB Output is correct
33 Correct 512 ms 32452 KB Output is correct
34 Correct 168 ms 32192 KB Output is correct
35 Correct 468 ms 31688 KB Output is correct
36 Correct 811 ms 34812 KB Output is correct
37 Correct 20 ms 30296 KB Output is correct
38 Correct 956 ms 35364 KB Output is correct
39 Correct 907 ms 35352 KB Output is correct
40 Correct 834 ms 35360 KB Output is correct
41 Correct 771 ms 35388 KB Output is correct
42 Correct 827 ms 35360 KB Output is correct
43 Correct 807 ms 35608 KB Output is correct
44 Correct 780 ms 35360 KB Output is correct
45 Correct 817 ms 35588 KB Output is correct
46 Correct 835 ms 35348 KB Output is correct
47 Correct 805 ms 35356 KB Output is correct
48 Correct 158 ms 33432 KB Output is correct
49 Correct 176 ms 34556 KB Output is correct
50 Correct 78 ms 30044 KB Output is correct
51 Correct 75 ms 30908 KB Output is correct
52 Correct 39 ms 29920 KB Output is correct
53 Correct 277 ms 34124 KB Output is correct
54 Correct 243 ms 31320 KB Output is correct
55 Correct 632 ms 33496 KB Output is correct
56 Correct 419 ms 32024 KB Output is correct
57 Correct 537 ms 34188 KB Output is correct
58 Correct 27 ms 30864 KB Output is correct
59 Correct 63 ms 30812 KB Output is correct
60 Correct 125 ms 33764 KB Output is correct
61 Correct 150 ms 34020 KB Output is correct
62 Correct 84 ms 32800 KB Output is correct
63 Correct 43 ms 29856 KB Output is correct
64 Correct 48 ms 27716 KB Output is correct
65 Correct 61 ms 31576 KB Output is correct
66 Correct 73 ms 31568 KB Output is correct
67 Correct 58 ms 32024 KB Output is correct
68 Correct 131 ms 37184 KB Output is correct
69 Correct 32 ms 23704 KB Output is correct
70 Correct 11 ms 22616 KB Output is correct
71 Correct 47 ms 27876 KB Output is correct
72 Correct 59 ms 30288 KB Output is correct
73 Correct 266 ms 42308 KB Output is correct
74 Correct 290 ms 38764 KB Output is correct
75 Correct 121 ms 34388 KB Output is correct
76 Correct 112 ms 33108 KB Output is correct
77 Correct 307 ms 39292 KB Output is correct