답안 #527396

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
527396 2022-02-17T10:57:55 Z Qingyu Cats or Dogs (JOI18_catdog) C++17
100 / 100
802 ms 33724 KB
#include "catdog.h"
#include <bits/stdc++.h>

const int N = 1e5 + 50;
int n, fa[N], top[N], dep[N], g[N][2], siz[N], son[N], dfn[N], dp[N][2], tot, tail[N], delta[N][2], id[N];
std::vector<int> G[N];

inline int lc(int o) { return o << 1; }
inline int rc(int o) { return o << 1 | 1; }

struct matrix_t {
	int M[2][2], n, m;
	matrix_t(int n = 0, int m = 0) : n(n), m(m) {
		memset(M, 0x3f, sizeof M);
	}
	void set(int g0, int g1) {
		M[0][0] = g0, M[1][0] = g0 + 1;
		M[0][1] = g1 + 1, M[1][1] = g1;
	}
} gm[N], tr[N << 2];

matrix_t operator*(const matrix_t &lhs, const matrix_t &rhs) {
	matrix_t res;
	for (int i = 0; i < 2; ++i)
		for (int j = 0; j < 2; ++j)
			for (int k = 0; k < 2; ++k)
				res.M[i][j] = std::min(res.M[i][j], lhs.M[i][k] + rhs.M[k][j]);
	return res;
}

void dfs(int x, int f, int d) {
	fa[x] = f, dep[x] = d, siz[x] = 1;
	int mx = -1;
	dp[x][0] = dp[x][1] = 0;
	for (int y : G[x])
		if (y != f) {
			dfs(y, x, d + 1);
			siz[x] += siz[y];
			if (siz[y] > mx) {
				mx = siz[y];
				son[x] = y;
			}
			dp[x][0] = std::min(dp[y][0], dp[y][1] + 1);
			dp[x][1] = std::min(dp[y][1], dp[y][0] + 1);
		}
}

void dfs2(int x, int topf) {
	dfn[x] = ++tot;
	id[tot] = x;
	top[x] = topf;
	tail[topf] = x;
	g[x][0] = g[x][1] = 0;
	if (son[x]) {
		dfs2(son[x], topf);
		for (int y : G[x])
			if (y != fa[x] && y != son[x]) {
				dfs2(y, y);
				g[x][0] += std::min(dp[y][0], dp[y][1] + 1);
				g[x][1] += std::min(dp[y][1], dp[y][0] + 1);
			}
	}
	gm[x].set(g[x][0], g[x][1]);
}

void push_up(int o) {
	tr[o] = tr[rc(o)] * tr[lc(o)];
}

void build(int o, int l, int r) {
	if (l == r) {
		tr[o] = gm[id[l]];
	}
	else {
		const int mid = l + r >> 1;
		build(lc(o), l, mid);
		build(rc(o), mid + 1, r);
		push_up(o);
	}
}

matrix_t query(int o, int l, int r, int ql, int qr) {
	if (ql <= l && r <= qr)
		return tr[o];
	const int mid = l + r >> 1;
	if (qr <= mid) return query(lc(o), l, mid, ql, qr);
	if (ql > mid) return query(rc(o), mid + 1, r, ql, qr);
	return query(rc(o), mid + 1, r, ql, qr) * query(lc(o), l, mid, ql, qr);
}

void modify(int o, int l, int r, int p) {
	if (l == r) {
		tr[o] = gm[id[l]];
	}
	else {
		const int mid = l + r >> 1;
		if (p <= mid) modify(lc(o), l, mid, p);
		else modify(rc(o), mid + 1, r, p);
		push_up(o);
	}
}

void initialize(int N, std::vector<int> A, std::vector<int> B) {
	n = N;
	for (int i = 0; i < A.size(); ++i) {
		G[A[i]].push_back(B[i]);
		G[B[i]].push_back(A[i]);
	}
	dfs(1, 0, 1);
	dfs2(1, 1);
	assert(tot == N);
	build(1, 1, N);
}

void cancel(int v) {
	g[v][0] -= delta[v][0];
	g[v][1] -= delta[v][1];
}
void update(int v) {
	g[v][0] += delta[v][0];
	g[v][1] += delta[v][1];
	gm[v].set(g[v][0], g[v][1]);
}

