#include <bits/stdc++.h>
template <class T>
using Vec = std::vector<T>;
struct UnionFind {
Vec<int> par;
UnionFind() = default;
UnionFind(const int size): par(size, -1) { }
int find(const int u) {
return par[u] < 0 ? u : par[u] = find(par[u]);
}
int size(const int u) {
return -par[find(u)];
}
bool merge(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return false;
if (par[u] > par[v]) {
std::swap(u, v);
}
par[u] += par[v];
par[v] = u;
return true;
}
};
struct StateManager {
UnionFind dsu;
Vec<int> deg;
int bad;
bool ok;
StateManager() = default;
StateManager(const int size, const int bad): dsu(size), deg(size), bad(bad), ok(true) { }
void add_edge(const int u, const int v) {
if (u == bad or v == bad or !ok) return;
deg[u] += 1;
deg[v] += 1;
if (!dsu.merge(u, v) or deg[u] >= 3 or deg[v] >= 3) {
ok = false;
}
}
};
int N;
Vec<Vec<int>> adj;
Vec<std::pair<int, int>> history;
UnionFind dsu;
Vec<bool> processed;
Vec<StateManager> whatif;
Vec<Vec<int>> deglist;
/*
0: no deg >= 3, no cycles
1: no deg >= 3, one cycle
2: some deg == 3
3: one deg >= 4
-1: bad, always CountCritical() == 0
*/
int state;
int cycle_leader = -1;
void Init(int N_) {
N = N_;
adj.resize(N);
dsu = UnionFind(N);
processed = Vec<bool>(N);
deglist.resize(N + 5);
for (int i = 0; i < N; ++i) {
deglist[0].push_back(i);
}
}
void Link(int A, int B) {
if (state == -1) return;
adj[A].push_back(B);
adj[B].push_back(A);
deglist[adj[A].size()].push_back(A);
deglist[adj[B].size()].push_back(B);
if (deglist[4].size() >= 2) {
state = -1;
return;
}
if (!deglist[4].empty()) {
if (state != 3) {
state = 3;
const auto u = deglist[4][0];
whatif.clear();
whatif.emplace_back(N, u);
for (const auto [x, y]: history) {
whatif[0].add_edge(x, y);
}
}
whatif[0].add_edge(A, B);
}
else {
if (deglist[3].size() >= 5) {
state = -1;
return;
}
if (!deglist[3].empty()) {
state = 2;
for (const auto u: deglist[3]) {
if (!processed[u]) {
processed[u] = true;
whatif.emplace_back(N, u);
for (const auto [x, y]: history) {
whatif.back().add_edge(x, y);
}
}
}
for (const auto v: deglist[3]) {
for (const auto u: adj[v]) {
if (!processed[u]) {
processed[u] = true;
whatif.emplace_back(N, u);
for (const auto [x, y]: history) {
whatif.back().add_edge(x, y);
}
}
}
}
assert(whatif.size() <= 16);
for (auto& state: whatif) {
state.add_edge(A, B);
}
}
else {
if (!dsu.merge(A, B)) {
if (cycle_leader != -1) {
state = -1;
return;
}
state = 1;
cycle_leader = dsu.find(A);
}
}
}
history.emplace_back(A, B);
}
int CountCritical() {
if (state == 0) {
return N;
}
if (state == 1) {
return dsu.size(cycle_leader);
}
if (state == 2 or state == 3) {
int ret = 0;
for (const auto& state: whatif) {
if (state.ok) {
ret += 1;
}
}
return ret;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
976 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
3 ms |
844 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
3 ms |
712 KB |
Output is correct |
9 |
Correct |
4 ms |
1100 KB |
Output is correct |
10 |
Correct |
4 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
430 ms |
60084 KB |
Output is correct |
2 |
Correct |
967 ms |
99932 KB |
Output is correct |
3 |
Correct |
329 ms |
150584 KB |
Output is correct |
4 |
Correct |
1084 ms |
115480 KB |
Output is correct |
5 |
Correct |
1063 ms |
115516 KB |
Output is correct |
6 |
Correct |
1041 ms |
113404 KB |
Output is correct |
7 |
Correct |
361 ms |
193568 KB |
Output is correct |
8 |
Correct |
1423 ms |
152312 KB |
Output is correct |
9 |
Correct |
1696 ms |
170284 KB |
Output is correct |
10 |
Correct |
727 ms |
112136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
976 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
3 ms |
844 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
3 ms |
712 KB |
Output is correct |
9 |
Correct |
4 ms |
1100 KB |
Output is correct |
10 |
Correct |
4 ms |
1100 KB |
Output is correct |
11 |
Correct |
5 ms |
1100 KB |
Output is correct |
12 |
Correct |
8 ms |
2380 KB |
Output is correct |
13 |
Correct |
9 ms |
2124 KB |
Output is correct |
14 |
Correct |
4 ms |
1336 KB |
Output is correct |
15 |
Correct |
5 ms |
2172 KB |
Output is correct |
16 |
Correct |
5 ms |
1408 KB |
Output is correct |
17 |
Correct |
4 ms |
2252 KB |
Output is correct |
18 |
Correct |
5 ms |
4108 KB |
Output is correct |
19 |
Correct |
5 ms |
1484 KB |
Output is correct |
20 |
Correct |
9 ms |
2124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
976 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
3 ms |
844 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
3 ms |
712 KB |
Output is correct |
9 |
Correct |
4 ms |
1100 KB |
Output is correct |
10 |
Correct |
4 ms |
1100 KB |
Output is correct |
11 |
Correct |
5 ms |
1100 KB |
Output is correct |
12 |
Correct |
8 ms |
2380 KB |
Output is correct |
13 |
Correct |
9 ms |
2124 KB |
Output is correct |
14 |
Correct |
4 ms |
1336 KB |
Output is correct |
15 |
Correct |
5 ms |
2172 KB |
Output is correct |
16 |
Correct |
5 ms |
1408 KB |
Output is correct |
17 |
Correct |
4 ms |
2252 KB |
Output is correct |
18 |
Correct |
5 ms |
4108 KB |
Output is correct |
19 |
Correct |
5 ms |
1484 KB |
Output is correct |
20 |
Correct |
9 ms |
2124 KB |
Output is correct |
21 |
Correct |
20 ms |
4808 KB |
Output is correct |
22 |
Correct |
34 ms |
7564 KB |
Output is correct |
23 |
Correct |
42 ms |
9404 KB |
Output is correct |
24 |
Correct |
54 ms |
9644 KB |
Output is correct |
25 |
Correct |
19 ms |
9064 KB |
Output is correct |
26 |
Correct |
58 ms |
10272 KB |
Output is correct |
27 |
Correct |
47 ms |
9916 KB |
Output is correct |
28 |
Correct |
39 ms |
17544 KB |
Output is correct |
29 |
Correct |
42 ms |
19404 KB |
Output is correct |
30 |
Correct |
87 ms |
11588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
976 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
3 ms |
844 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
3 ms |
712 KB |
Output is correct |
9 |
Correct |
4 ms |
1100 KB |
Output is correct |
10 |
Correct |
4 ms |
1100 KB |
Output is correct |
11 |
Correct |
430 ms |
60084 KB |
Output is correct |
12 |
Correct |
967 ms |
99932 KB |
Output is correct |
13 |
Correct |
329 ms |
150584 KB |
Output is correct |
14 |
Correct |
1084 ms |
115480 KB |
Output is correct |
15 |
Correct |
1063 ms |
115516 KB |
Output is correct |
16 |
Correct |
1041 ms |
113404 KB |
Output is correct |
17 |
Correct |
361 ms |
193568 KB |
Output is correct |
18 |
Correct |
1423 ms |
152312 KB |
Output is correct |
19 |
Correct |
1696 ms |
170284 KB |
Output is correct |
20 |
Correct |
727 ms |
112136 KB |
Output is correct |
21 |
Correct |
5 ms |
1100 KB |
Output is correct |
22 |
Correct |
8 ms |
2380 KB |
Output is correct |
23 |
Correct |
9 ms |
2124 KB |
Output is correct |
24 |
Correct |
4 ms |
1336 KB |
Output is correct |
25 |
Correct |
5 ms |
2172 KB |
Output is correct |
26 |
Correct |
5 ms |
1408 KB |
Output is correct |
27 |
Correct |
4 ms |
2252 KB |
Output is correct |
28 |
Correct |
5 ms |
4108 KB |
Output is correct |
29 |
Correct |
5 ms |
1484 KB |
Output is correct |
30 |
Correct |
9 ms |
2124 KB |
Output is correct |
31 |
Correct |
20 ms |
4808 KB |
Output is correct |
32 |
Correct |
34 ms |
7564 KB |
Output is correct |
33 |
Correct |
42 ms |
9404 KB |
Output is correct |
34 |
Correct |
54 ms |
9644 KB |
Output is correct |
35 |
Correct |
19 ms |
9064 KB |
Output is correct |
36 |
Correct |
58 ms |
10272 KB |
Output is correct |
37 |
Correct |
47 ms |
9916 KB |
Output is correct |
38 |
Correct |
39 ms |
17544 KB |
Output is correct |
39 |
Correct |
42 ms |
19404 KB |
Output is correct |
40 |
Correct |
87 ms |
11588 KB |
Output is correct |
41 |
Correct |
233 ms |
43236 KB |
Output is correct |
42 |
Correct |
626 ms |
102560 KB |
Output is correct |
43 |
Correct |
293 ms |
80176 KB |
Output is correct |
44 |
Correct |
329 ms |
167316 KB |
Output is correct |
45 |
Correct |
486 ms |
127256 KB |
Output is correct |
46 |
Correct |
697 ms |
95076 KB |
Output is correct |
47 |
Correct |
954 ms |
98572 KB |
Output is correct |
48 |
Correct |
350 ms |
189440 KB |
Output is correct |
49 |
Correct |
766 ms |
102152 KB |
Output is correct |
50 |
Correct |
741 ms |
101032 KB |
Output is correct |
51 |
Correct |
321 ms |
68508 KB |
Output is correct |
52 |
Correct |
303 ms |
143752 KB |
Output is correct |
53 |
Correct |
313 ms |
188840 KB |
Output is correct |
54 |
Correct |
1199 ms |
138980 KB |
Output is correct |
55 |
Correct |
1032 ms |
109716 KB |
Output is correct |