Submission #435379

# Submission time Handle Problem Language Result Execution time Memory
435379 2021-06-23T09:25:15 Z fleimgruber Fountain Parks (IOI21_parks) C++17
55 / 100
1161 ms 78612 KB
// --- algolib/bipartite-matching.h ---
// bipartite matching, that optionally finds the bipartition
// some of the functionality is enabled depending on FindBipartition, because
// in the !FindBipartition indices from left and right side coincide, making
// it impossible to provide reasonable return values
// if the graph is not bipartite, max_matching == -1. don't use the other
// functions in such cases.
// usage:
// 	- construct with either the adjacency list of a graph, or
//		((size left), (size right), adjacency list, containing only l -> r edges)
//	  for the ladder case, vertices are number [0..l[, [0..r[
// - access matching size using operator int()
// - access matched vertex using [vertex] (first constructor) or
//		partner(vertex, left_side)
// - if vertex numbers are distinct, reconstruct the min vertex cover (koenig's
//	 theorem) using min_vertex_cover()
#include <algorithm>
#include <type_traits>
#include <vector>

template<bool FindBipartition = true>
class MaxBipartiteMatching {
	using Graph = std::vector<std::vector<int>>;

	Graph e;
	int max_matching = 0;	
	std::vector<bool> vis, on_left;
	std::vector<int> match, left;

	bool dfs_bipartition(int i, bool is_left) { // false, if not bipartite
		vis[i] = true;
		on_left[i] = is_left;
		if (is_left)
			left.push_back(i);
		for (int j : e[i])
			if (!vis[j])
				dfs_bipartition(i, !is_left);
			else if (is_left == on_left[j])
				return false;
		return true;
	}

	bool augment(int i) {
		for (int j : e[i])
			if (!vis[j]) {
				vis[j] = true;
				if (match[j] == -1 || augment(match[j])) {
					match[i] = j;
					match[j] = i;
					return true;
				}
			}
		return false;
	}

	void find_matching() {
		int last;
		do {
			last = max_matching;
			if constexpr (FindBipartition) {
				std::fill(vis.begin(), vis.end(), false);
				for (int i : left)
					if (match[i] == -1)
						max_matching += augment(i);
			} else {
				int size_left = left[0];
				std::fill(vis.begin() + size_left, vis.end(), false);
				for (int i = 0; i < size_left; i++)
					if (match[i] == -1)
						max_matching += augment(i);
			}
		} while (max_matching > last);
	}

	void dfs_cover(int i) {
		vis[i] = true;
		for (int j : e[i])
			if (!vis[j] && match[i] != j) {
				vis[j] = true;
				if (int k = match[j]; k != -1 && !vis[k])
					dfs_cover(k);
			}
	}

public:
	// given any graph, finds bipartition and matching
	template<bool X = FindBipartition, typename std::enable_if_t<X, bool> = false>
	MaxBipartiteMatching(const Graph& e_) : e(e_), vis(e.size()),
		on_left(e.size()), match(e.size(), -1) {
		for (size_t i = 0; i < e.size(); i++) // bipartition
			if (!vis[i] && !dfs_bipartition(i, true)) {
				max_matching = -1;
				return;
			}
		find_matching();
	}
	
	// l vertices [0..l[ on the left, r vertices [0..r[ on the right
	// only left -> right edges given
	// in theory, a vis array of size r suffices. but in order to make the
	// code work with the other constructor, everything becomes terrible
	template<bool X = FindBipartition, typename std::enable_if_t<!X, bool> = true>	
	MaxBipartiteMatching(int l, int r, const Graph& e_) : e(e_), vis(l + r),
		match(l + r, -1) {
		left.push_back(l);
		for (auto& v : e) // shift [0..r[ by l
			for (int& i : v)
				i += l;
		find_matching(); // right -> left edges are not needed
	}

	// -1, if the graph is not bipartite
	operator int() { return max_matching; } // == size(min_vertex_cover)
	
	// Use either [left] or partner. returns -1 if there's no match
	template<bool X = FindBipartition>
	typename std::enable_if_t<X, int> operator[](int i) { return match[i]; }
	
	template<bool X = FindBipartition>
	typename std::enable_if_t<!X, int> partner(int i, bool left_side = true) {
		if (left_side) {
			int m = match[i];
			return m == -1 ? -1 : m - left[0];
		}
		return match[i + left[0]];
	}

	template<bool X = FindBipartition>
	typename std::enable_if_t<X, std::vector<int>> min_vertex_cover() {
		std::fill(vis.begin(), vis.end(), false);
		for (int i : left)
			if (match[i] == -1)
				dfs_cover(i);
		std::vector<int> cover;
		cover.reserve(max_matching);
		for (size_t i = 0; i < vis.size(); i++)
			if ((!on_left[i]) == vis[i])
				cover.push_back(i);
		return cover;
	}
};
// ----------------
// --- algolib/union-find.h ---
#include <algorithm>
#include <numeric>
#include <vector>

struct UnionFind {
	std::vector<int> p, size;

