#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;
par[t][i] = j;
sz[t][j] += sz[t][i];
}
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 (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 (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:36:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < edge.size(); i++){
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24572 KB |
Output is correct |
3 |
Correct |
24 ms |
24572 KB |
Output is correct |
4 |
Correct |
22 ms |
24572 KB |
Output is correct |
5 |
Correct |
23 ms |
24572 KB |
Output is correct |
6 |
Correct |
26 ms |
24572 KB |
Output is correct |
7 |
Correct |
22 ms |
24572 KB |
Output is correct |
8 |
Correct |
23 ms |
24572 KB |
Output is correct |
9 |
Correct |
24 ms |
24660 KB |
Output is correct |
10 |
Correct |
24 ms |
24672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
551 ms |
75308 KB |
Output is correct |
2 |
Correct |
1833 ms |
93272 KB |
Output is correct |
3 |
Correct |
2809 ms |
93272 KB |
Output is correct |
4 |
Correct |
1623 ms |
121916 KB |
Output is correct |
5 |
Correct |
1515 ms |
122148 KB |
Output is correct |
6 |
Correct |
1465 ms |
122148 KB |
Output is correct |
7 |
Correct |
2480 ms |
122148 KB |
Output is correct |
8 |
Correct |
2098 ms |
122148 KB |
Output is correct |
9 |
Correct |
2304 ms |
122148 KB |
Output is correct |
10 |
Correct |
1085 ms |
122148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24572 KB |
Output is correct |
3 |
Correct |
24 ms |
24572 KB |
Output is correct |
4 |
Correct |
22 ms |
24572 KB |
Output is correct |
5 |
Correct |
23 ms |
24572 KB |
Output is correct |
6 |
Correct |
26 ms |
24572 KB |
Output is correct |
7 |
Correct |
22 ms |
24572 KB |
Output is correct |
8 |
Correct |
23 ms |
24572 KB |
Output is correct |
9 |
Correct |
24 ms |
24660 KB |
Output is correct |
10 |
Correct |
24 ms |
24672 KB |
Output is correct |
11 |
Correct |
25 ms |
122148 KB |
Output is correct |
12 |
Correct |
30 ms |
122148 KB |
Output is correct |
13 |
Correct |
29 ms |
122148 KB |
Output is correct |
14 |
Correct |
26 ms |
122148 KB |
Output is correct |
15 |
Correct |
28 ms |
122148 KB |
Output is correct |
16 |
Correct |
28 ms |
122148 KB |
Output is correct |
17 |
Correct |
28 ms |
122148 KB |
Output is correct |
18 |
Correct |
28 ms |
122148 KB |
Output is correct |
19 |
Correct |
28 ms |
122148 KB |
Output is correct |
20 |
Correct |
30 ms |
122148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24572 KB |
Output is correct |
3 |
Correct |
24 ms |
24572 KB |
Output is correct |
4 |
Correct |
22 ms |
24572 KB |
Output is correct |
5 |
Correct |
23 ms |
24572 KB |
Output is correct |
6 |
Correct |
26 ms |
24572 KB |
Output is correct |
7 |
Correct |
22 ms |
24572 KB |
Output is correct |
8 |
Correct |
23 ms |
24572 KB |
Output is correct |
9 |
Correct |
24 ms |
24660 KB |
Output is correct |
10 |
Correct |
24 ms |
24672 KB |
Output is correct |
11 |
Correct |
25 ms |
122148 KB |
Output is correct |
12 |
Correct |
30 ms |
122148 KB |
Output is correct |
13 |
Correct |
29 ms |
122148 KB |
Output is correct |
14 |
Correct |
26 ms |
122148 KB |
Output is correct |
15 |
Correct |
28 ms |
122148 KB |
Output is correct |
16 |
Correct |
28 ms |
122148 KB |
Output is correct |
17 |
Correct |
28 ms |
122148 KB |
Output is correct |
18 |
Correct |
28 ms |
122148 KB |
Output is correct |
19 |
Correct |
28 ms |
122148 KB |
Output is correct |
20 |
Correct |
30 ms |
122148 KB |
Output is correct |
21 |
Correct |
49 ms |
122148 KB |
Output is correct |
22 |
Correct |
64 ms |
122148 KB |
Output is correct |
23 |
Correct |
73 ms |
122148 KB |
Output is correct |
24 |
Correct |
78 ms |
122148 KB |
Output is correct |
25 |
Correct |
51 ms |
122148 KB |
Output is correct |
26 |
Correct |
74 ms |
122148 KB |
Output is correct |
27 |
Correct |
91 ms |
122148 KB |
Output is correct |
28 |
Correct |
89 ms |
122148 KB |
Output is correct |
29 |
Correct |
64 ms |
122148 KB |
Output is correct |
30 |
Correct |
112 ms |
122148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24572 KB |
Output is correct |
3 |
Correct |
24 ms |
24572 KB |
Output is correct |
4 |
Correct |
22 ms |
24572 KB |
Output is correct |
5 |
Correct |
23 ms |
24572 KB |
Output is correct |
6 |
Correct |
26 ms |
24572 KB |
Output is correct |
7 |
Correct |
22 ms |
24572 KB |
Output is correct |
8 |
Correct |
23 ms |
24572 KB |
Output is correct |
9 |
Correct |
24 ms |
24660 KB |
Output is correct |
10 |
Correct |
24 ms |
24672 KB |
Output is correct |
11 |
Correct |
551 ms |
75308 KB |
Output is correct |
12 |
Correct |
1833 ms |
93272 KB |
Output is correct |
13 |
Correct |
2809 ms |
93272 KB |
Output is correct |
14 |
Correct |
1623 ms |
121916 KB |
Output is correct |
15 |
Correct |
1515 ms |
122148 KB |
Output is correct |
16 |
Correct |
1465 ms |
122148 KB |
Output is correct |
17 |
Correct |
2480 ms |
122148 KB |
Output is correct |
18 |
Correct |
2098 ms |
122148 KB |
Output is correct |
19 |
Correct |
2304 ms |
122148 KB |
Output is correct |
20 |
Correct |
1085 ms |
122148 KB |
Output is correct |
21 |
Correct |
25 ms |
122148 KB |
Output is correct |
22 |
Correct |
30 ms |
122148 KB |
Output is correct |
23 |
Correct |
29 ms |
122148 KB |
Output is correct |
24 |
Correct |
26 ms |
122148 KB |
Output is correct |
25 |
Correct |
28 ms |
122148 KB |
Output is correct |
26 |
Correct |
28 ms |
122148 KB |
Output is correct |
27 |
Correct |
28 ms |
122148 KB |
Output is correct |
28 |
Correct |
28 ms |
122148 KB |
Output is correct |
29 |
Correct |
28 ms |
122148 KB |
Output is correct |
30 |
Correct |
30 ms |
122148 KB |
Output is correct |
31 |
Correct |
49 ms |
122148 KB |
Output is correct |
32 |
Correct |
64 ms |
122148 KB |
Output is correct |
33 |
Correct |
73 ms |
122148 KB |
Output is correct |
34 |
Correct |
78 ms |
122148 KB |
Output is correct |
35 |
Correct |
51 ms |
122148 KB |
Output is correct |
36 |
Correct |
74 ms |
122148 KB |
Output is correct |
37 |
Correct |
91 ms |
122148 KB |
Output is correct |
38 |
Correct |
89 ms |
122148 KB |
Output is correct |
39 |
Correct |
64 ms |
122148 KB |
Output is correct |
40 |
Correct |
112 ms |
122148 KB |
Output is correct |
41 |
Correct |
314 ms |
122148 KB |
Output is correct |
42 |
Correct |
898 ms |
122148 KB |
Output is correct |
43 |
Correct |
404 ms |
122148 KB |
Output is correct |
44 |
Correct |
1552 ms |
122148 KB |
Output is correct |
45 |
Correct |
1703 ms |
122148 KB |
Output is correct |
46 |
Correct |
983 ms |
122148 KB |
Output is correct |
47 |
Correct |
1422 ms |
122148 KB |
Output is correct |
48 |
Correct |
1153 ms |
122148 KB |
Output is correct |
49 |
Correct |
1031 ms |
122148 KB |
Output is correct |
50 |
Correct |
1066 ms |
122148 KB |
Output is correct |
51 |
Correct |
513 ms |
122148 KB |
Output is correct |
52 |
Correct |
1281 ms |
122148 KB |
Output is correct |
53 |
Correct |
1025 ms |
122148 KB |
Output is correct |
54 |
Correct |
1814 ms |
122148 KB |
Output is correct |
55 |
Correct |
2191 ms |
122148 KB |
Output is correct |