#include <bits/stdc++.h>
using namespace std;
//#define TEST
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
const int len = 1e6+5;
int n, state, cycle, last;
int par[5][len], deg[5][len], sz[5][len], block[5][len], fine[5];
vector<int> adj[len];
vector<ii> edge;
int fin(int t, int i){
if (par[t][i] == i) return i;
return par[t][i] = fin(t, par[t][i]);
}
void join(int t, int i, int j){
i = fin(t, i), j = fin(t, j);
if (i == j) return;
if (sz[t][i] > sz[t][j])
sz[t][i] += sz[t][j], par[t][j] = i;
else
sz[t][j] += sz[t][i], par[t][i] = j;
}
void build(int t, int u){
block[t][u] = 1;
fine[t] = 1;
for (int i = 0; i < edge.size(); i++){
int a = edge[i].fi, b = edge[i].se;
if (!fine[t] || block[t][a] || block[t][b]) continue;
if (deg[t][a] > 1 || deg[t][b] > 1 || fin(t, a) == fin(t, b))
fine[t] = 0;
deg[t][a]++, deg[t][b]++;
join(t, a, b);
}
}
void Init(int N){
n = N, state = 0, cycle = 0, last = 0;
for (int t = 0; t < 5; t++)
for (int i = 0; i < n; i++)
par[t][i] = i, deg[t][i] = 0, sz[t][i] = 1;
}
void Link(int a, int b){
edge.pb(mp(a, b));
if (state == 0){
adj[a].pb(b), adj[b].pb(a);
deg[0][a]++, deg[0][b]++;
if (deg[0][a] < deg[0][b])
swap(a, b);
if (deg[0][a] == 3){
state = 1;
build(1, a);
build(2, adj[a][0]);
build(3, adj[a][1]);
build(4, adj[a][2]);
}
else{
a = fin(0, a), b = fin(0, b);
if (a == b)
cycle++, last = sz[0][a];
else
join(0, a, b);
}
}
else{
for (int t = 1; t <= 4; t++){
if (!fine[t] || block[t][a] || block[t][b]) continue;
if (deg[t][a] > 1 || deg[t][b] > 1 || fin(t, a) == fin(t, b))
fine[t] = 0;
deg[t][a]++, deg[t][b]++;
join(t, a, b);
}
}
}
int CountCritical(){
int ans = 0;
if (state == 0){
if (cycle == 0) ans = n;
else if (cycle == 1) ans = last;
else ans = 0;
}
else{
for (int j = 1; j <= 4; j++)
ans += fine[j];
}
return ans;
}
#ifdef TEST
int main() {
freopen("example.txt", "r", stdin);
int tmp;
/* Set input and output buffering */
char *inbuf, *outbuf;
inbuf = (char*) malloc((1 << 16) * sizeof(char));
outbuf = (char*) malloc((1 << 16) * sizeof(char));
tmp = setvbuf(stdin, inbuf, _IOFBF, (1 << 16));
tmp = setvbuf(stdout, outbuf, _IOFBF, (1 << 16));
int N, L;
tmp = scanf("%d %d", &N, &L);
assert(tmp == 2);
Init(N);
int i;
for (i = 0; i < L; i++) {
int A, B;
tmp = scanf("%d", &A);
if (A == -1) {
int critical;
critical = CountCritical();
printf("%d\n", critical);
} else {
tmp = scanf("%d", &B);
assert(tmp == 1);
Link(A, B);
}
}
return 0;
}
#endif // TEST
Compilation message
rings.cpp: In function 'void build(int, int)':
rings.cpp:38:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < edge.size(); i++){
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24440 KB |
Output is correct |
3 |
Correct |
25 ms |
24512 KB |
Output is correct |
4 |
Correct |
22 ms |
24512 KB |
Output is correct |
5 |
Correct |
25 ms |
24512 KB |
Output is correct |
6 |
Correct |
25 ms |
24768 KB |
Output is correct |
7 |
Correct |
23 ms |
24768 KB |
Output is correct |
8 |
Correct |
25 ms |
24768 KB |
Output is correct |
9 |
Correct |
25 ms |
24768 KB |
Output is correct |
10 |
Correct |
25 ms |
24768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
581 ms |
75108 KB |
Output is correct |
2 |
Correct |
998 ms |
93536 KB |
Output is correct |
3 |
Correct |
401 ms |
93536 KB |
Output is correct |
4 |
Correct |
1628 ms |
121856 KB |
Output is correct |
5 |
Correct |
1433 ms |
122112 KB |
Output is correct |
6 |
Correct |
1406 ms |
122112 KB |
Output is correct |
7 |
Correct |
386 ms |
122112 KB |
Output is correct |
8 |
Correct |
1593 ms |
122112 KB |
Output is correct |
9 |
Correct |
1919 ms |
122112 KB |
Output is correct |
10 |
Correct |
976 ms |
122112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24440 KB |
Output is correct |
3 |
Correct |
25 ms |
24512 KB |
Output is correct |
4 |
Correct |
22 ms |
24512 KB |
Output is correct |
5 |
Correct |
25 ms |
24512 KB |
Output is correct |
6 |
Correct |
25 ms |
24768 KB |
Output is correct |
7 |
Correct |
23 ms |
24768 KB |
Output is correct |
8 |
Correct |
25 ms |
24768 KB |
Output is correct |
9 |
Correct |
25 ms |
24768 KB |
Output is correct |
10 |
Correct |
25 ms |
24768 KB |
Output is correct |
11 |
Correct |
23 ms |
122112 KB |
Output is correct |
12 |
Correct |
30 ms |
122112 KB |
Output is correct |
13 |
Correct |
28 ms |
122112 KB |
Output is correct |
14 |
Correct |
26 ms |
122112 KB |
Output is correct |
15 |
Correct |
25 ms |
122112 KB |
Output is correct |
16 |
Correct |
29 ms |
122112 KB |
Output is correct |
17 |
Correct |
27 ms |
122112 KB |
Output is correct |
18 |
Correct |
28 ms |
122112 KB |
Output is correct |
19 |
Correct |
29 ms |
122112 KB |
Output is correct |
20 |
Correct |
29 ms |
122112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24440 KB |
Output is correct |
3 |
Correct |
25 ms |
24512 KB |
Output is correct |
4 |
Correct |
22 ms |
24512 KB |
Output is correct |
5 |
Correct |
25 ms |
24512 KB |
Output is correct |
6 |
Correct |
25 ms |
24768 KB |
Output is correct |
7 |
Correct |
23 ms |
24768 KB |
Output is correct |
8 |
Correct |
25 ms |
24768 KB |
Output is correct |
9 |
Correct |
25 ms |
24768 KB |
Output is correct |
10 |
Correct |
25 ms |
24768 KB |
Output is correct |
11 |
Correct |
23 ms |
122112 KB |
Output is correct |
12 |
Correct |
30 ms |
122112 KB |
Output is correct |
13 |
Correct |
28 ms |
122112 KB |
Output is correct |
14 |
Correct |
26 ms |
122112 KB |
Output is correct |
15 |
Correct |
25 ms |
122112 KB |
Output is correct |
16 |
Correct |
29 ms |
122112 KB |
Output is correct |
17 |
Correct |
27 ms |
122112 KB |
Output is correct |
18 |
Correct |
28 ms |
122112 KB |
Output is correct |
19 |
Correct |
29 ms |
122112 KB |
Output is correct |
20 |
Correct |
29 ms |
122112 KB |
Output is correct |
21 |
Correct |
49 ms |
122112 KB |
Output is correct |
22 |
Correct |
63 ms |
122112 KB |
Output is correct |
23 |
Correct |
75 ms |
122112 KB |
Output is correct |
24 |
Correct |
56 ms |
122112 KB |
Output is correct |
25 |
Correct |
40 ms |
122112 KB |
Output is correct |
26 |
Correct |
59 ms |
122112 KB |
Output is correct |
27 |
Correct |
85 ms |
122112 KB |
Output is correct |
28 |
Correct |
57 ms |
122112 KB |
Output is correct |
29 |
Correct |
52 ms |
122112 KB |
Output is correct |
30 |
Correct |
95 ms |
122112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24440 KB |
Output is correct |
3 |
Correct |
25 ms |
24512 KB |
Output is correct |
4 |
Correct |
22 ms |
24512 KB |
Output is correct |
5 |
Correct |
25 ms |
24512 KB |
Output is correct |
6 |
Correct |
25 ms |
24768 KB |
Output is correct |
7 |
Correct |
23 ms |
24768 KB |
Output is correct |
8 |
Correct |
25 ms |
24768 KB |
Output is correct |
9 |
Correct |
25 ms |
24768 KB |
Output is correct |
10 |
Correct |
25 ms |
24768 KB |
Output is correct |
11 |
Correct |
581 ms |
75108 KB |
Output is correct |
12 |
Correct |
998 ms |
93536 KB |
Output is correct |
13 |
Correct |
401 ms |
93536 KB |
Output is correct |
14 |
Correct |
1628 ms |
121856 KB |
Output is correct |
15 |
Correct |
1433 ms |
122112 KB |
Output is correct |
16 |
Correct |
1406 ms |
122112 KB |
Output is correct |
17 |
Correct |
386 ms |
122112 KB |
Output is correct |
18 |
Correct |
1593 ms |
122112 KB |
Output is correct |
19 |
Correct |
1919 ms |
122112 KB |
Output is correct |
20 |
Correct |
976 ms |
122112 KB |
Output is correct |
21 |
Correct |
23 ms |
122112 KB |
Output is correct |
22 |
Correct |
30 ms |
122112 KB |
Output is correct |
23 |
Correct |
28 ms |
122112 KB |
Output is correct |
24 |
Correct |
26 ms |
122112 KB |
Output is correct |
25 |
Correct |
25 ms |
122112 KB |
Output is correct |
26 |
Correct |
29 ms |
122112 KB |
Output is correct |
27 |
Correct |
27 ms |
122112 KB |
Output is correct |
28 |
Correct |
28 ms |
122112 KB |
Output is correct |
29 |
Correct |
29 ms |
122112 KB |
Output is correct |
30 |
Correct |
29 ms |
122112 KB |
Output is correct |
31 |
Correct |
49 ms |
122112 KB |
Output is correct |
32 |
Correct |
63 ms |
122112 KB |
Output is correct |
33 |
Correct |
75 ms |
122112 KB |
Output is correct |
34 |
Correct |
56 ms |
122112 KB |
Output is correct |
35 |
Correct |
40 ms |
122112 KB |
Output is correct |
36 |
Correct |
59 ms |
122112 KB |
Output is correct |
37 |
Correct |
85 ms |
122112 KB |
Output is correct |
38 |
Correct |
57 ms |
122112 KB |
Output is correct |
39 |
Correct |
52 ms |
122112 KB |
Output is correct |
40 |
Correct |
95 ms |
122112 KB |
Output is correct |
41 |
Correct |
299 ms |
122112 KB |
Output is correct |
42 |
Correct |
867 ms |
122112 KB |
Output is correct |
43 |
Correct |
281 ms |
122112 KB |
Output is correct |
44 |
Correct |
371 ms |
122112 KB |
Output is correct |
45 |
Correct |
589 ms |
122112 KB |
Output is correct |
46 |
Correct |
916 ms |
122112 KB |
Output is correct |
47 |
Correct |
1228 ms |
122112 KB |
Output is correct |
48 |
Correct |
347 ms |
122112 KB |
Output is correct |
49 |
Correct |
897 ms |
122112 KB |
Output is correct |
50 |
Correct |
997 ms |
122112 KB |
Output is correct |
51 |
Correct |
286 ms |
122112 KB |
Output is correct |
52 |
Correct |
358 ms |
122112 KB |
Output is correct |
53 |
Correct |
317 ms |
122112 KB |
Output is correct |
54 |
Correct |
1280 ms |
122112 KB |
Output is correct |
55 |
Correct |
636 ms |
122112 KB |
Output is correct |