	UnionFind(int n) : p(n), size(n, 1) {
		std::iota(p.begin(), p.end(), 0);
	}

	int create() {
		int r = p.size();
		p.push_back(r);
		size.push_back(1);
		return r;
	}

	int find(int i) {
		if (i == p[i])
			return i;
		return p[i] = find(p[i]);
	}
	int operator[](int i) { return find(i); }
	bool connected(int a, int b) { return find(a) == find(b); }

	bool connect(int a, int b) {
		a = find(a), b = find(b);
		if (a == b)
			return false;
		if (size[a] > size[b])
			std::swap(a, b);
		size[b] += size[a];
		p[a] = b;
		return true;
	}
};
// ----------------
#include "parks.h"
#include <bits/stdc++.h>

using namespace std;

using Point = pair<int, int>;
map<Point, int> all/*points in input*/, bench; // position => id
vector<Point> v_bench; // id => position
vector<int> u, v;

bool has(map<Point, int>& map, int x, int y) {
	return map.find({ x, y }) != map.end();
}

int id_of_bench(int x, int y) {
	if (!has(bench, x, y)) {
		int id = bench.size();
		bench.insert({{ x, y }, id});
		v_bench.push_back({ x, y });
		return id;
	}
	return bench[{ x, y }];
}

bool build_spanning_tree(vector<int>& X, vector<int>& Y) {
	int n = X.size();
	struct Edge {
		int i, j; // ids
		int weight;
	};
	vector<Edge> edges;
	for (int i = 0; i < n; i++) {
		int x = X[i], y = Y[i], cnt = 0;
		for (auto [xp, yp] : vector<Point>{{ x+2, y }, { x, y+2 }})
			if (has(all, xp, yp)) {
				cnt++;
				int j = all[{xp, yp}];
				edges.push_back({ i, j, 0 });
			}
		// 2x2 square => extra priority?
		if (cnt == 2 && has(all, x+2, y+2)) {
			edges[edges.size() - 2].weight = 2;
			edges[edges.size() - 1].weight = 1;
		}
	}
	// mst
	sort(edges.begin(), edges.end(), [](auto& a, auto& b) {
		return a.weight > b.weight;
	});
	UnionFind dsu(n);
	for (auto [i, j, _] : edges)
		if (dsu.connect(i, j)) {
			u.push_back(i);
			v.push_back(j);
			if (int(u.size()) == n-1)
				return true;
		}
	return n == 1;
}