void travel(int x) {
	while (x) {
		matrix_t old = query(1, 1, n, dfn[top[x]], dfn[tail[top[x]]]);
		modify(1, 1, n, dfn[x]);
		matrix_t now = query(1, 1, n, dfn[top[x]], dfn[tail[top[x]]]);
		x = fa[top[x]];
		if (!x) break;
		{
			int f0 = std::min(old.M[0][0], old.M[1][0]);
			int f1 = std::min(old.M[0][1], old.M[1][1]);
			g[x][0] -= std::min(f0, f1 + 1);
			g[x][1] -= std::min(f1, f0 + 1);
		}
		{
			int f0 = std::min(now.M[0][0], now.M[1][0]);
			int f1 = std::min(now.M[0][1], now.M[1][1]);
			g[x][0] += std::min(f0, f1 + 1);
			g[x][1] += std::min(f1, f0 + 1);
		}
		gm[x].set(g[x][0], g[x][1]);
	}
}
int getans() {
	auto M = query(1, 1, n, dfn[1], dfn[tail[top[1]]]);
	int ans = std::min(M.M[0][0], M.M[0][1]);
	ans = std::min(ans, M.M[1][0]);
	ans = std::min(ans, M.M[1][1]);
	return ans;
}
int cat(int v) {
	cancel(v);
	delta[v][0] = 0, delta[v][1] = 0x3f3f3f3f;
	update(v);
	travel(v);
	return getans();
}

int dog(int v) {
	cancel(v);
	delta[v][0] = 0x3f3f3f3f, delta[v][1] = 0;
	update(v);
	travel(v);
	return getans();
}

int neighbor(int v) {
	cancel(v);
	delta[v][0] = delta[v][1] = 0;
	update(v);
	travel(v);
	return getans();
}

Compilation message

catdog.cpp: In function 'void build(int, int, int)':
catdog.cpp:75:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   75 |   const int mid = l + r >> 1;
      |                   ~~^~~
catdog.cpp: In function 'matrix_t query(int, int, int, int, int)':
catdog.cpp:85:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   85 |  const int mid = l + r >> 1;
      |                  ~~^~~
catdog.cpp: In function 'void modify(int, int, int, int)':
catdog.cpp:96:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   96 |   const int mid = l + r >> 1;
      |                   ~~^~~
catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:105:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |  for (int i = 0; i < A.size(); ++i) {
      |                  ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14412 KB Output is correct
2 Correct 7 ms 14388 KB Output is correct
3 Correct 7 ms 14396 KB Output is correct
4 Correct 7 ms 14400 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 7 ms 14352 KB Output is correct
7 Correct 7 ms 14412 KB Output is correct
8 Correct 7 ms 14412 KB Output is correct
9 Correct 7 ms 14388 KB Output is correct
10 Correct 7 ms 14412 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 8 ms 14412 KB Output is correct
13 Correct 9 ms 14412 KB Output is correct
14 Correct 7 ms 14400 KB Output is correct
15 Correct 9 ms 14432 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14412 KB Output is correct
2 Correct 7 ms 14388 KB Output is correct
3 Correct 7 ms 14396 KB Output is correct
4 Correct 7 ms 14400 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 7 ms 14352 KB Output is correct
7 Correct 7 ms 14412 KB Output is correct
8 Correct 7 ms 14412 KB Output is correct
9 Correct 7 ms 14388 KB Output is correct
10 Correct 7 ms 14412 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 8 ms 14412 KB Output is correct
13 Correct 9 ms 14412 KB Output is correct
14 Correct 7 ms 14400 KB Output is correct
15 Correct 9 ms 14432 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 8 ms 14532 KB Output is correct
18 Correct 8 ms 14520 KB Output is correct
19 Correct 7 ms 14540 KB Output is correct
20 Correct 7 ms 14412 KB Output is correct
21 Correct 8 ms 14408 KB Output is correct
22 Correct 7 ms 14408 KB Output is correct
23 Correct 12 ms 14520 KB Output is correct
24 Correct 7 ms 14540 KB Output is correct
25 Correct 8 ms 14412 KB Output is correct
26 Correct 7 ms 14400 KB Output is correct
27 Correct 8 ms 14396 KB Output is correct
28 Correct 9 ms 14528 KB Output is correct
29 Correct 8 ms 14540 KB Output is correct
30 Correct 7 ms 14408 KB Output is correct
31 Correct 7 ms 14532 KB Output is correct
32 Correct 9 ms 14408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14412 KB Output is correct
2 Correct 7 ms 14388 KB Output is correct
3 Correct 7 ms 14396 KB Output is correct
4 Correct 7 ms 14400 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 7 ms 14352 KB Output is correct
7 Correct 7 ms 14412 KB Output is correct
8 Correct 7 ms 14412 KB Output is correct
9 Correct 7 ms 14388 KB Output is correct
10 Correct 7 ms 14412 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 8 ms 14412 KB Output is correct
13 Correct 9 ms 14412 KB Output is correct
14 Correct 7 ms 14400 KB Output is correct
15 Correct 9 ms 14432 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 8 ms 14532 KB Output is correct
18 Correct 8 ms 14520 KB Output is correct
19 Correct 7 ms 14540 KB Output is correct
20 Correct 7 ms 14412 KB Output is correct
21 Correct 8 ms 14408 KB Output is correct
22 Correct 7 ms 14408 KB Output is correct
23 Correct 12 ms 14520 KB Output is correct
24 Correct 7 ms 14540 KB Output is correct
25 Correct 8 ms 14412 KB Output is correct
26 Correct 7 ms 14400 KB Output is correct
27 Correct 8 ms 14396 KB Output is correct
28 Correct 9 ms 14528 KB Output is correct
29 Correct 8 ms 14540 KB Output is correct
30 Correct 7 ms 14408 KB Output is correct
31 Correct 7 ms 14532 KB Output is correct
32 Correct 9 ms 14408 KB Output is correct
33 Correct 308 ms 21508 KB Output is correct
34 Correct 86 ms 21636 KB Output is correct
35 Correct 270 ms 19912 KB Output is correct
36 Correct 457 ms 26280 KB Output is correct
37 Correct 23 ms 17736 KB Output is correct
38 Correct 524 ms 27456 KB Output is correct
39 Correct 450 ms 27464 KB Output is correct
40 Correct 427 ms 27464 KB Output is correct
41 Correct 415 ms 27436 KB Output is correct
42 Correct 479 ms 27472 KB Output is correct
43 Correct 425 ms 27440 KB Output is correct
44 Correct 550 ms 27464 KB Output is correct
45 Correct 802 ms 27456 KB Output is correct
46 Correct 489 ms 27464 KB Output is correct
47 Correct 478 ms 27464 KB Output is correct
48 Correct 133 ms 23564 KB Output is correct
49 Correct 151 ms 25756 KB Output is correct
50 Correct 50 ms 17092 KB Output is correct
51 Correct 57 ms 18896 KB Output is correct
52 Correct 32 ms 16688 KB Output is correct
53 Correct 276 ms 25788 KB Output is correct
54 Correct 173 ms 19680 KB Output is correct
55 Correct 739 ms 23788 KB Output is correct
56 Correct 231 ms 20588 KB Output is correct
57 Correct 277 ms 25404 KB Output is correct
58 Correct 44 ms 18964 KB Output is correct
59 Correct 54 ms 18556 KB Output is correct
60 Correct 125 ms 24588 KB Output is correct
61 Correct 125 ms 25004 KB Output is correct
62 Correct 104 ms 22700 KB Output is correct
63 Correct 59 ms 22724 KB Output is correct
64 Correct 64 ms 24512 KB Output is correct
65 Correct 72 ms 30140 KB Output is correct
66 Correct 75 ms 18540 KB Output is correct
67 Correct 80 ms 25536 KB Output is correct
68 Correct 159 ms 30532 KB Output is correct
69 Correct 39 ms 16076 KB Output is correct
70 Correct 14 ms 14656 KB Output is correct
71 Correct 63 ms 21980 KB Output is correct
72 Correct 99 ms 28100 KB Output is correct
73 Correct 223 ms 33724 KB Output is correct
74 Correct 267 ms 30312 KB Output is correct
75 Correct 163 ms 33712 KB Output is correct
76 Correct 179 ms 32444 KB Output is correct
77 Correct 244 ms 30656 KB Output is correct