Submission #978180

# Submission time Handle Problem Language Result Execution time Memory
978180 2024-05-09T02:32:06 Z vjudge1 Cats or Dogs (JOI18_catdog) C++17
100 / 100
405 ms 28244 KB
// #include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
const int inf = 100000;
int n, m, u, v, add[2], sub[2], color[100005], f[100005][2], _g[100005][2];
int cnt, fa[100005], siz[100005], dep[100005], son[100005], dfn[100005], top[100005], rnk[100005], bot[100005];
vector<int> e[100005];
struct matrix {
	int v[2][2];
	matrix(int val = inf) {
		v[0][0] = val, v[0][1] = inf;
		v[1][0] = inf, v[1][1] = val;
	}
	matrix operator = (const matrix& _) {
		v[0][0] = _.v[0][0], v[0][1] = _.v[0][1];
		v[1][0] = _.v[1][0], v[1][1] = _.v[1][1];
		return *this;
	}
	matrix operator * (const matrix& _) const {
		matrix ret;
		ret.v[0][0] = min(v[0][0] + _.v[0][0], v[0][1] + _.v[1][0]);
		ret.v[0][1] = min(v[0][0] + _.v[0][1], v[0][1] + _.v[1][1]);
		ret.v[1][0] = min(v[1][0] + _.v[0][0], v[1][1] + _.v[1][0]);
		ret.v[1][1] = min(v[1][0] + _.v[0][1], v[1][1] + _.v[1][1]);
		return ret;//循环展开后的广义矩阵乘法
	}
} ans, before, after, g[100005];
struct Segment_Tree {//线段树维护转移矩阵
	matrix val[400005];
	int pos, L, R, l[400005], r[400005];
	#define lc (k << 1)
	#define rc ((k << 1) | 1)
	#define mid ((l[k] + r[k]) >> 1)
	void push_up(int k) {
		val[k] = val[lc] * val[rc];
	}
	void build(int k) {
		if(l[k] == r[k]) {
			val[k] = g[rnk[l[k]]];
			return;
		}
		l[lc] = l[k], r[lc] = mid, l[rc] = mid + 1, r[rc] = r[k];
		build(lc), build(rc);
		push_up(k);
	}
	void change(int k) {
		if(l[k] == r[k]) {
			val[k] = g[rnk[pos]];
			return;
		}
		if(pos <= mid) change(lc);
		else change(rc);
		push_up(k);
	}
	matrix ask(int k) {
		if(L <= l[k] && r[k] <= R) return val[k];
		matrix ret(0);
		if(L <= mid) ret = ret * ask(lc);
		if(R > mid) ret = ret * ask(rc);
		return ret;
	}
	void Change(int Pos) {
		pos = Pos;
		return change(1);
	}
	matrix Ask(int l, int r) {
		L = l, R = r;
		return ask(1);
	}
} tree;
//树链剖分
void dfs1(int now) {
	siz[now] = 1, dep[now] = dep[fa[now]] + 1;
	for(const auto& i : e[now]) {
		if(i != fa[now]) {
			fa[i] = now;
			dfs1(i);
			siz[now] += siz[i];
			if(siz[i] > siz[son[now]]) son[now] = i;
		}
	}
}
void dfs2(int now) {
	++cnt; dfn[now] = cnt, rnk[cnt] = now;
	if(now == son[fa[now]]) top[now] = top[fa[now]];
	else top[now] = now;
	if(son[now]) {
		dfs2(son[now]);
		bot[now] = bot[son[now]];
		f[now][0] += min(f[son[now]][0], f[son[now]][1] + 1);
		f[now][1] += min(f[son[now]][0] + 1, f[son[now]][1]);
	}
	else bot[now] = now;
	for(const auto& i : e[now]) {
		if(i != fa[now] && i != son[now]) {
			dfs2(i);
			f[now][0] += min(f[i][0], f[i][1] + 1);
			f[now][1] += min(f[i][0] + 1, f[i][1]);
			_g[now][0] += min(f[i][0], f[i][1] + 1);
			_g[now][1] += min(f[i][0] + 1, f[i][1]);
		}
	}
	g[now].v[0][0] = _g[now][0], g[now].v[0][1] = _g[now][0] + 1;
	g[now].v[1][0] = _g[now][1] + 1, g[now].v[1][1] = _g[now][1];
}
//修改转移矩阵
void change(int now, int val) {
	g[now].v[0][0] = _g[now][0], g[now].v[0][1] = _g[now][0] + 1;
	g[now].v[1][0] = _g[now][1] + 1, g[now].v[1][1] = _g[now][1];
	color[now] = val;
	if(color[now]) g[now].v[color[now] - 1][0] = g[now].v[color[now] - 1][1] = inf;//把不可能出现的一行设为inf
	while(now) {
		before = tree.Ask(dfn[top[now]], dfn[bot[now]]);
		tree.Change(dfn[now]);
		after = tree.Ask(dfn[top[now]], dfn[bot[now]]);
		now = fa[top[now]];
		add[0] = min(after.v[0][0], after.v[0][1]), sub[0] = min(before.v[0][0], before.v[0][1]);
		add[1] = min(after.v[1][0], after.v[1][1]), sub[1] = min(before.v[1][0], before.v[1][1]);
		_g[now][0] += min(add[0], add[1] + 1) - min(sub[0], sub[1] + 1), _g[now][1] += min(add[0] + 1, add[1]) - min(sub[0] + 1, sub[1]);
		g[now].v[0][0] = _g[now][0], g[now].v[0][1] = _g[now][0] + 1;
		g[now].v[1][0] = _g[now][1] + 1, g[now].v[1][1] = _g[now][1];
		if(color[now]) g[now].v[color[now] - 1][0] = g[now].v[color[now] - 1][1] = inf;//不可能出现的状态
	}
}
//下面就是交互了
void initialize(int N, std::vector<int> A, std::vector<int> B) {
	n = N;
	for(int i = 2; i <= n; ++i) {
		u = A[i - 2], v = B[i - 2];
		e[u].push_back(v);
		e[v].push_back(u);
	}
	dfs1(1);
	dfs2(1);
	tree.l[1] = 1, tree.r[1] = n;
	tree.build(1);
}
int cat(int v) {
	change(v, 1);
	ans = tree.Ask(dfn[top[1]], dfn[bot[1]]);
	return min(min(ans.v[0][0], ans.v[0][1]), min(ans.v[1][0], ans.v[1][1]));
}
int dog(int v) {
	change(v, 2);
	ans = tree.Ask(dfn[top[1]], dfn[bot[1]]);
	return min(min(ans.v[0][0], ans.v[0][1]), min(ans.v[1][0], ans.v[1][1]));
}
int neighbor(int v) {
	change(v, 0);
	ans = tree.Ask(dfn[top[1]], dfn[bot[1]]);
	return min(min(ans.v[0][0], ans.v[0][1]), min(ans.v[1][0], ans.v[1][1]));
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10588 KB Output is correct
2 Correct 5 ms 10588 KB Output is correct
3 Correct 5 ms 10588 KB Output is correct
4 Correct 5 ms 10588 KB Output is correct
5 Correct 5 ms 10588 KB Output is correct
6 Correct 5 ms 10588 KB Output is correct
7 Correct 5 ms 10588 KB Output is correct
8 Correct 5 ms 10564 KB Output is correct
9 Correct 5 ms 10588 KB Output is correct
10 Correct 5 ms 10588 KB Output is correct
11 Correct 4 ms 10584 KB Output is correct
12 Correct 5 ms 10588 KB Output is correct
13 Correct 5 ms 10588 KB Output is correct
14 Correct 5 ms 10588 KB Output is correct
15 Correct 5 ms 10588 KB Output is correct
16 Correct 5 ms 10452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10588 KB Output is correct
2 Correct 5 ms 10588 KB Output is correct
3 Correct 5 ms 10588 KB Output is correct
4 Correct 5 ms 10588 KB Output is correct
5 Correct 5 ms 10588 KB Output is correct
6 Correct 5 ms 10588 KB Output is correct
7 Correct 5 ms 10588 KB Output is correct
8 Correct 5 ms 10564 KB Output is correct
9 Correct 5 ms 10588 KB Output is correct
10 Correct 5 ms 10588 KB Output is correct
11 Correct 4 ms 10584 KB Output is correct
12 Correct 5 ms 10588 KB Output is correct
13 Correct 5 ms 10588 KB Output is correct
14 Correct 5 ms 10588 KB Output is correct
15 Correct 5 ms 10588 KB Output is correct
16 Correct 5 ms 10452 KB Output is correct
17 Correct 6 ms 10588 KB Output is correct
18 Correct 6 ms 10588 KB Output is correct
19 Correct 5 ms 10588 KB Output is correct
20 Correct 5 ms 10588 KB Output is correct
21 Correct 7 ms 10664 KB Output is correct
22 Correct 6 ms 10588 KB Output is correct
23 Correct 6 ms 10588 KB Output is correct
24 Correct 6 ms 10584 KB Output is correct
25 Correct 5 ms 10588 KB Output is correct
26 Correct 5 ms 10584 KB Output is correct
27 Correct 6 ms 10588 KB Output is correct
28 Correct 5 ms 10584 KB Output is correct
29 Correct 5 ms 10584 KB Output is correct
30 Correct 6 ms 10588 KB Output is correct
31 Correct 5 ms 10632 KB Output is correct
32 Correct 5 ms 10696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10588 KB Output is correct
2 Correct 5 ms 10588 KB Output is correct
3 Correct 5 ms 10588 KB Output is correct
4 Correct 5 ms 10588 KB Output is correct
5 Correct 5 ms 10588 KB Output is correct
6 Correct 5 ms 10588 KB Output is correct
7 Correct 5 ms 10588 KB Output is correct
8 Correct 5 ms 10564 KB Output is correct
9 Correct 5 ms 10588 KB Output is correct
10 Correct 5 ms 10588 KB Output is correct
11 Correct 4 ms 10584 KB Output is correct
12 Correct 5 ms 10588 KB Output is correct
13 Correct 5 ms 10588 KB Output is correct
14 Correct 5 ms 10588 KB Output is correct
15 Correct 5 ms 10588 KB Output is correct
16 Correct 5 ms 10452 KB Output is correct
17 Correct 6 ms 10588 KB Output is correct
18 Correct 6 ms 10588 KB Output is correct
19 Correct 5 ms 10588 KB Output is correct
20 Correct 5 ms 10588 KB Output is correct
21 Correct 7 ms 10664 KB Output is correct
22 Correct 6 ms 10588 KB Output is correct
23 Correct 6 ms 10588 KB Output is correct
24 Correct 6 ms 10584 KB Output is correct
25 Correct 5 ms 10588 KB Output is correct
26 Correct 5 ms 10584 KB Output is correct
27 Correct 6 ms 10588 KB Output is correct
28 Correct 5 ms 10584 KB Output is correct
29 Correct 5 ms 10584 KB Output is correct
30 Correct 6 ms 10588 KB Output is correct
31 Correct 5 ms 10632 KB Output is correct
32 Correct 5 ms 10696 KB Output is correct
33 Correct 237 ms 17396 KB Output is correct
34 Correct 80 ms 17792 KB Output is correct
35 Correct 195 ms 15904 KB Output is correct
36 Correct 344 ms 22424 KB Output is correct
37 Correct 16 ms 13916 KB Output is correct
38 Correct 374 ms 23264 KB Output is correct
39 Correct 367 ms 23424 KB Output is correct
40 Correct 379 ms 23544 KB Output is correct
41 Correct 372 ms 23616 KB Output is correct
42 Correct 360 ms 23424 KB Output is correct
43 Correct 358 ms 23400 KB Output is correct
44 Correct 368 ms 23432 KB Output is correct
45 Correct 405 ms 23416 KB Output is correct
46 Correct 369 ms 23440 KB Output is correct
47 Correct 353 ms 23364 KB Output is correct
48 Correct 135 ms 19100 KB Output is correct
49 Correct 114 ms 20476 KB Output is correct
50 Correct 44 ms 12932 KB Output is correct
51 Correct 48 ms 14692 KB Output is correct
52 Correct 22 ms 12636 KB Output is correct
53 Correct 161 ms 22344 KB Output is correct
54 Correct 113 ms 15896 KB Output is correct
55 Correct 296 ms 20324 KB Output is correct
56 Correct 193 ms 16680 KB Output is correct
57 Correct 252 ms 21648 KB Output is correct
58 Correct 23 ms 14804 KB Output is correct
59 Correct 45 ms 14100 KB Output is correct
60 Correct 102 ms 19700 KB Output is correct
61 Correct 106 ms 20000 KB Output is correct
62 Correct 66 ms 18388 KB Output is correct
63 Correct 37 ms 18516 KB Output is correct
64 Correct 40 ms 19796 KB Output is correct
65 Correct 56 ms 25516 KB Output is correct
66 Correct 54 ms 14416 KB Output is correct
67 Correct 56 ms 21852 KB Output is correct
68 Correct 103 ms 25580 KB Output is correct
69 Correct 27 ms 11868 KB Output is correct
70 Correct 10 ms 10844 KB Output is correct
71 Correct 48 ms 17620 KB Output is correct
72 Correct 66 ms 23636 KB Output is correct
73 Correct 152 ms 28244 KB Output is correct
74 Correct 174 ms 25436 KB Output is correct
75 Correct 120 ms 27984 KB Output is correct
76 Correct 115 ms 27172 KB Output is correct
77 Correct 176 ms 25680 KB Output is correct