#include<cstdio>
#include<vector>
using namespace std;
const int SZ = 1000010;
int min(int a, int b) { return a < b ? a : b; }
int n;
const bool ifDebug = false;
int cycleNum;
vector<int>l[SZ];
typedef struct Graph {
int st[4], ts[3];
int p[SZ], sz[SZ], ln[SZ], c[SZ];
bool is3E[SZ];
int x;
void init() {
x = n;
st[0] = ts[0] = n;
st[1] = st[2] = st[3] = ts[1] = ts[2] = 0;
for (int i = 0; i < n; i++) {
p[i] = i;
sz[i] = 1;
ln[i] = c[i] = 0;
is3E[i] = false;
}
}
void init(int X) {
if (ifDebug)printf("init(%d)\n", X);
init();
x = X;
PR();
for (int i = 0; i < n; i++) {
for (int e : l[i]) {
if (e < i)Link(e, i);
}
}
}
int f(int a) {
return a == p[a] ? a : p[a] = f(p[a]);
}
void u(int A, int B) {
int a = f(A), b = f(B);
if (a != b) {
p[b] = a;
ts[gT(a)]--;
ts[gT(b)]--;
ln[a] += ln[b];
sz[a] += sz[b];
is3E[a] |= is3E[b];
}
else {
ts[gT(a)]--;
}
}
void Con(int A, int B) {
int a = f(A);
st[min(c[A], 3)]--;
if (x == n)l[A].push_back(B);
c[A]++;
st[min(c[A], 3)]++;
if (c[A] >= 3)is3E[a] = true;
}
int gT(int a) {
if (is3E[a]) {
return 2;
}
else if (sz[a] == ln[a]) {
return 1;
}
else {
return 0;
}
}
void Link(int A, int B) {
if (A == x || B == x)return;
if (ifDebug)printf("%d:%d-%d\n", x, A, B);
u(A, B);
Con(A, B);
Con(B, A);
int a = f(A);
ln[a]++;
ts[gT(a)]++;
}
bool isChain() {
return (ts[1] + ts[2] == 0);
}
void PR() {
if (!ifDebug)return;
printf("-------%d-------\n", x);
for (int a = 0; a < n; a++) {
printf("(%d)", c[a]);
if (a == p[a]) {
printf("[%d(%d,%d,%d)]", a, sz[a], gT(a), ln[a]);
printf("\n");
}
else {
printf("%d->%d\n", a, f(a));
}
}
printf("-----------------\n");
}
}Graph;
Graph MG, GV[4];
int GS;
//type: 0-chain/1-cycle/2-3 exists/3-4 exists
void Init(int N) {
cycleNum = n = N;
MG.init();
}
void Link(int A, int B) {
bool p = (MG.st[3] == 0);
MG.Link(A, B);
for (int i = 0; i < GS; i++) {
GV[i].Link(A, B);
}
if (MG.gT(MG.f(A)) == 1)cycleNum = MG.f(A);
if (MG.gT(MG.f(B)) == 1)cycleNum = MG.f(B);
if (p && MG.st[3]) {
int a = A;
if (l[A].size() < 3)a = B;
GS = l[a].size() + 1;
GV[l[a].size()].init(a);
for (int i = 0; i < l[a].size(); i++) {
GV[i].init(l[a][i]);
}
}
}
int CountCritical() {
if (MG.ts[2]) {
if (ifDebug)printf(">=3 Exists");
int r = 0;
for (int i = 0; i < GS; i++) {
if (GV[i].isChain())r++;
}
return r;
}
else if (MG.ts[1]) {
if (ifDebug)printf("cycle Exists");
if (MG.ts[1] > 1)return 0;
return MG.sz[cycleNum];
}
else {
if (ifDebug)printf("Already Chain");
return n;
}
}
Compilation message
rings.cpp: In function 'void Link(int, int)':
rings.cpp:122:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for (int i = 0; i < l[a].size(); i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
17 ms |
24468 KB |
Output is correct |
3 |
Correct |
19 ms |
24488 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
16 ms |
23884 KB |
Output is correct |
6 |
Correct |
16 ms |
24096 KB |
Output is correct |
7 |
Correct |
17 ms |
24404 KB |
Output is correct |
8 |
Correct |
16 ms |
24036 KB |
Output is correct |
9 |
Correct |
18 ms |
24524 KB |
Output is correct |
10 |
Correct |
19 ms |
24564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
511 ms |
48936 KB |
Output is correct |
2 |
Correct |
2448 ms |
115580 KB |
Output is correct |
3 |
Correct |
3554 ms |
134144 KB |
Output is correct |
4 |
Correct |
1172 ms |
71660 KB |
Output is correct |
5 |
Correct |
1126 ms |
71688 KB |
Output is correct |
6 |
Correct |
1111 ms |
70640 KB |
Output is correct |
7 |
Correct |
3397 ms |
133700 KB |
Output is correct |
8 |
Correct |
2671 ms |
130812 KB |
Output is correct |
9 |
Correct |
2814 ms |
138384 KB |
Output is correct |
10 |
Correct |
841 ms |
70436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
17 ms |
24468 KB |
Output is correct |
3 |
Correct |
19 ms |
24488 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
16 ms |
23884 KB |
Output is correct |
6 |
Correct |
16 ms |
24096 KB |
Output is correct |
7 |
Correct |
17 ms |
24404 KB |
Output is correct |
8 |
Correct |
16 ms |
24036 KB |
Output is correct |
9 |
Correct |
18 ms |
24524 KB |
Output is correct |
10 |
Correct |
19 ms |
24564 KB |
Output is correct |
11 |
Correct |
18 ms |
24552 KB |
Output is correct |
12 |
Correct |
23 ms |
25044 KB |
Output is correct |
13 |
Correct |
22 ms |
25140 KB |
Output is correct |
14 |
Correct |
19 ms |
24940 KB |
Output is correct |
15 |
Correct |
21 ms |
25712 KB |
Output is correct |
16 |
Correct |
23 ms |
24268 KB |
Output is correct |
17 |
Correct |
23 ms |
25008 KB |
Output is correct |
18 |
Correct |
24 ms |
26044 KB |
Output is correct |
19 |
Correct |
19 ms |
24268 KB |
Output is correct |
20 |
Correct |
22 ms |
25124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
17 ms |
24468 KB |
Output is correct |
3 |
Correct |
19 ms |
24488 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
16 ms |
23884 KB |
Output is correct |
6 |
Correct |
16 ms |
24096 KB |
Output is correct |
7 |
Correct |
17 ms |
24404 KB |
Output is correct |
8 |
Correct |
16 ms |
24036 KB |
Output is correct |
9 |
Correct |
18 ms |
24524 KB |
Output is correct |
10 |
Correct |
19 ms |
24564 KB |
Output is correct |
11 |
Correct |
18 ms |
24552 KB |
Output is correct |
12 |
Correct |
23 ms |
25044 KB |
Output is correct |
13 |
Correct |
22 ms |
25140 KB |
Output is correct |
14 |
Correct |
19 ms |
24940 KB |
Output is correct |
15 |
Correct |
21 ms |
25712 KB |
Output is correct |
16 |
Correct |
23 ms |
24268 KB |
Output is correct |
17 |
Correct |
23 ms |
25008 KB |
Output is correct |
18 |
Correct |
24 ms |
26044 KB |
Output is correct |
19 |
Correct |
19 ms |
24268 KB |
Output is correct |
20 |
Correct |
22 ms |
25124 KB |
Output is correct |
21 |
Correct |
32 ms |
25588 KB |
Output is correct |
22 |
Correct |
46 ms |
26700 KB |
Output is correct |
23 |
Correct |
56 ms |
27564 KB |
Output is correct |
24 |
Correct |
139 ms |
33168 KB |
Output is correct |
25 |
Correct |
38 ms |
32624 KB |
Output is correct |
26 |
Correct |
119 ms |
33884 KB |
Output is correct |
27 |
Correct |
83 ms |
27924 KB |
Output is correct |
28 |
Correct |
165 ms |
33992 KB |
Output is correct |
29 |
Correct |
87 ms |
33904 KB |
Output is correct |
30 |
Correct |
70 ms |
28520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
17 ms |
24468 KB |
Output is correct |
3 |
Correct |
19 ms |
24488 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
16 ms |
23884 KB |
Output is correct |
6 |
Correct |
16 ms |
24096 KB |
Output is correct |
7 |
Correct |
17 ms |
24404 KB |
Output is correct |
8 |
Correct |
16 ms |
24036 KB |
Output is correct |
9 |
Correct |
18 ms |
24524 KB |
Output is correct |
10 |
Correct |
19 ms |
24564 KB |
Output is correct |
11 |
Correct |
511 ms |
48936 KB |
Output is correct |
12 |
Correct |
2448 ms |
115580 KB |
Output is correct |
13 |
Correct |
3554 ms |
134144 KB |
Output is correct |
14 |
Correct |
1172 ms |
71660 KB |
Output is correct |
15 |
Correct |
1126 ms |
71688 KB |
Output is correct |
16 |
Correct |
1111 ms |
70640 KB |
Output is correct |
17 |
Correct |
3397 ms |
133700 KB |
Output is correct |
18 |
Correct |
2671 ms |
130812 KB |
Output is correct |
19 |
Correct |
2814 ms |
138384 KB |
Output is correct |
20 |
Correct |
841 ms |
70436 KB |
Output is correct |
21 |
Correct |
18 ms |
24552 KB |
Output is correct |
22 |
Correct |
23 ms |
25044 KB |
Output is correct |
23 |
Correct |
22 ms |
25140 KB |
Output is correct |
24 |
Correct |
19 ms |
24940 KB |
Output is correct |
25 |
Correct |
21 ms |
25712 KB |
Output is correct |
26 |
Correct |
23 ms |
24268 KB |
Output is correct |
27 |
Correct |
23 ms |
25008 KB |
Output is correct |
28 |
Correct |
24 ms |
26044 KB |
Output is correct |
29 |
Correct |
19 ms |
24268 KB |
Output is correct |
30 |
Correct |
22 ms |
25124 KB |
Output is correct |
31 |
Correct |
32 ms |
25588 KB |
Output is correct |
32 |
Correct |
46 ms |
26700 KB |
Output is correct |
33 |
Correct |
56 ms |
27564 KB |
Output is correct |
34 |
Correct |
139 ms |
33168 KB |
Output is correct |
35 |
Correct |
38 ms |
32624 KB |
Output is correct |
36 |
Correct |
119 ms |
33884 KB |
Output is correct |
37 |
Correct |
83 ms |
27924 KB |
Output is correct |
38 |
Correct |
165 ms |
33992 KB |
Output is correct |
39 |
Correct |
87 ms |
33904 KB |
Output is correct |
40 |
Correct |
70 ms |
28520 KB |
Output is correct |
41 |
Correct |
310 ms |
40708 KB |
Output is correct |
42 |
Correct |
1122 ms |
109488 KB |
Output is correct |
43 |
Correct |
426 ms |
104488 KB |
Output is correct |
44 |
Correct |
2119 ms |
128940 KB |
Output is correct |
45 |
Correct |
2301 ms |
127556 KB |
Output is correct |
46 |
Correct |
774 ms |
64232 KB |
Output is correct |
47 |
Correct |
975 ms |
64936 KB |
Output is correct |
48 |
Correct |
1209 ms |
123428 KB |
Output is correct |
49 |
Correct |
820 ms |
66112 KB |
Output is correct |
50 |
Correct |
804 ms |
65556 KB |
Output is correct |
51 |
Correct |
560 ms |
94788 KB |
Output is correct |
52 |
Correct |
1838 ms |
107936 KB |
Output is correct |
53 |
Correct |
1191 ms |
123932 KB |
Output is correct |
54 |
Correct |
2418 ms |
116440 KB |
Output is correct |
55 |
Correct |
2762 ms |
124856 KB |
Output is correct |