Submission #105091

# Submission time Handle Problem Language Result Execution time Memory
105091 2019-04-10T14:19:02 Z eriksuenderhauf Parachute rings (IOI12_rings) C++11
100 / 100
1781 ms 110044 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
//#include "grader.h"
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
using namespace std;
const int MAXN = 1e6 + 5;
int par[MAXN], deg[MAXN], cycle = 0, cnt[MAXN], n = 0;
int mrk[MAXN];
vi cand, adj[MAXN];

struct DSU {
	int par[MAXN], deg[MAXN], root = 0;
	bool valid = true;
	int qry(int x) {
		if (!valid) return 0;
		return x == par[x] ? x : par[x] = qry(par[x]);
	}

	void join(int u, int v) {
		if (!valid) return;
		u = qry(u), v = qry(v);
		par[u] = v;
	}

	void init(int n) {
		for (int i = 0; i < n; i++)
			par[i] = i;
	}
} e, f;

int qry(int x) {
	return x == par[x] ? x : par[x] = qry(par[x]);
}

void join(int u, int v) {
	u = qry(u), v = qry(v);
	par[u] = v;
}

void Init(int N) {
	n = N;
	for (int i = 0; i < N; i++) {
		par[i] = i;
		cand.pb(i);
		mrk[i] = 1;
	}
}

void do3(int b) {
	vi tmp;
	for (int v: adj[b])
		if (mrk[v])
			tmp.pb(v);
	if (mrk[b])
		tmp.pb(b);
	for (int v: cand)
		mrk[v] = 0;
	for (int v: tmp)
		mrk[v] = 1;
	cand = tmp;
}

int par2[MAXN];
bool fin = false;
vii edg;

void dfs(int u, int p) {
	par2[u] = p;
	for (int v: adj[u])
		if (v != p)
			dfs(v, u);
}

void addE(int u, int v) {
	if (u == e.root || v == e.root || e.valid == false) return;
	e.deg[u]++, e.deg[v]++;
	if (e.qry(u) == e.qry(v) || e.deg[u] > 2 || e.deg[v] > 2)
		e.valid = false;
	e.join(u, v);
}
void addF(int u, int v) {
	if (u == f.root || v == f.root || f.valid == false) return;
	f.deg[u]++, f.deg[v]++;
	if (f.qry(u) == f.qry(v) || f.deg[u] > 2 || f.deg[v] > 2)
		f.valid = false;
	f.join(u, v);
}

void Link(int a, int b) {
	if (cand.empty()) return;
	if (fin) {
		addE(a, b);
		addF(a, b);
		return;
	}
	edg.pb({a, b});
	int delta = 0;
	if (qry(a) == qry(b) && cycle == 0) {
		dfs(a, -1);
		vi tmp = {a};
		int u = b;
		while (u != a) {
			tmp.pb(u);
			u = par2[u];
		}
		vi nx;
		for (int i: tmp)
			if (mrk[i] == 1) {
				nx.pb(i);
				mrk[i] = 2;
			}
		for (int i: cand)
			mrk[i]--;
		cand = nx;
	}
	adj[a].pb(b);
	adj[b].pb(a);
	if (qry(a) == qry(b)) {
		delta++;
		cycle++;
	}
	cnt[deg[a]]--;
	cnt[deg[b]]--;
	deg[a]++, deg[b]++;
	cnt[deg[a]]++;
	cnt[deg[b]]++;
	if (cnt[4] > 1) {
		cand = {};
		return;
	}
	if (deg[a] > deg[b])
		swap(a, b);
	if (deg[b] == 3) do3(b);
	if (deg[a] == 3) do3(a);
	if (deg[b] == 4) {
		if (!mrk[b]) {
			cand = {};
			return;
		}
		memset(mrk, 0, sizeof mrk);
		mrk[b] = 1;
		cand = {b};
	}
	join(a, b);
	if (cand.empty()) return;
	if (cycle > 1 && delta > 0) {
		if (qry(a) != qry(cand[0])) {
			cand = {};
			return;
		}
		if (mrk[a] && mrk[b]) {
			cand = {a, b};
			memset(mrk, 0, sizeof mrk);
			mrk[a] = 1, mrk[b] = 1;
		} else if (mrk[a] || mrk[b]) {
			if (mrk[a]) {
				memset(mrk, 0, sizeof mrk);
				mrk[a] = 1;
				cand = {a};
			} else {
				memset(mrk, 0, sizeof mrk);
				mrk[b] = 1;
				cand = {b};
			}
		}
		fin = true;
		if (cand.size() == 1) {
			f.valid = false;
			e.init(n);
			e.root = cand[0];
			for (int i = 0; i < edg.size(); i++) {
				int u, v;
				tie(u, v) = edg[i];
				if (u == cand[0] || v == cand[0]) continue;
				e.deg[u]++, e.deg[v]++;
				if (e.qry(u) == e.qry(v) || e.deg[u] > 2 || e.deg[v] > 2) {
					cand = {};
					return;
				}
				e.join(u, v);
			}
		} else {
			e.init(n);
			e.root = cand[0];
			for (int i = 0; e.valid && i < edg.size(); i++) {
				int u, v;
				tie(u, v) = edg[i];
				if (u == cand[0] || v == cand[0]) continue;
				e.deg[u]++, e.deg[v]++;
				if (e.qry(u) == e.qry(v) || e.deg[u] > 2 || e.deg[v] > 2)
					e.valid = false;
				e.join(u, v);
			}
			f.init(n);
			f.root = cand[1];
			for (int i = 0; f.valid && i < edg.size(); i++) {
				int u, v;
				tie(u, v) = edg[i];
				if (u == cand[1] || v == cand[1]) continue;
				f.deg[u]++, f.deg[v]++;
				if (f.qry(u) == f.qry(v) || f.deg[u] > 2 || f.deg[v] > 2)
					f.valid = false;
				f.join(u, v);
			}
		}
	}
}

