Submission #153759

# Submission time Handle Problem Language Result Execution time Memory
153759 2019-09-15T20:16:11 Z tutis Cats or Dogs (JOI18_catdog) C++17
100 / 100
2156 ms 29304 KB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 101010;
vector<int>adj[maxn];
int pa[maxn], sz[maxn];
void dfs(int i, int p)
{
	if (p != 0)
		adj[i].erase(find(adj[i].begin(), adj[i].end(), p));
	pa[i] = p;
	sz[i] = 1;
	pair<int, int>mx = { -1, -1};
	for (int t = 0; t < (int)adj[i].size(); t++)
	{
		int j = adj[i][t];
		dfs(j, i);
		sz[i] += sz[j];
		mx = max(mx, {sz[j], t});
	}
	if (mx.second != -1)
		swap(adj[i][0], adj[i][mx.second]);
}
int timer = 1;
int nr[maxn], head[maxn], tail[maxn];
void hld(int i, int h)
{
	nr[i] = timer++;
	head[i] = h;
	tail[h] = i;
	for (int t = 0; t < (int)adj[i].size(); t++)
	{
		int j = adj[i][t];
		hld(j, t == 0 ? h : j);
	}
}
struct line
{
	int cc = 0, cd = maxn, dd = 0, dc = maxn;
	line() {}
};
line merge(line a, line b)
{
	line c;
	c.cc = min({a.cc + b.cc, a.cc + b.dc + 1, a.cd + b.cc + 1, a.cd + b.dc});
	c.cd = min({a.cc + b.cd, a.cc + b.dd + 1, a.cd + b.cd + 1, a.cd + b.dd});
	c.dd = min({a.dc + b.cd, a.dc + b.dd + 1, a.dd + b.cd + 1, a.dd + b.dd});
	c.dc = min({a.dc + b.cc, a.dc + b.dc + 1, a.dd + b.cc + 1, a.dd + b.dc});
	return c;
}
struct ST
{
	line X;
	int l, r;
	ST *left, *right;
	ST(int l, int r): l(l), r(r)
	{
		if (l < r)
		{
			left = new ST(l, (l + r) / 2);
			right = new ST((l + r) / 2 + 1, r);
			X = merge(left->X, right->X);
		}
	}
	line get(int x, int y)
	{
		if (x <= l && r <= y)
			return X;
		if (r < x || y < l)
			return line();
		return merge(left->get(x, y), right->get(x, y));
	}
	void set(int x, int w, int c)
	{
		if (l == r)
		{
			if (c)
				X.cc = w;
			else
				X.dd = w;
		}
		else
		{
			if (x <= (l + r) / 2)
				left->set(x, w, c);
			else
				right->set(x, w, c);
			X = merge(left->X, right->X);
		}
	}
} medis(0, 101010);
void initialize(int N, vector<int> A, vector<int> B)
{
	for (int i = 0; i < N - 1; i++)
	{
		adj[A[i]].push_back(B[i]);
		adj[B[i]].push_back(A[i]);
	}
	dfs(1, 0);
	hld(1, 1);
}
int C[maxn], D[maxn], V[maxn];
void fix(int v)
{
	while (true)
	{
		int w = head[v];
		line X = medis.get(nr[w], nr[tail[w]]);
		C[pa[w]] -= min({X.cd, X.cc, X.dc + 1, X.dd + 1});
		D[pa[w]] -= min({X.cd + 1, X.cc + 1, X.dc, X.dd});
		medis.set(nr[v], maxn, 0);
		medis.set(nr[v], maxn, 1);
		if (V[v] != 1)
			medis.set(nr[v], C[v], 1);
		if (V[v] != -1)
			medis.set(nr[v], D[v], 0);
		X = medis.get(nr[w], nr[tail[w]]);
		C[pa[w]] += min({X.cd, X.cc, X.dc + 1, X.dd + 1});
		D[pa[w]] += min({X.cd + 1, X.cc + 1, X.dc, X.dd});
		if (v == 1)break;
		if (v != w)
			v = w;
		else
			v = pa[v];
	}
}
int cat(int v)
{
	V[v] = -1;
	fix(v);
	return min(C[0], D[0]);
}
int dog(int v)
{
	V[v] = 1;
	fix(v);
	return min(C[0], D[0]);
}
int neighbor(int v)
{
	V[v] = 0;
	fix(v);
	return min(C[0], D[0]);
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 12280 KB Output is correct
2 Correct 19 ms 12264 KB Output is correct
3 Correct 19 ms 12152 KB Output is correct
4 Correct 19 ms 12280 KB Output is correct
5 Correct 19 ms 12280 KB Output is correct
6 Correct 19 ms 12252 KB Output is correct
7 Correct 19 ms 12152 KB Output is correct
8 Correct 19 ms 12152 KB Output is correct
9 Correct 19 ms 12152 KB Output is correct
10 Correct 18 ms 12152 KB Output is correct
11 Correct 18 ms 12280 KB Output is correct
12 Correct 18 ms 12284 KB Output is correct
13 Correct 19 ms 12280 KB Output is correct
14 Correct 19 ms 12280 KB Output is correct
15 Correct 18 ms 12152 KB Output is correct
16 Correct 19 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 12280 KB Output is correct
2 Correct 19 ms 12264 KB Output is correct
3 Correct 19 ms 12152 KB Output is correct
4 Correct 19 ms 12280 KB Output is correct
5 Correct 19 ms 12280 KB Output is correct
6 Correct 19 ms 12252 KB Output is correct
7 Correct 19 ms 12152 KB Output is correct
8 Correct 19 ms 12152 KB Output is correct
9 Correct 19 ms 12152 KB Output is correct
10 Correct 18 ms 12152 KB Output is correct
11 Correct 18 ms 12280 KB Output is correct
12 Correct 18 ms 12284 KB Output is correct
13 Correct 19 ms 12280 KB Output is correct
14 Correct 19 ms 12280 KB Output is correct
15 Correct 18 ms 12152 KB Output is correct
16 Correct 19 ms 12280 KB Output is correct
17 Correct 30 ms 12280 KB Output is correct
18 Correct 26 ms 12280 KB Output is correct
19 Correct 22 ms 12280 KB Output is correct
20 Correct 19 ms 12160 KB Output is correct
21 Correct 22 ms 12280 KB Output is correct
22 Correct 21 ms 12280 KB Output is correct
23 Correct 27 ms 12252 KB Output is correct
24 Correct 26 ms 12280 KB Output is correct
25 Correct 24 ms 12280 KB Output is correct
26 Correct 21 ms 12240 KB Output is correct
27 Correct 20 ms 12280 KB Output is correct
28 Correct 20 ms 12408 KB Output is correct
29 Correct 23 ms 12408 KB Output is correct
30 Correct 21 ms 12280 KB Output is correct
31 Correct 19 ms 12280 KB Output is correct
32 Correct 22 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 12280 KB Output is correct
2 Correct 19 ms 12264 KB Output is correct
3 Correct 19 ms 12152 KB Output is correct
4 Correct 19 ms 12280 KB Output is correct
5 Correct 19 ms 12280 KB Output is correct
6 Correct 19 ms 12252 KB Output is correct
7 Correct 19 ms 12152 KB Output is correct
8 Correct 19 ms 12152 KB Output is correct
9 Correct 19 ms 12152 KB Output is correct
10 Correct 18 ms 12152 KB Output is correct
11 Correct 18 ms 12280 KB Output is correct
12 Correct 18 ms 12284 KB Output is correct
13 Correct 19 ms 12280 KB Output is correct
14 Correct 19 ms 12280 KB Output is correct
15 Correct 18 ms 12152 KB Output is correct
16 Correct 19 ms 12280 KB Output is correct
17 Correct 30 ms 12280 KB Output is correct
18 Correct 26 ms 12280 KB Output is correct
19 Correct 22 ms 12280 KB Output is correct
20 Correct 19 ms 12160 KB Output is correct
21 Correct 22 ms 12280 KB Output is correct
22 Correct 21 ms 12280 KB Output is correct
23 Correct 27 ms 12252 KB Output is correct
24 Correct 26 ms 12280 KB Output is correct
25 Correct 24 ms 12280 KB Output is correct
26 Correct 21 ms 12240 KB Output is correct
27 Correct 20 ms 12280 KB Output is correct
28 Correct 20 ms 12408 KB Output is correct
29 Correct 23 ms 12408 KB Output is correct
30 Correct 21 ms 12280 KB Output is correct
31 Correct 19 ms 12280 KB Output is correct
32 Correct 22 ms 12280 KB Output is correct
33 Correct 1296 ms 17440 KB Output is correct
34 Correct 348 ms 17652 KB Output is correct
35 Correct 1226 ms 16500 KB Output is correct
36 Correct 1916 ms 21148 KB Output is correct
37 Correct 42 ms 15096 KB Output is correct
38 Correct 2072 ms 21964 KB Output is correct
39 Correct 2128 ms 21956 KB Output is correct
40 Correct 2105 ms 21960 KB Output is correct
41 Correct 2156 ms 21964 KB Output is correct
42 Correct 1994 ms 21980 KB Output is correct
43 Correct 2047 ms 21960 KB Output is correct
44 Correct 1929 ms 21968 KB Output is correct
45 Correct 2029 ms 21972 KB Output is correct
46 Correct 2035 ms 22216 KB Output is correct
47 Correct 2076 ms 22220 KB Output is correct
48 Correct 457 ms 19036 KB Output is correct
49 Correct 471 ms 20768 KB Output is correct
50 Correct 203 ms 14456 KB Output is correct
51 Correct 207 ms 15708 KB Output is correct
52 Correct 94 ms 14072 KB Output is correct
53 Correct 851 ms 20908 KB Output is correct
54 Correct 683 ms 16520 KB Output is correct
55 Correct 1746 ms 19828 KB Output is correct
56 Correct 1139 ms 17184 KB Output is correct
57 Correct 1431 ms 20268 KB Output is correct
58 Correct 73 ms 15732 KB Output is correct
59 Correct 201 ms 15580 KB Output is correct
60 Correct 409 ms 19320 KB Output is correct
61 Correct 482 ms 19652 KB Output is correct
62 Correct 273 ms 18100 KB Output is correct
63 Correct 134 ms 19448 KB Output is correct
64 Correct 151 ms 21112 KB Output is correct
65 Correct 147 ms 25896 KB Output is correct
66 Correct 290 ms 16092 KB Output is correct
67 Correct 208 ms 22008 KB Output is correct
68 Correct 449 ms 25592 KB Output is correct
69 Correct 167 ms 13828 KB Output is correct
70 Correct 59 ms 12536 KB Output is correct
71 Correct 228 ms 19296 KB Output is correct
72 Correct 295 ms 24568 KB Output is correct
73 Correct 671 ms 29304 KB Output is correct
74 Correct 708 ms 25080 KB Output is correct
75 Correct 517 ms 29252 KB Output is correct
76 Correct 518 ms 27640 KB Output is correct
77 Correct 690 ms 25464 KB Output is correct