Submission #435638

# Submission time Handle Problem Language Result Execution time Memory
435638 2021-06-23T13:55:09 Z PinkRabbit Fountain Parks (IOI21_parks) C++17
55 / 100
2956 ms 79420 KB
// PinkRabbit

#include "parks.h"
#include <algorithm>
#include <vector>
#include <map>
#include <ctime>
#include <random>
#include <chrono>

std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
inline T range(T l, T r) {
	return std::uniform_int_distribution<T>(l, r)(rng);
}

template<typename T>
inline void vec2arr(std::vector<T> v, T *arr, T bias = 0) {
	int n = (int)v.size();
	for (int i = 1; i <= n; ++i)
		arr[i] = v[i - 1] + bias;
}
template<typename T>
inline std::vector<T> arr2vec(T *arr, int n, T bias = 0) {
	std::vector<T> v(n);
	for (int i = 1; i <= n; ++i)
		v[i - 1] = arr[i] + bias;
	return v;
}

typedef std::vector<int> VI;
const int MN = 200005;
const int MV = 100005;

const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

int N;
int x[MN], y[MN];
int ndir[MN][4], edir[MN][4];
std::map<int, int> mp[MV];
int eu[MN * 2], ev[MN * 2], M;

int per[MN * 2], iper[MN * 2];
int AnsU[MN], AnsV[MN], AnsA[MN], AnsB[MN];
int pa[MN];
int fp(int u) {
	return pa[u] ? pa[u] = fp(pa[u]) : u;
}
inline bool GenTree() {
	int tot = 0;
	for (int i = 1; i <= N; ++i)
		pa[i] = 0;
	std::shuffle(per + 1, per + M + 1, rng);
	for (int i = 1; i <= M; ++i)
		iper[i] = 0;
	for (int id = 1; id <= M; ++id) {
		int u = eu[per[id]], v = ev[per[id]];
		int pu = fp(u), pv = fp(v);
		if (pu != pv) {
			++tot;
			AnsU[tot] = u;
			AnsV[tot] = v;
			pa[pu] = pv;
			iper[per[id]] = tot;
		}
	}
	return tot == N - 1;
}

VI G[MN * 2];
int bel[MN * 2], scnt;
int dfn[MN * 2], low[MN * 2], dfc;
int stk[MN * 2], top;
bool instk[MN * 2];
void Tarjan(int u) {
	dfn[u] = low[u] = ++dfc;
	instk[stk[++top] = u] = true;
	for (int v : G[u]) {
		if (!dfn[v]) {
			Tarjan(v);
			low[u] = std::min(low[u], low[v]);
		} else if (instk[v])
			low[u] = std::min(low[u], dfn[v]);
	}
	if (low[u] == dfn[u]) {
		++scnt;
		while (true) {
			int w = stk[top--];
			instk[w] = false;
			bel[w] = scnt;
			if (w == u)
				break;
		}
	}
}
inline bool Solve() {
	for (int i = 1; i <= 2 * (N - 1); ++i)
		G[i].clear(), dfn[i] = low[i] = 0;
	auto AddE = [&](int u, int tu, int v, int tv) -> void {
		if (!u || !v || !iper[u] || !iper[v])
			return ;
		u = iper[u], v = iper[v];
		G[u + (tu ? N - 1 : 0)].push_back(v + (tv ? 0 : N - 1));
		G[v + (tv ? N - 1 : 0)].push_back(u + (tu ? 0 : N - 1));
	};
	for (int i = 1; i <= N; ++i) {
		AddE(edir[i][0], 1, edir[i][1], 1);
		AddE(edir[i][1], 0, edir[i][2], 1);
		AddE(edir[i][2], 0, edir[i][3], 0);
		AddE(edir[i][3], 1, edir[i][0], 0);
		if (ndir[i][0] && ndir[i][1] && ndir[ndir[i][0]][1]) {
			AddE(edir[i][0], 1, edir[ndir[i][1]][0], 0);
			AddE(edir[i][1], 1, edir[ndir[i][0]][1], 0);
		}
	}
	dfc = scnt = 0;
	for (int u = 1; u <= 2 * (N - 1); ++u)
		if (!dfn[u])
			Tarjan(u);
	for (int i = 1; i <= N - 1; ++i) {
		if (bel[i] == bel[i + N - 1])
			return false;
		int d = bel[i + N - 1] < bel[i];
		AnsA[i] = x[AnsU[i]] + x[AnsV[i]];
		AnsB[i] = y[AnsU[i]] + y[AnsV[i]];
		if (x[AnsU[i]] == x[AnsV[i]])
			AnsA[i] += (d ? 1 : -1);
		if (y[AnsU[i]] == y[AnsV[i]])
			AnsB[i] += (d ? 1 : -1);
	}
	build(arr2vec(AnsU, N - 1, -1), arr2vec(AnsV, N - 1, -1), arr2vec(AnsA, N - 1), arr2vec(AnsB, N - 1));
	return true;
}

