Submission #67477

# Submission time Handle Problem Language Result Execution time Memory
67477 2018-08-14T10:33:36 Z aome Cats or Dogs (JOI18_catdog) C++17
100 / 100
473 ms 81024 KB
#include "catdog.h"

#include <bits/stdc++.h>

using namespace std;

const int N = 100005;
const int INF = 1e9;

int n;
int a[N];
int par[N], nchild[N];
int T, head[N], pos[N], L[N], R[N];
vector<int> G[N];

struct Node {
	int f[2][2];

	Node() {
		f[0][0] = f[1][1] = f[0][1] = f[1][0] = INF;
 	}

 	Node operator + (Node x) {
 		Node ret;
 		for (int i = 0; i < 2; ++i) {
 			for (int j = 0; j < 2; ++j) {
 				for (int k = 0; k < 2; ++k) {
 					for (int l = 0; l < 2; ++l) {
 						ret.f[i][l] = min(ret.f[i][l], f[i][j] + x.f[k][l] + (j ^ k));
 					}
 				}
 			}
 		}
 		return ret;
 	}
};

struct SegmentTree {
	vector<Node> T;

	#define mid ((l + r) >> 1)

	void build(int i, int l, int r) {
		if (l == r) {
			T[i].f[0][0] = T[i].f[1][1] = 0; return;
		}
		build(i << 1, l, mid);
		build(i << 1 | 1, mid + 1, r);
		T[i] = T[i << 1] + T[i << 1 | 1];
	}

	void upd(int i, int l, int r, int p, int v0, int v1) {
		if (l == r) {
			T[i].f[0][0] += v0, T[i].f[1][1] += v1; return;
		}
		if (mid >= p) upd(i << 1, l, mid, p, v0, v1);
		else upd(i << 1 | 1, mid + 1, r, p, v0, v1);
		T[i] = T[i << 1] + T[i << 1 | 1];
	} 

	#undef mid
} IT[N];

void dfs(int u) {
	nchild[u] = 1;
	for (auto v : G[u]) {
		if (v == par[u]) continue;
		par[v] = u, dfs(v), nchild[u] += nchild[v];
	}
}

void dfsHld(int u) {
	if (head[u] == -1) head[u] = u;
	pos[u] = ++T;
	int nx = -1;
	for (auto v : G[u]) {
		if (v == par[u]) continue;
		if (nx == -1 || nchild[nx] < nchild[v]) nx = v;
	}
	if (nx != -1) { head[nx] = head[u], dfsHld(nx); }
	for (auto v : G[u]) {
		if (v == par[u] || v == nx) continue; dfsHld(v);
	}
}

void initialize(int _n, vector<int> A, vector<int> B) {
	n = _n;
	for (int i = 0; i < (n - 1); ++i) {
		--A[i], --B[i];
		G[A[i]].push_back(B[i]), G[B[i]].push_back(A[i]);
	}
	memset(head, -1, sizeof head);
	dfs(0), dfsHld(0);
	for (int i = 0; i < n; ++i) L[i] = n, R[i] = 1;
	for (int i = 0; i < n; ++i) {
		L[head[i]] = min(L[head[i]], pos[i]);
		R[head[i]] = max(R[head[i]], pos[i]);
	}
	for (int i = 0; i < n; ++i) {
		if (head[i] != i) continue;
		int tmp = R[i] - L[i] + 1;
		IT[i].T.assign(tmp * 4 + 5, Node());
		IT[i].build(1, L[i], R[i]);
	}
}

void upd(int u, int v0, int v1) {
	while (head[u] != head[0]) {
		int p = head[u];
		int x[2], y[2];
		x[0] = x[1] = INF;
		for (int i = 0; i < 2; ++i) {
			for (int j = 0; j < 2; ++j) {
				for (int k = 0; k < 2; ++k) {
					x[i] = min(x[i], IT[p].T[1].f[j][k] + (i ^ j));
				}
			}
		}
		IT[p].upd(1, L[p], R[p], pos[u], v0, v1);
		y[0] = y[1] = INF;
		for (int i = 0; i < 2; ++i) {
			for (int j = 0; j < 2; ++j) {
				for (int k = 0; k < 2; ++k) {
					y[i] = min(y[i], IT[p].T[1].f[j][k] + (i ^ j));
				}
			}
		}
		u = par[p], v0 = y[0] - x[0], v1 = y[1] - x[1];
	}
	IT[0].upd(1, L[0], R[0], pos[u], v0, v1);
}