int CountCritical() {
	int ret = 0;
	if (fin) {
		ret += (e.valid ? 1 : 0);
		ret += (f.valid ? 1 : 0);
		return ret;
	}
	return cand.size();
}

Compilation message

rings.cpp: In function 'void Link(int, int)':
rings.cpp:174:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; i < edg.size(); i++) {
                    ~~^~~~~~~~~~~~
rings.cpp:188:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; e.valid && i < edg.size(); i++) {
                               ~~^~~~~~~~~~~~
rings.cpp:199:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; f.valid && i < edg.size(); i++) {
                               ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23928 KB Output is correct
2 Correct 32 ms 28024 KB Output is correct
3 Correct 29 ms 28024 KB Output is correct
4 Correct 26 ms 23928 KB Output is correct
5 Correct 26 ms 24184 KB Output is correct
6 Correct 30 ms 24576 KB Output is correct
7 Correct 27 ms 24056 KB Output is correct
8 Correct 28 ms 24064 KB Output is correct
9 Correct 26 ms 24192 KB Output is correct
10 Correct 29 ms 24160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 587 ms 52468 KB Output is correct
2 Correct 1261 ms 68452 KB Output is correct
3 Correct 322 ms 40168 KB Output is correct
4 Correct 1566 ms 80160 KB Output is correct
5 Correct 1606 ms 83992 KB Output is correct
6 Correct 1781 ms 110044 KB Output is correct
7 Correct 319 ms 40168 KB Output is correct
8 Correct 1425 ms 75244 KB Output is correct
9 Correct 1612 ms 78688 KB Output is correct
10 Correct 990 ms 78292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23928 KB Output is correct
2 Correct 32 ms 28024 KB Output is correct
3 Correct 29 ms 28024 KB Output is correct
4 Correct 26 ms 23928 KB Output is correct
5 Correct 26 ms 24184 KB Output is correct
6 Correct 30 ms 24576 KB Output is correct
7 Correct 27 ms 24056 KB Output is correct
8 Correct 28 ms 24064 KB Output is correct
9 Correct 26 ms 24192 KB Output is correct
10 Correct 29 ms 24160 KB Output is correct
11 Correct 32 ms 28280 KB Output is correct
12 Correct 32 ms 24576 KB Output is correct
13 Correct 38 ms 28408 KB Output is correct
14 Correct 33 ms 28152 KB Output is correct
15 Correct 35 ms 28488 KB Output is correct
16 Correct 36 ms 24568 KB Output is correct
17 Correct 30 ms 24184 KB Output is correct
18 Correct 33 ms 24448 KB Output is correct
19 Correct 29 ms 24576 KB Output is correct
20 Correct 39 ms 24568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23928 KB Output is correct
2 Correct 32 ms 28024 KB Output is correct
3 Correct 29 ms 28024 KB Output is correct
4 Correct 26 ms 23928 KB Output is correct
5 Correct 26 ms 24184 KB Output is correct
6 Correct 30 ms 24576 KB Output is correct
7 Correct 27 ms 24056 KB Output is correct
8 Correct 28 ms 24064 KB Output is correct
9 Correct 26 ms 24192 KB Output is correct
10 Correct 29 ms 24160 KB Output is correct
11 Correct 32 ms 28280 KB Output is correct
12 Correct 32 ms 24576 KB Output is correct
13 Correct 38 ms 28408 KB Output is correct
14 Correct 33 ms 28152 KB Output is correct
15 Correct 35 ms 28488 KB Output is correct
16 Correct 36 ms 24568 KB Output is correct
17 Correct 30 ms 24184 KB Output is correct
18 Correct 33 ms 24448 KB Output is correct
19 Correct 29 ms 24576 KB Output is correct
20 Correct 39 ms 24568 KB Output is correct
21 Correct 48 ms 26228 KB Output is correct
22 Correct 62 ms 27636 KB Output is correct
23 Correct 81 ms 28656 KB Output is correct
24 Correct 70 ms 31156 KB Output is correct
25 Correct 44 ms 30580 KB Output is correct
26 Correct 105 ms 32756 KB Output is correct
27 Correct 111 ms 29464 KB Output is correct
28 Correct 52 ms 26612 KB Output is correct
29 Correct 50 ms 26356 KB Output is correct
30 Correct 129 ms 31600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23928 KB Output is correct
2 Correct 32 ms 28024 KB Output is correct
3 Correct 29 ms 28024 KB Output is correct
4 Correct 26 ms 23928 KB Output is correct
5 Correct 26 ms 24184 KB Output is correct
6 Correct 30 ms 24576 KB Output is correct
7 Correct 27 ms 24056 KB Output is correct
8 Correct 28 ms 24064 KB Output is correct
9 Correct 26 ms 24192 KB Output is correct
10 Correct 29 ms 24160 KB Output is correct
11 Correct 587 ms 52468 KB Output is correct
12 Correct 1261 ms 68452 KB Output is correct
13 Correct 322 ms 40168 KB Output is correct
14 Correct 1566 ms 80160 KB Output is correct
15 Correct 1606 ms 83992 KB Output is correct
16 Correct 1781 ms 110044 KB Output is correct
17 Correct 319 ms 40168 KB Output is correct
18 Correct 1425 ms 75244 KB Output is correct
19 Correct 1612 ms 78688 KB Output is correct
20 Correct 990 ms 78292 KB Output is correct
21 Correct 32 ms 28280 KB Output is correct
22 Correct 32 ms 24576 KB Output is correct
23 Correct 38 ms 28408 KB Output is correct
24 Correct 33 ms 28152 KB Output is correct
25 Correct 35 ms 28488 KB Output is correct
26 Correct 36 ms 24568 KB Output is correct
27 Correct 30 ms 24184 KB Output is correct
28 Correct 33 ms 24448 KB Output is correct
29 Correct 29 ms 24576 KB Output is correct
30 Correct 39 ms 24568 KB Output is correct
31 Correct 48 ms 26228 KB Output is correct
32 Correct 62 ms 27636 KB Output is correct
33 Correct 81 ms 28656 KB Output is correct
34 Correct 70 ms 31156 KB Output is correct
35 Correct 44 ms 30580 KB Output is correct
36 Correct 105 ms 32756 KB Output is correct
37 Correct 111 ms 29464 KB Output is correct
38 Correct 52 ms 26612 KB Output is correct
39 Correct 50 ms 26356 KB Output is correct
40 Correct 129 ms 31600 KB Output is correct
41 Correct 300 ms 45320 KB Output is correct
42 Correct 698 ms 63276 KB Output is correct
43 Correct 260 ms 53724 KB Output is correct
44 Correct 332 ms 52352 KB Output is correct
45 Correct 483 ms 63200 KB Output is correct
46 Correct 991 ms 81076 KB Output is correct
47 Correct 1507 ms 103312 KB Output is correct
48 Correct 338 ms 48240 KB Output is correct
49 Correct 978 ms 81004 KB Output is correct
50 Correct 1140 ms 80580 KB Output is correct
51 Correct 322 ms 54296 KB Output is correct
52 Correct 341 ms 47336 KB Output is correct
53 Correct 299 ms 47328 KB Output is correct
54 Correct 1290 ms 80052 KB Output is correct
55 Correct 1446 ms 84992 KB Output is correct