Submission #589383

# Submission time Handle Problem Language Result Execution time Memory
589383 2022-07-04T14:25:35 Z Virv Parachute rings (IOI12_rings) C++17
100 / 100
1647 ms 113852 KB
#include <numeric>
#include <vector>

struct DSU {
	std::vector<int> p, r;

	explicit DSU(int n) : p(n), r(n, 1) { iota(p.begin(), p.end(), 0); }

	int find(int i) { return i == p[i] ? i : p[i] = find(p[i]); }

	bool merge(int i, int j) {
		i = find(i);
		j = find(j);
		if (i == j) return false;
		if (r[i] > r[j]) std::swap(i, j);
		p[i] = j;
		r[j] += r[i];
		return true;
	}
};

struct World {
	std::vector<int> c;
	DSU				 dsu;
	int				 killed;

	explicit World(int n) : c(n), dsu(n), killed(-1) {}
};

static int							 n;
static std::vector<World>			 worlds;
static std::vector<std::vector<int>> g;
static bool							 many;
static int							 cyc;

void Init(int n) {
	::n = n;
	worlds.emplace_back(n);
	g.assign(n, {});
	many = false;
	cyc	 = -1;
}

void Link(int a, int b) {
	if (many) {
		for (int i = worlds.size(); i--;) {
			auto &[c, dsu, k] = worlds[i];
			if (a == k || b == k) continue;

			c[a] += 1;
			c[b] += 1;
			if (c[a] > 2 || c[b] > 2 || !dsu.merge(a, b)) {
				worlds[i] = std::move(worlds.back());
				worlds.pop_back();
			}
		}
	} else {
		auto &[c, dsu, _] = worlds[0];

		g[a].push_back(b);
		g[b].push_back(a);

		c[a] += 1;
		c[b] += 1;
		if (c[a] > 2 || c[b] > 2) {
			auto			 x	= c[a] > 2 ? a : b;
			std::vector<int> ks = g[x];
			ks.push_back(x);

			worlds.clear();
			many = true;

			for (auto k : ks) {
				worlds.emplace_back(n);
				worlds.back().killed = k;
			}

			for (int i = 0; i < n; ++i)
				for (auto j : g[i])
					if (i < j) Link(i, j);
			return;
		}

		if (!dsu.merge(a, b)) {
			if (cyc == -1) {
				cyc = a;
			} else {
				worlds.clear();
				many = true;
			}
			return;
		}
	}
}

int CountCritical() {
	if (many) {
		return worlds.size();
	} else {
		auto &[_, dsu, __] = worlds[0];
		return cyc != -1 ? dsu.r[dsu.find(cyc)] : n;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 407 ms 41116 KB Output is correct
2 Correct 685 ms 81152 KB Output is correct
3 Correct 250 ms 71488 KB Output is correct
4 Correct 1039 ms 80188 KB Output is correct
5 Correct 1002 ms 80168 KB Output is correct
6 Correct 964 ms 78548 KB Output is correct
7 Correct 249 ms 71708 KB Output is correct
8 Correct 1366 ms 105120 KB Output is correct
9 Correct 1647 ms 113852 KB Output is correct
10 Correct 685 ms 77760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 2 ms 724 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 5 ms 1236 KB Output is correct
13 Correct 5 ms 1240 KB Output is correct
14 Correct 2 ms 980 KB Output is correct
15 Correct 3 ms 1748 KB Output is correct
16 Correct 4 ms 980 KB Output is correct
17 Correct 3 ms 980 KB Output is correct
18 Correct 3 ms 1748 KB Output is correct
19 Correct 4 ms 980 KB Output is correct
20 Correct 5 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 2 ms 724 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 5 ms 1236 KB Output is correct
13 Correct 5 ms 1240 KB Output is correct
14 Correct 2 ms 980 KB Output is correct
15 Correct 3 ms 1748 KB Output is correct
16 Correct 4 ms 980 KB Output is correct
17 Correct 3 ms 980 KB Output is correct
18 Correct 3 ms 1748 KB Output is correct
19 Correct 4 ms 980 KB Output is correct
20 Correct 5 ms 1236 KB Output is correct
21 Correct 15 ms 3028 KB Output is correct
22 Correct 29 ms 4688 KB Output is correct
23 Correct 36 ms 5932 KB Output is correct
24 Correct 27 ms 6908 KB Output is correct
25 Correct 14 ms 7368 KB Output is correct
26 Correct 26 ms 7108 KB Output is correct
27 Correct 32 ms 5580 KB Output is correct
28 Correct 21 ms 6636 KB Output is correct
29 Correct 23 ms 7468 KB Output is correct
30 Correct 44 ms 6892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 2 ms 724 KB Output is correct
11 Correct 407 ms 41116 KB Output is correct
12 Correct 685 ms 81152 KB Output is correct
13 Correct 250 ms 71488 KB Output is correct
14 Correct 1039 ms 80188 KB Output is correct
15 Correct 1002 ms 80168 KB Output is correct
16 Correct 964 ms 78548 KB Output is correct
17 Correct 249 ms 71708 KB Output is correct
18 Correct 1366 ms 105120 KB Output is correct
19 Correct 1647 ms 113852 KB Output is correct
20 Correct 685 ms 77760 KB Output is correct
21 Correct 2 ms 724 KB Output is correct
22 Correct 5 ms 1236 KB Output is correct
23 Correct 5 ms 1240 KB Output is correct
24 Correct 2 ms 980 KB Output is correct
25 Correct 3 ms 1748 KB Output is correct
26 Correct 4 ms 980 KB Output is correct
27 Correct 3 ms 980 KB Output is correct
28 Correct 3 ms 1748 KB Output is correct
29 Correct 4 ms 980 KB Output is correct
30 Correct 5 ms 1236 KB Output is correct
31 Correct 15 ms 3028 KB Output is correct
32 Correct 29 ms 4688 KB Output is correct
33 Correct 36 ms 5932 KB Output is correct
34 Correct 27 ms 6908 KB Output is correct
35 Correct 14 ms 7368 KB Output is correct
36 Correct 26 ms 7108 KB Output is correct
37 Correct 32 ms 5580 KB Output is correct
38 Correct 21 ms 6636 KB Output is correct
39 Correct 23 ms 7468 KB Output is correct
40 Correct 44 ms 6892 KB Output is correct
41 Correct 216 ms 29928 KB Output is correct
42 Correct 671 ms 78680 KB Output is correct
43 Correct 196 ms 70576 KB Output is correct
44 Correct 241 ms 65728 KB Output is correct
45 Correct 361 ms 74208 KB Output is correct
46 Correct 615 ms 66608 KB Output is correct
47 Correct 856 ms 68552 KB Output is correct
48 Correct 231 ms 72040 KB Output is correct
49 Correct 640 ms 71740 KB Output is correct
50 Correct 666 ms 70936 KB Output is correct
51 Correct 196 ms 60960 KB Output is correct
52 Correct 223 ms 52612 KB Output is correct
53 Correct 199 ms 71308 KB Output is correct
54 Correct 1000 ms 84352 KB Output is correct
55 Correct 423 ms 74952 KB Output is correct