int get() {
	int ret = INF;
	for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) {
		ret = min(ret, IT[0].T[1].f[i][j]);
	}
	return ret;
}

int cat(int v) {
	--v, a[v] = 0, upd(v, 0, INF); return get();
}

int dog(int v) {
	--v, a[v] = 1, upd(v, INF, 0); return get();
}

int neighbor(int v) {
	--v; if (!a[v]) upd(v, 0, -INF); else upd(v, -INF, 0); return get();
}

Compilation message

catdog.cpp: In function 'void dfsHld(int)':
catdog.cpp:82:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   if (v == par[u] || v == nx) continue; dfsHld(v);
   ^~
catdog.cpp:82:41: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   if (v == par[u] || v == nx) continue; dfsHld(v);
                                         ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5368 KB Output is correct
2 Correct 8 ms 5476 KB Output is correct
3 Correct 9 ms 5552 KB Output is correct
4 Correct 9 ms 5568 KB Output is correct
5 Correct 8 ms 5700 KB Output is correct
6 Correct 8 ms 5700 KB Output is correct
7 Correct 7 ms 5844 KB Output is correct
8 Correct 9 ms 5844 KB Output is correct
9 Correct 8 ms 5844 KB Output is correct
10 Correct 8 ms 5880 KB Output is correct
11 Correct 9 ms 5880 KB Output is correct
12 Correct 6 ms 5880 KB Output is correct
13 Correct 11 ms 5880 KB Output is correct
14 Correct 7 ms 5880 KB Output is correct
15 Correct 9 ms 5880 KB Output is correct
16 Correct 7 ms 5880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5368 KB Output is correct
2 Correct 8 ms 5476 KB Output is correct
3 Correct 9 ms 5552 KB Output is correct
4 Correct 9 ms 5568 KB Output is correct
5 Correct 8 ms 5700 KB Output is correct
6 Correct 8 ms 5700 KB Output is correct
7 Correct 7 ms 5844 KB Output is correct
8 Correct 9 ms 5844 KB Output is correct
9 Correct 8 ms 5844 KB Output is correct
10 Correct 8 ms 5880 KB Output is correct
11 Correct 9 ms 5880 KB Output is correct
12 Correct 6 ms 5880 KB Output is correct
13 Correct 11 ms 5880 KB Output is correct
14 Correct 7 ms 5880 KB Output is correct
15 Correct 9 ms 5880 KB Output is correct
16 Correct 7 ms 5880 KB Output is correct
17 Correct 9 ms 5984 KB Output is correct
18 Correct 9 ms 6032 KB Output is correct
19 Correct 9 ms 6060 KB Output is correct
20 Correct 6 ms 6060 KB Output is correct
21 Correct 9 ms 6060 KB Output is correct
22 Correct 9 ms 6060 KB Output is correct
23 Correct 9 ms 6128 KB Output is correct
24 Correct 10 ms 6128 KB Output is correct
25 Correct 10 ms 6128 KB Output is correct
26 Correct 10 ms 6128 KB Output is correct
27 Correct 9 ms 6128 KB Output is correct
28 Correct 8 ms 6128 KB Output is correct
29 Correct 11 ms 6192 KB Output is correct
30 Correct 11 ms 6192 KB Output is correct
31 Correct 8 ms 6192 KB Output is correct
32 Correct 8 ms 6192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5368 KB Output is correct
2 Correct 8 ms 5476 KB Output is correct
3 Correct 9 ms 5552 KB Output is correct
4 Correct 9 ms 5568 KB Output is correct
5 Correct 8 ms 5700 KB Output is correct
6 Correct 8 ms 5700 KB Output is correct
7 Correct 7 ms 5844 KB Output is correct
8 Correct 9 ms 5844 KB Output is correct
9 Correct 8 ms 5844 KB Output is correct
10 Correct 8 ms 5880 KB Output is correct
11 Correct 9 ms 5880 KB Output is correct
12 Correct 6 ms 5880 KB Output is correct
13 Correct 11 ms 5880 KB Output is correct
14 Correct 7 ms 5880 KB Output is correct
15 Correct 9 ms 5880 KB Output is correct
16 Correct 7 ms 5880 KB Output is correct
17 Correct 9 ms 5984 KB Output is correct
18 Correct 9 ms 6032 KB Output is correct
19 Correct 9 ms 6060 KB Output is correct
20 Correct 6 ms 6060 KB Output is correct
21 Correct 9 ms 6060 KB Output is correct
22 Correct 9 ms 6060 KB Output is correct
23 Correct 9 ms 6128 KB Output is correct
24 Correct 10 ms 6128 KB Output is correct
25 Correct 10 ms 6128 KB Output is correct
26 Correct 10 ms 6128 KB Output is correct
27 Correct 9 ms 6128 KB Output is correct
28 Correct 8 ms 6128 KB Output is correct
29 Correct 11 ms 6192 KB Output is correct
30 Correct 11 ms 6192 KB Output is correct
31 Correct 8 ms 6192 KB Output is correct
32 Correct 8 ms 6192 KB Output is correct
33 Correct 225 ms 16596 KB Output is correct
34 Correct 160 ms 19072 KB Output is correct
35 Correct 257 ms 19072 KB Output is correct
36 Correct 398 ms 27032 KB Output is correct
37 Correct 45 ms 27032 KB Output is correct
38 Correct 465 ms 31128 KB Output is correct
39 Correct 405 ms 33088 KB Output is correct
40 Correct 427 ms 35016 KB Output is correct
41 Correct 447 ms 36908 KB Output is correct
42 Correct 415 ms 38960 KB Output is correct
43 Correct 473 ms 40888 KB Output is correct
44 Correct 366 ms 42736 KB Output is correct
45 Correct 400 ms 44664 KB Output is correct
46 Correct 393 ms 46584 KB Output is correct
47 Correct 333 ms 48488 KB Output is correct
48 Correct 116 ms 48752 KB Output is correct
49 Correct 147 ms 54824 KB Output is correct
50 Correct 40 ms 54824 KB Output is correct
51 Correct 51 ms 54824 KB Output is correct
52 Correct 47 ms 54824 KB Output is correct
53 Correct 231 ms 54824 KB Output is correct
54 Correct 119 ms 54824 KB Output is correct
55 Correct 265 ms 54824 KB Output is correct
56 Correct 176 ms 54824 KB Output is correct
57 Correct 257 ms 56708 KB Output is correct
58 Correct 50 ms 56708 KB Output is correct
59 Correct 53 ms 56708 KB Output is correct
60 Correct 137 ms 62592 KB Output is correct
61 Correct 146 ms 65048 KB Output is correct
62 Correct 91 ms 65048 KB Output is correct
63 Correct 101 ms 65048 KB Output is correct
64 Correct 93 ms 65048 KB Output is correct
65 Correct 134 ms 65472 KB Output is correct
66 Correct 72 ms 65472 KB Output is correct
67 Correct 127 ms 65472 KB Output is correct
68 Correct 194 ms 69008 KB Output is correct
69 Correct 40 ms 69008 KB Output is correct
70 Correct 15 ms 69008 KB Output is correct
71 Correct 89 ms 69008 KB Output is correct
72 Correct 158 ms 69008 KB Output is correct
73 Correct 242 ms 75080 KB Output is correct
74 Correct 278 ms 75088 KB Output is correct
75 Correct 238 ms 78944 KB Output is correct
76 Correct 245 ms 80108 KB Output is correct
77 Correct 336 ms 81024 KB Output is correct