int construct_roads(vector<int> X, vector<int> Y) {
	int n = X.size();
	for (int i = 0; i < n; i++)
		all.insert({{ X[i], Y[i] }, i});
	if (!build_spanning_tree(X, Y))
		return 0;
	vector<vector<int>> g(n - 1); // left side = spanning tree edges
	for (int i = 0; i < n - 1; i++)
		if (X[u[i]] == X[v[i]]) { // vertical
			int x = X[u[i]], y = max(Y[u[i]], Y[v[i]]) - 1;
			g[i].push_back(id_of_bench(x-1, y));
			g[i].push_back(id_of_bench(x+1, y));
		} else { // horizontal
			int x = max(X[u[i]], X[v[i]]) - 1, y = Y[u[i]];
			g[i].push_back(id_of_bench(x, y-1));
			g[i].push_back(id_of_bench(x, y+1));
		}
	int m = bench.size();
	MaxBipartiteMatching<false> matching(n-1, m, g);
	if (matching != n-1)
		return 0;
	vector<int> a, b;
	for (int i = 0; i < n-1; i++) {
		auto [x, y] = v_bench[matching.partner(i)];
		a.push_back(x);
		b.push_back(y);
	}
	build(u, v, a, b);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 351 ms 39572 KB Output is correct
10 Correct 20 ms 4152 KB Output is correct
11 Correct 112 ms 21292 KB Output is correct
12 Correct 31 ms 6108 KB Output is correct
13 Correct 39 ms 5184 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 388 ms 39660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 351 ms 39572 KB Output is correct
10 Correct 20 ms 4152 KB Output is correct
11 Correct 112 ms 21292 KB Output is correct
12 Correct 31 ms 6108 KB Output is correct
13 Correct 39 ms 5184 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 388 ms 39660 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1002 ms 66364 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 3 ms 588 KB Output is correct
26 Correct 3 ms 588 KB Output is correct
27 Correct 5 ms 716 KB Output is correct
28 Correct 258 ms 25780 KB Output is correct
29 Correct 485 ms 39892 KB Output is correct
30 Correct 684 ms 50604 KB Output is correct
31 Correct 1018 ms 66400 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 2 ms 460 KB Output is correct
44 Correct 3 ms 636 KB Output is correct
45 Correct 408 ms 35056 KB Output is correct
46 Correct 641 ms 50080 KB Output is correct
47 Correct 649 ms 49976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 351 ms 39572 KB Output is correct
10 Correct 20 ms 4152 KB Output is correct
11 Correct 112 ms 21292 KB Output is correct
12 Correct 31 ms 6108 KB Output is correct
13 Correct 39 ms 5184 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 388 ms 39660 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1002 ms 66364 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 3 ms 588 KB Output is correct
26 Correct 3 ms 588 KB Output is correct
27 Correct 5 ms 716 KB Output is correct
28 Correct 258 ms 25780 KB Output is correct
29 Correct 485 ms 39892 KB Output is correct
30 Correct 684 ms 50604 KB Output is correct
31 Correct 1018 ms 66400 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 2 ms 460 KB Output is correct
44 Correct 3 ms 636 KB Output is correct
45 Correct 408 ms 35056 KB Output is correct
46 Correct 641 ms 50080 KB Output is correct
47 Correct 649 ms 49976 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Incorrect 1016 ms 54832 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 351 ms 39572 KB Output is correct
10 Correct 20 ms 4152 KB Output is correct
11 Correct 112 ms 21292 KB Output is correct
12 Correct 31 ms 6108 KB Output is correct
13 Correct 39 ms 5184 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 388 ms 39660 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 934 ms 64252 KB Output is correct
21 Correct 970 ms 63552 KB Output is correct
22 Correct 893 ms 63900 KB Output is correct
23 Correct 747 ms 66228 KB Output is correct
24 Correct 409 ms 17480 KB Output is correct
25 Correct 574 ms 22760 KB Output is correct
26 Correct 461 ms 22692 KB Output is correct
27 Correct 989 ms 78360 KB Output is correct
28 Correct 950 ms 78408 KB Output is correct
29 Correct 1100 ms 78432 KB Output is correct
30 Correct 1161 ms 78500 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 37 ms 5028 KB Output is correct
33 Correct 174 ms 8884 KB Output is correct
34 Correct 864 ms 63648 KB Output is correct
35 Correct 14 ms 1424 KB Output is correct
36 Correct 77 ms 5844 KB Output is correct
37 Correct 189 ms 11308 KB Output is correct
38 Correct 295 ms 26920 KB Output is correct
39 Correct 448 ms 36696 KB Output is correct
40 Correct 638 ms 46360 KB Output is correct
41 Correct 865 ms 56076 KB Output is correct
42 Correct 977 ms 66336 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 2 ms 460 KB Output is correct
55 Correct 5 ms 588 KB Output is correct
56 Correct 406 ms 34956 KB Output is correct
57 Correct 663 ms 50064 KB Output is correct
58 Correct 682 ms 50248 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 972 ms 78608 KB Output is correct
63 Correct 898 ms 78356 KB Output is correct
64 Correct 850 ms 78040 KB Output is correct
65 Correct 4 ms 716 KB Output is correct
66 Correct 9 ms 1100 KB Output is correct
67 Correct 381 ms 34660 KB Output is correct
68 Correct 758 ms 51080 KB Output is correct
69 Correct 995 ms 68468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 351 ms 39572 KB Output is correct
10 Correct 20 ms 4152 KB Output is correct
11 Correct 112 ms 21292 KB Output is correct
12 Correct 31 ms 6108 KB Output is correct
13 Correct 39 ms 5184 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 388 ms 39660 KB Output is correct
17 Correct 920 ms 78444 KB Output is correct
18 Correct 919 ms 78568 KB Output is correct
19 Correct 1025 ms 67284 KB Output is correct
20 Correct 1024 ms 65684 KB Output is correct
21 Correct 889 ms 64220 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 108 ms 11536 KB Output is correct
24 Correct 31 ms 2588 KB Output is correct
25 Correct 133 ms 8384 KB Output is correct
26 Correct 363 ms 13512 KB Output is correct
27 Correct 380 ms 33852 KB Output is correct
28 Correct 560 ms 44408 KB Output is correct
29 Correct 841 ms 50112 KB Output is correct
30 Correct 919 ms 58808 KB Output is correct
31 Correct 1157 ms 67124 KB Output is correct
32 Correct 1045 ms 70180 KB Output is correct
33 Correct 1027 ms 78612 KB Output is correct
34 Correct 5 ms 844 KB Output is correct
35 Correct 11 ms 1316 KB Output is correct
36 Correct 442 ms 34740 KB Output is correct
37 Correct 839 ms 51272 KB Output is correct
38 Correct 1016 ms 68688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 351 ms 39572 KB Output is correct
10 Correct 20 ms 4152 KB Output is correct
11 Correct 112 ms 21292 KB Output is correct
12 Correct 31 ms 6108 KB Output is correct
13 Correct 39 ms 5184 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 388 ms 39660 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1002 ms 66364 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 3 ms 588 KB Output is correct
26 Correct 3 ms 588 KB Output is correct
27 Correct 5 ms 716 KB Output is correct
28 Correct 258 ms 25780 KB Output is correct
29 Correct 485 ms 39892 KB Output is correct
30 Correct 684 ms 50604 KB Output is correct
31 Correct 1018 ms 66400 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 2 ms 460 KB Output is correct
44 Correct 3 ms 636 KB Output is correct
45 Correct 408 ms 35056 KB Output is correct
46 Correct 641 ms 50080 KB Output is correct
47 Correct 649 ms 49976 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Incorrect 1016 ms 54832 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -