#include <bits/stdc++.h>
using namespace std;
class DsuClass {
private:
int n;
int skip;
vector<int> t;
vector<int> deg;
bool ok;
int getComponent(int a) {
assert(0 <= a && a < n);
if (t[a] == a) return a;
return t[a] = getComponent(t[a]);
}
public:
DsuClass() = default;
DsuClass(int n, int skip) :
n(n),
skip(skip),
t(n),
ok(true),
deg(n, 0) {
iota(t.begin(), t.end(), 0);
};
void unite(int a, int b) {
assert(0 <= a && a < n);
assert(0 <= b && b < n);
if (a == skip || b == skip) return;
deg[a]++;
deg[b]++;
ok &= (deg[a] <= 2);
ok &= (deg[b] <= 2);
ok &= (getComponent(a) != getComponent(b));
t[getComponent(a)] = getComponent(b);
}
bool isOk() {
return ok;
}
};
const int N = (int) 1e6 + 7;
int n;
int maxDeg = 0;
int maxDegVertex = 0;
vector<int> g[N];
bool Started = false;
int cycles = 0;
int sizeOfCycle = -1;
int parrent[N];
int root(int a) {
if (parrent[a] == a) return a;
return parrent[a] = root(parrent[a]);
}
void Init(int __n__) {
assert(!Started);
Started = true;
n = __n__;
for (int i = 0; i < n; i++) parrent[i] = i;
}
bool vis[N];
bool act[N];
int cntActive;
void computeSizeOfCycle(int a, int theParrent = -1) {
act[a] = true;
vis[a] = true;
cntActive++;
for (auto &b : g[a]) {
if (act[b] && b != theParrent) {
assert(sizeOfCycle == -1);
sizeOfCycle = cntActive;
}
if (vis[b]) {
continue;
}
computeSizeOfCycle(b, a);
}
cntActive--;
act[a] = false;
}
queue<pair<int, int>> edgesQueue;
void Link(int a, int b) {
assert(0 <= a && a < n);
assert(0 <= b && b < n);
g[a].push_back(b);
g[b].push_back(a);
edgesQueue.push({a, b});
bool onCycle = (root(a) == root(b));
if (onCycle) {
cycles++;
if (cycles == 1) {
/// super duper mega fresh new cycle
sizeOfCycle = -1;
computeSizeOfCycle(a);
assert(sizeOfCycle != -1);
}
}
parrent[root(a)] = root(b);
if ((int) g[a].size() > maxDeg) {
maxDeg = (int) g[a].size();
maxDegVertex = a;
}
if ((int) g[b].size() > maxDeg) {
maxDeg = (int) g[b].size();
maxDegVertex = b;
}
}
vector<DsuClass> potentials;
int CountCritical() {
if (maxDeg <= 2) {
if (cycles == 0) {
return n;
}
if (cycles == 1) {
return sizeOfCycle;
}
if (cycles >= 2) {
return 0; /// independent cycles are bad for your health
}
} else {
/// maxDeg >= 3
if (maxDeg == 3) {
if (potentials.empty()) {
potentials.push_back(DsuClass(n, maxDegVertex));
for (auto &a : g[maxDegVertex]) {
potentials.push_back(DsuClass(n, a));
}
}
} else {
/// maxDeg >= 4
if (potentials.empty()) {
potentials.push_back(DsuClass(n, maxDegVertex));
}
if ((int) potentials.size() >= 2) potentials.resize(1);
}
while (!edgesQueue.empty()) {
int a = edgesQueue.front().first, b = edgesQueue.front().second;
edgesQueue.pop();
for (auto &v : potentials) {
v.unite(a, b);
}
}
int solution = 0;
for (auto &v : potentials) {
solution += v.isOk();
}
return solution;
}
return -1;
}
Compilation message
rings.cpp: In constructor 'DsuClass::DsuClass(int, int)':
rings.cpp:11:8: warning: 'DsuClass::ok' will be initialized after [-Wreorder]
11 | bool ok;
| ^~
rings.cpp:10:15: warning: 'std::vector<int> DsuClass::deg' [-Wreorder]
10 | vector<int> deg;
| ^~~
rings.cpp:24:3: warning: when initialized here [-Wreorder]
24 | DsuClass(int n, int skip) :
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
24048 KB |
Output is correct |
3 |
Correct |
14 ms |
24096 KB |
Output is correct |
4 |
Correct |
13 ms |
23788 KB |
Output is correct |
5 |
Correct |
13 ms |
24068 KB |
Output is correct |
6 |
Correct |
16 ms |
24340 KB |
Output is correct |
7 |
Correct |
13 ms |
24012 KB |
Output is correct |
8 |
Correct |
13 ms |
23944 KB |
Output is correct |
9 |
Correct |
14 ms |
24224 KB |
Output is correct |
10 |
Correct |
18 ms |
24216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
46392 KB |
Output is correct |
2 |
Correct |
757 ms |
64808 KB |
Output is correct |
3 |
Correct |
1004 ms |
84796 KB |
Output is correct |
4 |
Correct |
878 ms |
82356 KB |
Output is correct |
5 |
Correct |
876 ms |
84016 KB |
Output is correct |
6 |
Correct |
981 ms |
118892 KB |
Output is correct |
7 |
Correct |
971 ms |
83784 KB |
Output is correct |
8 |
Correct |
1239 ms |
106256 KB |
Output is correct |
9 |
Correct |
1357 ms |
111932 KB |
Output is correct |
10 |
Correct |
566 ms |
79980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
24048 KB |
Output is correct |
3 |
Correct |
14 ms |
24096 KB |
Output is correct |
4 |
Correct |
13 ms |
23788 KB |
Output is correct |
5 |
Correct |
13 ms |
24068 KB |
Output is correct |
6 |
Correct |
16 ms |
24340 KB |
Output is correct |
7 |
Correct |
13 ms |
24012 KB |
Output is correct |
8 |
Correct |
13 ms |
23944 KB |
Output is correct |
9 |
Correct |
14 ms |
24224 KB |
Output is correct |
10 |
Correct |
18 ms |
24216 KB |
Output is correct |
11 |
Correct |
15 ms |
24140 KB |
Output is correct |
12 |
Correct |
18 ms |
24780 KB |
Output is correct |
13 |
Correct |
19 ms |
24524 KB |
Output is correct |
14 |
Correct |
17 ms |
24140 KB |
Output is correct |
15 |
Correct |
15 ms |
24232 KB |
Output is correct |
16 |
Correct |
16 ms |
24392 KB |
Output is correct |
17 |
Correct |
17 ms |
24524 KB |
Output is correct |
18 |
Correct |
17 ms |
24780 KB |
Output is correct |
19 |
Correct |
16 ms |
24388 KB |
Output is correct |
20 |
Correct |
18 ms |
24504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
24048 KB |
Output is correct |
3 |
Correct |
14 ms |
24096 KB |
Output is correct |
4 |
Correct |
13 ms |
23788 KB |
Output is correct |
5 |
Correct |
13 ms |
24068 KB |
Output is correct |
6 |
Correct |
16 ms |
24340 KB |
Output is correct |
7 |
Correct |
13 ms |
24012 KB |
Output is correct |
8 |
Correct |
13 ms |
23944 KB |
Output is correct |
9 |
Correct |
14 ms |
24224 KB |
Output is correct |
10 |
Correct |
18 ms |
24216 KB |
Output is correct |
11 |
Correct |
15 ms |
24140 KB |
Output is correct |
12 |
Correct |
18 ms |
24780 KB |
Output is correct |
13 |
Correct |
19 ms |
24524 KB |
Output is correct |
14 |
Correct |
17 ms |
24140 KB |
Output is correct |
15 |
Correct |
15 ms |
24232 KB |
Output is correct |
16 |
Correct |
16 ms |
24392 KB |
Output is correct |
17 |
Correct |
17 ms |
24524 KB |
Output is correct |
18 |
Correct |
17 ms |
24780 KB |
Output is correct |
19 |
Correct |
16 ms |
24388 KB |
Output is correct |
20 |
Correct |
18 ms |
24504 KB |
Output is correct |
21 |
Correct |
27 ms |
25572 KB |
Output is correct |
22 |
Correct |
40 ms |
26556 KB |
Output is correct |
23 |
Correct |
45 ms |
27464 KB |
Output is correct |
24 |
Correct |
49 ms |
27416 KB |
Output is correct |
25 |
Correct |
25 ms |
27500 KB |
Output is correct |
26 |
Correct |
47 ms |
28056 KB |
Output is correct |
27 |
Correct |
50 ms |
28688 KB |
Output is correct |
28 |
Correct |
64 ms |
30944 KB |
Output is correct |
29 |
Correct |
47 ms |
28100 KB |
Output is correct |
30 |
Correct |
56 ms |
30332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
24048 KB |
Output is correct |
3 |
Correct |
14 ms |
24096 KB |
Output is correct |
4 |
Correct |
13 ms |
23788 KB |
Output is correct |
5 |
Correct |
13 ms |
24068 KB |
Output is correct |
6 |
Correct |
16 ms |
24340 KB |
Output is correct |
7 |
Correct |
13 ms |
24012 KB |
Output is correct |
8 |
Correct |
13 ms |
23944 KB |
Output is correct |
9 |
Correct |
14 ms |
24224 KB |
Output is correct |
10 |
Correct |
18 ms |
24216 KB |
Output is correct |
11 |
Correct |
305 ms |
46392 KB |
Output is correct |
12 |
Correct |
757 ms |
64808 KB |
Output is correct |
13 |
Correct |
1004 ms |
84796 KB |
Output is correct |
14 |
Correct |
878 ms |
82356 KB |
Output is correct |
15 |
Correct |
876 ms |
84016 KB |
Output is correct |
16 |
Correct |
981 ms |
118892 KB |
Output is correct |
17 |
Correct |
971 ms |
83784 KB |
Output is correct |
18 |
Correct |
1239 ms |
106256 KB |
Output is correct |
19 |
Correct |
1357 ms |
111932 KB |
Output is correct |
20 |
Correct |
566 ms |
79980 KB |
Output is correct |
21 |
Correct |
15 ms |
24140 KB |
Output is correct |
22 |
Correct |
18 ms |
24780 KB |
Output is correct |
23 |
Correct |
19 ms |
24524 KB |
Output is correct |
24 |
Correct |
17 ms |
24140 KB |
Output is correct |
25 |
Correct |
15 ms |
24232 KB |
Output is correct |
26 |
Correct |
16 ms |
24392 KB |
Output is correct |
27 |
Correct |
17 ms |
24524 KB |
Output is correct |
28 |
Correct |
17 ms |
24780 KB |
Output is correct |
29 |
Correct |
16 ms |
24388 KB |
Output is correct |
30 |
Correct |
18 ms |
24504 KB |
Output is correct |
31 |
Correct |
27 ms |
25572 KB |
Output is correct |
32 |
Correct |
40 ms |
26556 KB |
Output is correct |
33 |
Correct |
45 ms |
27464 KB |
Output is correct |
34 |
Correct |
49 ms |
27416 KB |
Output is correct |
35 |
Correct |
25 ms |
27500 KB |
Output is correct |
36 |
Correct |
47 ms |
28056 KB |
Output is correct |
37 |
Correct |
50 ms |
28688 KB |
Output is correct |
38 |
Correct |
64 ms |
30944 KB |
Output is correct |
39 |
Correct |
47 ms |
28100 KB |
Output is correct |
40 |
Correct |
56 ms |
30332 KB |
Output is correct |
41 |
Correct |
177 ms |
39444 KB |
Output is correct |
42 |
Correct |
466 ms |
76248 KB |
Output is correct |
43 |
Correct |
254 ms |
56652 KB |
Output is correct |
44 |
Correct |
633 ms |
76352 KB |
Output is correct |
45 |
Correct |
845 ms |
73416 KB |
Output is correct |
46 |
Correct |
559 ms |
72564 KB |
Output is correct |
47 |
Correct |
818 ms |
97096 KB |
Output is correct |
48 |
Correct |
480 ms |
60724 KB |
Output is correct |
49 |
Correct |
543 ms |
69376 KB |
Output is correct |
50 |
Correct |
575 ms |
69120 KB |
Output is correct |
51 |
Correct |
266 ms |
51916 KB |
Output is correct |
52 |
Correct |
706 ms |
84100 KB |
Output is correct |
53 |
Correct |
469 ms |
61488 KB |
Output is correct |
54 |
Correct |
1163 ms |
89384 KB |
Output is correct |
55 |
Correct |
903 ms |
73968 KB |
Output is correct |