int construct_roads(VI t_x, VI t_y) {
	clock_t tstart = clock();
	N = (int)t_x.size();
	vec2arr(t_x, x);
	vec2arr(t_y, y);
	for (int i = 1; i <= N; ++i) {
		x[i] /= 2;
		y[i] /= 2;
		mp[x[i]][y[i]] = i;
	}
	for (int i = 1; i <= N; ++i) {
		for (int d = 0; d < 2; ++d) {
			int nx = x[i] + dx[d];
			int ny = y[i] + dy[d];
			auto it = mp[nx].find(ny);
			if (it != mp[nx].end()) {
				++M;
				eu[M] = i;
				ev[M] = it -> second;
				if (rng() & 1)
					std::swap(eu[M], ev[M]);
				ndir[i][d] = it->second;
				ndir[it->second][d ^ 2] = i;
				edir[i][d] = M;
				edir[it->second][d ^ 2] = M;
			}
		}
	}
	for (int i = 1; i <= M; ++i)
		per[i] = i;
	if (!GenTree())
		return 0;
	if (Solve())
		return 1;
	while ((clock() - tstart) / (double)CLOCKS_PER_SEC < 2.7) {
		GenTree();
		if (Solve())
			return 1;
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14412 KB Output is correct
2 Correct 18 ms 14492 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14412 KB Output is correct
5 Correct 11 ms 14400 KB Output is correct
6 Correct 12 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 12 ms 14412 KB Output is correct
9 Correct 176 ms 35016 KB Output is correct
10 Correct 20 ms 16588 KB Output is correct
11 Correct 65 ms 25472 KB Output is correct
12 Correct 26 ms 17616 KB Output is correct
13 Correct 50 ms 20048 KB Output is correct
14 Correct 11 ms 14584 KB Output is correct
15 Correct 12 ms 14632 KB Output is correct
16 Correct 180 ms 34988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14412 KB Output is correct
2 Correct 18 ms 14492 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14412 KB Output is correct
5 Correct 11 ms 14400 KB Output is correct
6 Correct 12 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 12 ms 14412 KB Output is correct
9 Correct 176 ms 35016 KB Output is correct
10 Correct 20 ms 16588 KB Output is correct
11 Correct 65 ms 25472 KB Output is correct
12 Correct 26 ms 17616 KB Output is correct
13 Correct 50 ms 20048 KB Output is correct
14 Correct 11 ms 14584 KB Output is correct
15 Correct 12 ms 14632 KB Output is correct
16 Correct 180 ms 34988 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14412 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 12 ms 14440 KB Output is correct
21 Correct 11 ms 14412 KB Output is correct
22 Correct 11 ms 14464 KB Output is correct
23 Correct 725 ms 65452 KB Output is correct
24 Correct 11 ms 14412 KB Output is correct
25 Correct 14 ms 14748 KB Output is correct
26 Correct 13 ms 14796 KB Output is correct
27 Correct 14 ms 14936 KB Output is correct
28 Correct 206 ms 34668 KB Output is correct
29 Correct 348 ms 45028 KB Output is correct
30 Correct 523 ms 55232 KB Output is correct
31 Correct 730 ms 65436 KB Output is correct
32 Correct 11 ms 14496 KB Output is correct
33 Correct 11 ms 14412 KB Output is correct
34 Correct 10 ms 14444 KB Output is correct
35 Correct 10 ms 14308 KB Output is correct
36 Correct 11 ms 14284 KB Output is correct
37 Correct 11 ms 14396 KB Output is correct
38 Correct 10 ms 14412 KB Output is correct
39 Correct 10 ms 14412 KB Output is correct
40 Correct 11 ms 14412 KB Output is correct
41 Correct 11 ms 14412 KB Output is correct
42 Correct 10 ms 14376 KB Output is correct
43 Correct 12 ms 14668 KB Output is correct
44 Correct 14 ms 14796 KB Output is correct
45 Correct 254 ms 38956 KB Output is correct
46 Correct 402 ms 50200 KB Output is correct
47 Correct 450 ms 50092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14412 KB Output is correct
2 Correct 18 ms 14492 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14412 KB Output is correct
5 Correct 11 ms 14400 KB Output is correct
6 Correct 12 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 12 ms 14412 KB Output is correct
9 Correct 176 ms 35016 KB Output is correct
10 Correct 20 ms 16588 KB Output is correct
11 Correct 65 ms 25472 KB Output is correct
12 Correct 26 ms 17616 KB Output is correct
13 Correct 50 ms 20048 KB Output is correct
14 Correct 11 ms 14584 KB Output is correct
15 Correct 12 ms 14632 KB Output is correct
16 Correct 180 ms 34988 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14412 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 12 ms 14440 KB Output is correct
21 Correct 11 ms 14412 KB Output is correct
22 Correct 11 ms 14464 KB Output is correct
23 Correct 725 ms 65452 KB Output is correct
24 Correct 11 ms 14412 KB Output is correct
25 Correct 14 ms 14748 KB Output is correct
26 Correct 13 ms 14796 KB Output is correct
27 Correct 14 ms 14936 KB Output is correct
28 Correct 206 ms 34668 KB Output is correct
29 Correct 348 ms 45028 KB Output is correct
30 Correct 523 ms 55232 KB Output is correct
31 Correct 730 ms 65436 KB Output is correct
32 Correct 11 ms 14496 KB Output is correct
33 Correct 11 ms 14412 KB Output is correct
34 Correct 10 ms 14444 KB Output is correct
35 Correct 10 ms 14308 KB Output is correct
36 Correct 11 ms 14284 KB Output is correct
37 Correct 11 ms 14396 KB Output is correct
38 Correct 10 ms 14412 KB Output is correct
39 Correct 10 ms 14412 KB Output is correct
40 Correct 11 ms 14412 KB Output is correct
41 Correct 11 ms 14412 KB Output is correct
42 Correct 10 ms 14376 KB Output is correct
43 Correct 12 ms 14668 KB Output is correct
44 Correct 14 ms 14796 KB Output is correct
45 Correct 254 ms 38956 KB Output is correct
46 Correct 402 ms 50200 KB Output is correct
47 Correct 450 ms 50092 KB Output is correct
48 Correct 10 ms 14412 KB Output is correct
49 Correct 10 ms 14432 KB Output is correct
50 Correct 10 ms 14372 KB Output is correct
51 Correct 10 ms 14380 KB Output is correct
52 Correct 10 ms 14412 KB Output is correct
53 Correct 9 ms 14368 KB Output is correct
54 Correct 10 ms 14412 KB Output is correct
55 Incorrect 2956 ms 60004 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14412 KB Output is correct
2 Correct 18 ms 14492 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14412 KB Output is correct
5 Correct 11 ms 14400 KB Output is correct
6 Correct 12 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 12 ms 14412 KB Output is correct
9 Correct 176 ms 35016 KB Output is correct
10 Correct 20 ms 16588 KB Output is correct
11 Correct 65 ms 25472 KB Output is correct
12 Correct 26 ms 17616 KB Output is correct
13 Correct 50 ms 20048 KB Output is correct
14 Correct 11 ms 14584 KB Output is correct
15 Correct 12 ms 14632 KB Output is correct
16 Correct 180 ms 34988 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 12 ms 14388 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 615 ms 79216 KB Output is correct
21 Correct 613 ms 76576 KB Output is correct
22 Correct 596 ms 75436 KB Output is correct
23 Correct 292 ms 50084 KB Output is correct
24 Correct 238 ms 29368 KB Output is correct
25 Correct 397 ms 40116 KB Output is correct
26 Correct 301 ms 40372 KB Output is correct
27 Correct 388 ms 55860 KB Output is correct
28 Correct 401 ms 55844 KB Output is correct
29 Correct 445 ms 55800 KB Output is correct
30 Correct 458 ms 55832 KB Output is correct
31 Correct 11 ms 14412 KB Output is correct
32 Correct 34 ms 18064 KB Output is correct
33 Correct 70 ms 21828 KB Output is correct
34 Correct 696 ms 77744 KB Output is correct
35 Correct 18 ms 15684 KB Output is correct
36 Correct 56 ms 20868 KB Output is correct
37 Correct 143 ms 27224 KB Output is correct
38 Correct 213 ms 35392 KB Output is correct
39 Correct 274 ms 43164 KB Output is correct
40 Correct 410 ms 51136 KB Output is correct
41 Correct 561 ms 59068 KB Output is correct
42 Correct 626 ms 66880 KB Output is correct
43 Correct 12 ms 14412 KB Output is correct
44 Correct 12 ms 14412 KB Output is correct
45 Correct 10 ms 14412 KB Output is correct
46 Correct 9 ms 14412 KB Output is correct
47 Correct 10 ms 14412 KB Output is correct
48 Correct 9 ms 14412 KB Output is correct
49 Correct 10 ms 14412 KB Output is correct
50 Correct 11 ms 14412 KB Output is correct
51 Correct 11 ms 14496 KB Output is correct
52 Correct 12 ms 14412 KB Output is correct
53 Correct 10 ms 14412 KB Output is correct
54 Correct 17 ms 14660 KB Output is correct
55 Correct 14 ms 14836 KB Output is correct
56 Correct 236 ms 39020 KB Output is correct
57 Correct 373 ms 50072 KB Output is correct
58 Correct 388 ms 50068 KB Output is correct
59 Correct 10 ms 14412 KB Output is correct
60 Correct 10 ms 14412 KB Output is correct
61 Correct 14 ms 14316 KB Output is correct
62 Correct 425 ms 55852 KB Output is correct
63 Correct 437 ms 55792 KB Output is correct
64 Correct 393 ms 55708 KB Output is correct
65 Correct 14 ms 14924 KB Output is correct
66 Correct 18 ms 15488 KB Output is correct
67 Correct 257 ms 39176 KB Output is correct
68 Correct 450 ms 51744 KB Output is correct
69 Correct 656 ms 64152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14412 KB Output is correct
2 Correct 18 ms 14492 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14412 KB Output is correct
5 Correct 11 ms 14400 KB Output is correct
6 Correct 12 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 12 ms 14412 KB Output is correct
9 Correct 176 ms 35016 KB Output is correct
10 Correct 20 ms 16588 KB Output is correct
11 Correct 65 ms 25472 KB Output is correct
12 Correct 26 ms 17616 KB Output is correct
13 Correct 50 ms 20048 KB Output is correct
14 Correct 11 ms 14584 KB Output is correct
15 Correct 12 ms 14632 KB Output is correct
16 Correct 180 ms 34988 KB Output is correct
17 Correct 381 ms 56216 KB Output is correct
18 Correct 394 ms 56396 KB Output is correct
19 Correct 598 ms 79420 KB Output is correct
20 Correct 631 ms 64992 KB Output is correct
21 Correct 518 ms 54900 KB Output is correct
22 Correct 15 ms 14412 KB Output is correct
23 Correct 63 ms 22604 KB Output is correct
24 Correct 28 ms 16972 KB Output is correct
25 Correct 89 ms 23760 KB Output is correct
26 Correct 187 ms 30356 KB Output is correct
27 Correct 272 ms 40492 KB Output is correct
28 Correct 359 ms 47156 KB Output is correct
29 Correct 477 ms 53740 KB Output is correct
30 Correct 518 ms 60048 KB Output is correct
31 Correct 666 ms 66864 KB Output is correct
32 Correct 634 ms 63192 KB Output is correct
33 Correct 483 ms 55804 KB Output is correct
34 Correct 14 ms 15052 KB Output is correct
35 Correct 23 ms 15548 KB Output is correct
36 Correct 258 ms 39216 KB Output is correct
37 Correct 466 ms 51836 KB Output is correct
38 Correct 688 ms 64160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14412 KB Output is correct
2 Correct 18 ms 14492 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14412 KB Output is correct
5 Correct 11 ms 14400 KB Output is correct
6 Correct 12 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 12 ms 14412 KB Output is correct
9 Correct 176 ms 35016 KB Output is correct
10 Correct 20 ms 16588 KB Output is correct
11 Correct 65 ms 25472 KB Output is correct
12 Correct 26 ms 17616 KB Output is correct
13 Correct 50 ms 20048 KB Output is correct
14 Correct 11 ms 14584 KB Output is correct
15 Correct 12 ms 14632 KB Output is correct
16 Correct 180 ms 34988 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14412 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 12 ms 14440 KB Output is correct
21 Correct 11 ms 14412 KB Output is correct
22 Correct 11 ms 14464 KB Output is correct
23 Correct 725 ms 65452 KB Output is correct
24 Correct 11 ms 14412 KB Output is correct
25 Correct 14 ms 14748 KB Output is correct
26 Correct 13 ms 14796 KB Output is correct
27 Correct 14 ms 14936 KB Output is correct
28 Correct 206 ms 34668 KB Output is correct
29 Correct 348 ms 45028 KB Output is correct
30 Correct 523 ms 55232 KB Output is correct
31 Correct 730 ms 65436 KB Output is correct
32 Correct 11 ms 14496 KB Output is correct
33 Correct 11 ms 14412 KB Output is correct
34 Correct 10 ms 14444 KB Output is correct
35 Correct 10 ms 14308 KB Output is correct
36 Correct 11 ms 14284 KB Output is correct
37 Correct 11 ms 14396 KB Output is correct
38 Correct 10 ms 14412 KB Output is correct
39 Correct 10 ms 14412 KB Output is correct
40 Correct 11 ms 14412 KB Output is correct
41 Correct 11 ms 14412 KB Output is correct
42 Correct 10 ms 14376 KB Output is correct
43 Correct 12 ms 14668 KB Output is correct
44 Correct 14 ms 14796 KB Output is correct
45 Correct 254 ms 38956 KB Output is correct
46 Correct 402 ms 50200 KB Output is correct
47 Correct 450 ms 50092 KB Output is correct
48 Correct 10 ms 14412 KB Output is correct
49 Correct 10 ms 14432 KB Output is correct
50 Correct 10 ms 14372 KB Output is correct
51 Correct 10 ms 14380 KB Output is correct
52 Correct 10 ms 14412 KB Output is correct
53 Correct 9 ms 14368 KB Output is correct
54 Correct 10 ms 14412 KB Output is correct
55 Incorrect 2956 ms 60004 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -