/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int CHAINS = 1;
const int CYCLE = 2;
const int RANDOM = 3;
const int QUIT = 4;
struct DSU {
vector <int> root;
vector <int> dim;
int ignore;
int cycleLength;
int findRoot (int u) {
return (root[u] == u ? u : root[u] = findRoot(root[u]));
}
bool join (int u, int v) {
if (u == ignore || v == ignore) {
return false;
}
u = findRoot(u);
v = findRoot(v);
if (u != v) {
root[u] = v;
dim[v] += dim[u];
return false;
} else {
cycleLength = dim[u];
return true;
}
}
DSU (int N, int _ignore = -1) {
root.resize(N);
dim.resize(N);
iota(root.begin(), root.end(), 0);
fill(dim.begin(), dim.end(), 1);
ignore = _ignore;
cycleLength = 0;
}
DSU () {}
};
int N;
vector <vector <int>> adj;
DSU graph;
vector <pair <int, DSU>> options;
int phase;
int cntBlack;
vector <int> adjBlack;
bool red;
vector <pair <int, int>> links;
void Init (int _N) {
N = _N;
adj.resize(N);
adjBlack.resize(N);
phase = CHAINS;
graph = DSU(N);
}
int CountCritical () {
if (phase == QUIT) {
return 0;
} else if (phase == CHAINS) {
return N;
} else if (phase == CYCLE) {
return graph.cycleLength;
} else if (phase == RANDOM) {
int answer = 0;
for (pair <int, DSU> &opt : options) {
if (red == true && (int) adj[opt.first].size() <= 3) {
continue;
}
if (adjBlack[opt.first] < cntBlack) {
continue;
}
if (opt.second.cycleLength > 0) {
continue;
}
answer++;
}
return answer;
}
}
void Link (int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
if ((int) adj[v].size() >= 3) {
swap(u, v);
}
if (phase < RANDOM && (int) adj[u].size() >= 3) {
phase = RANDOM;
options.push_back(make_pair(u, DSU()));
for (int v : adj[u]) {
options.push_back(make_pair(v, DSU()));
}
for (pair <int, DSU> &opt : options) {
opt.second = DSU(N, opt.first);
for (pair <int, int> l : links) {
opt.second.join(l.first, l.second);
}
}
}
links.push_back(make_pair(u, v));
if (phase == CHAINS) {
if (graph.join(u, v) == true) {
phase = CYCLE;
}
} else if (phase == CYCLE) {
if (graph.join(u, v) == true) {
phase = QUIT;
}
} else {
graph.join(u, v);
for (pair <int, DSU> &opt : options) {
opt.second.join(u, v);
}
if ((int) adj[u].size() == 3) {
cntBlack++;
adjBlack[u]++;
for (int x : adj[u]) {
adjBlack[x]++;
}
}
if ((int) adj[v].size() == 3) {
cntBlack++;
adjBlack[v]++;
for (int x : adj[v]) {
adjBlack[x]++;
}
}
if ((int) adj[u].size() == 4 || (int) adj[v].size() == 4) {
if (red == true) {
phase = QUIT;
} else {
red = true;
}
}
}
}
Compilation message
rings.cpp: In function 'int CountCritical()':
rings.cpp:98:1: warning: control reaches end of non-void function [-Wreturn-type]
98 | }
| ^
rings.cpp: In function 'void Link(int, int)':
rings.cpp:20:8: warning: '*((void*)&<anonymous> +48)' may be used uninitialized in this function [-Wmaybe-uninitialized]
20 | struct DSU {
| ^~~
rings.cpp:20:8: warning: '*((void*)&<anonymous> +48)' may be used uninitialized in this function [-Wmaybe-uninitialized]
20 | struct DSU {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
728 KB |
Output is correct |
3 |
Correct |
3 ms |
852 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 |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
358 ms |
45876 KB |
Output is correct |
2 |
Correct |
1307 ms |
95484 KB |
Output is correct |
3 |
Correct |
2519 ms |
114180 KB |
Output is correct |
4 |
Correct |
1034 ms |
87980 KB |
Output is correct |
5 |
Correct |
961 ms |
88016 KB |
Output is correct |
6 |
Correct |
983 ms |
86136 KB |
Output is correct |
7 |
Correct |
2311 ms |
112772 KB |
Output is correct |
8 |
Correct |
1369 ms |
111688 KB |
Output is correct |
9 |
Correct |
1449 ms |
119312 KB |
Output is correct |
10 |
Correct |
662 ms |
85556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
728 KB |
Output is correct |
3 |
Correct |
3 ms |
852 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 |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
3 ms |
852 KB |
Output is correct |
12 |
Correct |
5 ms |
1420 KB |
Output is correct |
13 |
Correct |
5 ms |
1492 KB |
Output is correct |
14 |
Correct |
4 ms |
1236 KB |
Output is correct |
15 |
Correct |
4 ms |
1876 KB |
Output is correct |
16 |
Correct |
4 ms |
1108 KB |
Output is correct |
17 |
Correct |
5 ms |
1492 KB |
Output is correct |
18 |
Correct |
7 ms |
2292 KB |
Output is correct |
19 |
Correct |
4 ms |
1236 KB |
Output is correct |
20 |
Correct |
5 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
728 KB |
Output is correct |
3 |
Correct |
3 ms |
852 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 |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
3 ms |
852 KB |
Output is correct |
12 |
Correct |
5 ms |
1420 KB |
Output is correct |
13 |
Correct |
5 ms |
1492 KB |
Output is correct |
14 |
Correct |
4 ms |
1236 KB |
Output is correct |
15 |
Correct |
4 ms |
1876 KB |
Output is correct |
16 |
Correct |
4 ms |
1108 KB |
Output is correct |
17 |
Correct |
5 ms |
1492 KB |
Output is correct |
18 |
Correct |
7 ms |
2292 KB |
Output is correct |
19 |
Correct |
4 ms |
1236 KB |
Output is correct |
20 |
Correct |
5 ms |
1492 KB |
Output is correct |
21 |
Correct |
15 ms |
3664 KB |
Output is correct |
22 |
Correct |
24 ms |
5620 KB |
Output is correct |
23 |
Correct |
32 ms |
7076 KB |
Output is correct |
24 |
Correct |
80 ms |
9212 KB |
Output is correct |
25 |
Correct |
19 ms |
7508 KB |
Output is correct |
26 |
Correct |
56 ms |
10192 KB |
Output is correct |
27 |
Correct |
40 ms |
7648 KB |
Output is correct |
28 |
Correct |
57 ms |
10684 KB |
Output is correct |
29 |
Correct |
47 ms |
9312 KB |
Output is correct |
30 |
Correct |
65 ms |
8700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
728 KB |
Output is correct |
3 |
Correct |
3 ms |
852 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 |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
358 ms |
45876 KB |
Output is correct |
12 |
Correct |
1307 ms |
95484 KB |
Output is correct |
13 |
Correct |
2519 ms |
114180 KB |
Output is correct |
14 |
Correct |
1034 ms |
87980 KB |
Output is correct |
15 |
Correct |
961 ms |
88016 KB |
Output is correct |
16 |
Correct |
983 ms |
86136 KB |
Output is correct |
17 |
Correct |
2311 ms |
112772 KB |
Output is correct |
18 |
Correct |
1369 ms |
111688 KB |
Output is correct |
19 |
Correct |
1449 ms |
119312 KB |
Output is correct |
20 |
Correct |
662 ms |
85556 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
5 ms |
1420 KB |
Output is correct |
23 |
Correct |
5 ms |
1492 KB |
Output is correct |
24 |
Correct |
4 ms |
1236 KB |
Output is correct |
25 |
Correct |
4 ms |
1876 KB |
Output is correct |
26 |
Correct |
4 ms |
1108 KB |
Output is correct |
27 |
Correct |
5 ms |
1492 KB |
Output is correct |
28 |
Correct |
7 ms |
2292 KB |
Output is correct |
29 |
Correct |
4 ms |
1236 KB |
Output is correct |
30 |
Correct |
5 ms |
1492 KB |
Output is correct |
31 |
Correct |
15 ms |
3664 KB |
Output is correct |
32 |
Correct |
24 ms |
5620 KB |
Output is correct |
33 |
Correct |
32 ms |
7076 KB |
Output is correct |
34 |
Correct |
80 ms |
9212 KB |
Output is correct |
35 |
Correct |
19 ms |
7508 KB |
Output is correct |
36 |
Correct |
56 ms |
10192 KB |
Output is correct |
37 |
Correct |
40 ms |
7648 KB |
Output is correct |
38 |
Correct |
57 ms |
10684 KB |
Output is correct |
39 |
Correct |
47 ms |
9312 KB |
Output is correct |
40 |
Correct |
65 ms |
8700 KB |
Output is correct |
41 |
Correct |
197 ms |
31536 KB |
Output is correct |
42 |
Correct |
499 ms |
82272 KB |
Output is correct |
43 |
Correct |
320 ms |
69340 KB |
Output is correct |
44 |
Correct |
1006 ms |
108800 KB |
Output is correct |
45 |
Correct |
1568 ms |
102108 KB |
Output is correct |
46 |
Correct |
601 ms |
73644 KB |
Output is correct |
47 |
Correct |
850 ms |
74728 KB |
Output is correct |
48 |
Correct |
691 ms |
92304 KB |
Output is correct |
49 |
Correct |
567 ms |
77092 KB |
Output is correct |
50 |
Correct |
666 ms |
76060 KB |
Output is correct |
51 |
Correct |
360 ms |
62784 KB |
Output is correct |
52 |
Correct |
897 ms |
88148 KB |
Output is correct |
53 |
Correct |
722 ms |
92744 KB |
Output is correct |
54 |
Correct |
1290 ms |
96992 KB |
Output is correct |
55 |
Correct |
1628 ms |
105464 KB |
Output is correct |