#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 1e6 + 5;
int n, mxdeg;
vector<int> adj[SIZE];
struct DS {
int p, mxD;
int cycle, csum;
int to[SIZE], sz[SIZE], sum[SIZE], deg[SIZE], mxdeg[SIZE];
void init() {
mxD = cycle = csum = 0;
iota(to + 1, to + n + 1, 1);
fill(sz + 1, sz + n + 1, 1);
fill(sum + 1, sum + n + 1, 0);
fill(deg + 1, deg + n + 1, 0);
fill(mxdeg + 1, mxdeg + n + 1, 0);
}
void build(int P = 0) {
p = P;
init();
FOR (i, 1, n) for (int j : adj[i]) if (i < j) Merge(i, j);
}
int dsu(int x) {
return x == to[x] ? x : (to[x] = dsu(to[x]));
}
void add(int x) {
if (mxdeg[x] == 2 && sz[x] == sum[x]) cycle++, csum = sz[x];
mxD = max(mxD, min(mxdeg[x], 4));
}
void del(int x) {
if (mxdeg[x] == 2 && sz[x] == sum[x]) cycle--;
}
void Merge(int a, int b) {
if (a == p || b == p) return;
int da = dsu(a), db = dsu(b);
if (da == db) del(da);
else del(da), del(db);
sum[da] -= deg[a] == 2;
sum[db] -= deg[b] == 2;
mxdeg[da] = max(mxdeg[da], ++deg[a]);
mxdeg[db] = max(mxdeg[db], ++deg[b]);
sum[da] += deg[a] == 2;
sum[db] += deg[b] == 2;
if (da == db) {
add(da);
return;
}
if (sz[da] < sz[db]) swap(da, db);
to[db] = da;
sz[da] += sz[db];
sum[da] += sum[db];
mxdeg[da] = max(mxdeg[da], mxdeg[db]);
add(da);
}
} ori, ds[4];
void Init(int N) {
n = N;
ori.build();
}
void Link(int a, int b) {
a++, b++;
adj[a].pb(b);
adj[b].pb(a);
ori.Merge(a, b);
int cur = ori.mxD;
if (cur == mxdeg + 1) {
mxdeg = cur;
if (cur == 4) {
FOR (i, 1, n) if (adj[i].size() == 4) {
ds[0].build(i);
return;
}
}
if (cur == 3) {
FOR (i, 1, n) if (adj[i].size() == 3) {
ds[0].build(i);
int k = 0;
for (int j : adj[i]) ds[++k].build(j);
return;
}
}
}
mxdeg = cur;
if (mxdeg == 4) ds[0].Merge(a, b);
else if (mxdeg == 3) FOR (i, 0, 3) ds[i].Merge(a, b);
}
int CountCritical() {
if (mxdeg <= 1) return n;
if (mxdeg == 2) return ori.cycle == 0 ? n : ori.cycle == 1 ? ori.csum : 0;
int ans = 0;
debug(mxdeg, ori.mxD);
FOR (i, 0, (mxdeg == 3 ? 3 : 0)) ans += ds[i].mxD <= 1 || (ds[i].mxD == 2 && ds[i].cycle == 0), debug(ans, i);
return ans;
}
#ifdef WAIMAI
#define inbuf_len 1 << 16
#define outbuf_len 1 << 16
int main() {
int tmp;
/* Set input and output buffering */
char *inbuf, *outbuf;
inbuf = (char*) malloc(inbuf_len * sizeof(char));
outbuf = (char*) malloc(outbuf_len * sizeof(char));
tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);
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
Compilation message
rings.cpp: In function 'int CountCritical()':
rings.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 7122
| ^~~~
rings.cpp:117:5: note: in expansion of macro 'debug'
117 | debug(mxdeg, ori.mxD);
| ^~~~~
rings.cpp:118:114: warning: right operand of comma operator has no effect [-Wunused-value]
118 | FOR (i, 0, (mxdeg == 3 ? 3 : 0)) ans += ds[i].mxD <= 1 || (ds[i].mxD == 2 && ds[i].cycle == 0), debug(ans, i);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23796 KB |
Output is correct |
2 |
Correct |
17 ms |
24604 KB |
Output is correct |
3 |
Correct |
16 ms |
24640 KB |
Output is correct |
4 |
Correct |
15 ms |
23892 KB |
Output is correct |
5 |
Correct |
15 ms |
24000 KB |
Output is correct |
6 |
Correct |
15 ms |
24148 KB |
Output is correct |
7 |
Correct |
16 ms |
24444 KB |
Output is correct |
8 |
Correct |
15 ms |
24084 KB |
Output is correct |
9 |
Correct |
17 ms |
24660 KB |
Output is correct |
10 |
Correct |
16 ms |
24608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
431 ms |
56632 KB |
Output is correct |
2 |
Correct |
1305 ms |
138000 KB |
Output is correct |
3 |
Correct |
1227 ms |
161596 KB |
Output is correct |
4 |
Correct |
1042 ms |
87968 KB |
Output is correct |
5 |
Correct |
1042 ms |
88044 KB |
Output is correct |
6 |
Correct |
1004 ms |
86648 KB |
Output is correct |
7 |
Correct |
1174 ms |
160524 KB |
Output is correct |
8 |
Correct |
1867 ms |
157260 KB |
Output is correct |
9 |
Correct |
1962 ms |
166212 KB |
Output is correct |
10 |
Correct |
718 ms |
85576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23796 KB |
Output is correct |
2 |
Correct |
17 ms |
24604 KB |
Output is correct |
3 |
Correct |
16 ms |
24640 KB |
Output is correct |
4 |
Correct |
15 ms |
23892 KB |
Output is correct |
5 |
Correct |
15 ms |
24000 KB |
Output is correct |
6 |
Correct |
15 ms |
24148 KB |
Output is correct |
7 |
Correct |
16 ms |
24444 KB |
Output is correct |
8 |
Correct |
15 ms |
24084 KB |
Output is correct |
9 |
Correct |
17 ms |
24660 KB |
Output is correct |
10 |
Correct |
16 ms |
24608 KB |
Output is correct |
11 |
Correct |
16 ms |
24660 KB |
Output is correct |
12 |
Correct |
18 ms |
25300 KB |
Output is correct |
13 |
Correct |
19 ms |
25324 KB |
Output is correct |
14 |
Correct |
16 ms |
25260 KB |
Output is correct |
15 |
Correct |
17 ms |
26100 KB |
Output is correct |
16 |
Correct |
17 ms |
24404 KB |
Output is correct |
17 |
Correct |
18 ms |
25304 KB |
Output is correct |
18 |
Correct |
18 ms |
26452 KB |
Output is correct |
19 |
Correct |
17 ms |
24432 KB |
Output is correct |
20 |
Correct |
20 ms |
25380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23796 KB |
Output is correct |
2 |
Correct |
17 ms |
24604 KB |
Output is correct |
3 |
Correct |
16 ms |
24640 KB |
Output is correct |
4 |
Correct |
15 ms |
23892 KB |
Output is correct |
5 |
Correct |
15 ms |
24000 KB |
Output is correct |
6 |
Correct |
15 ms |
24148 KB |
Output is correct |
7 |
Correct |
16 ms |
24444 KB |
Output is correct |
8 |
Correct |
15 ms |
24084 KB |
Output is correct |
9 |
Correct |
17 ms |
24660 KB |
Output is correct |
10 |
Correct |
16 ms |
24608 KB |
Output is correct |
11 |
Correct |
16 ms |
24660 KB |
Output is correct |
12 |
Correct |
18 ms |
25300 KB |
Output is correct |
13 |
Correct |
19 ms |
25324 KB |
Output is correct |
14 |
Correct |
16 ms |
25260 KB |
Output is correct |
15 |
Correct |
17 ms |
26100 KB |
Output is correct |
16 |
Correct |
17 ms |
24404 KB |
Output is correct |
17 |
Correct |
18 ms |
25304 KB |
Output is correct |
18 |
Correct |
18 ms |
26452 KB |
Output is correct |
19 |
Correct |
17 ms |
24432 KB |
Output is correct |
20 |
Correct |
20 ms |
25380 KB |
Output is correct |
21 |
Correct |
27 ms |
26188 KB |
Output is correct |
22 |
Correct |
38 ms |
27572 KB |
Output is correct |
23 |
Correct |
50 ms |
28520 KB |
Output is correct |
24 |
Correct |
61 ms |
35364 KB |
Output is correct |
25 |
Correct |
29 ms |
34184 KB |
Output is correct |
26 |
Correct |
58 ms |
36232 KB |
Output is correct |
27 |
Correct |
48 ms |
29252 KB |
Output is correct |
28 |
Correct |
102 ms |
36428 KB |
Output is correct |
29 |
Correct |
56 ms |
35944 KB |
Output is correct |
30 |
Correct |
80 ms |
30076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23796 KB |
Output is correct |
2 |
Correct |
17 ms |
24604 KB |
Output is correct |
3 |
Correct |
16 ms |
24640 KB |
Output is correct |
4 |
Correct |
15 ms |
23892 KB |
Output is correct |
5 |
Correct |
15 ms |
24000 KB |
Output is correct |
6 |
Correct |
15 ms |
24148 KB |
Output is correct |
7 |
Correct |
16 ms |
24444 KB |
Output is correct |
8 |
Correct |
15 ms |
24084 KB |
Output is correct |
9 |
Correct |
17 ms |
24660 KB |
Output is correct |
10 |
Correct |
16 ms |
24608 KB |
Output is correct |
11 |
Correct |
431 ms |
56632 KB |
Output is correct |
12 |
Correct |
1305 ms |
138000 KB |
Output is correct |
13 |
Correct |
1227 ms |
161596 KB |
Output is correct |
14 |
Correct |
1042 ms |
87968 KB |
Output is correct |
15 |
Correct |
1042 ms |
88044 KB |
Output is correct |
16 |
Correct |
1004 ms |
86648 KB |
Output is correct |
17 |
Correct |
1174 ms |
160524 KB |
Output is correct |
18 |
Correct |
1867 ms |
157260 KB |
Output is correct |
19 |
Correct |
1962 ms |
166212 KB |
Output is correct |
20 |
Correct |
718 ms |
85576 KB |
Output is correct |
21 |
Correct |
16 ms |
24660 KB |
Output is correct |
22 |
Correct |
18 ms |
25300 KB |
Output is correct |
23 |
Correct |
19 ms |
25324 KB |
Output is correct |
24 |
Correct |
16 ms |
25260 KB |
Output is correct |
25 |
Correct |
17 ms |
26100 KB |
Output is correct |
26 |
Correct |
17 ms |
24404 KB |
Output is correct |
27 |
Correct |
18 ms |
25304 KB |
Output is correct |
28 |
Correct |
18 ms |
26452 KB |
Output is correct |
29 |
Correct |
17 ms |
24432 KB |
Output is correct |
30 |
Correct |
20 ms |
25380 KB |
Output is correct |
31 |
Correct |
27 ms |
26188 KB |
Output is correct |
32 |
Correct |
38 ms |
27572 KB |
Output is correct |
33 |
Correct |
50 ms |
28520 KB |
Output is correct |
34 |
Correct |
61 ms |
35364 KB |
Output is correct |
35 |
Correct |
29 ms |
34184 KB |
Output is correct |
36 |
Correct |
58 ms |
36232 KB |
Output is correct |
37 |
Correct |
48 ms |
29252 KB |
Output is correct |
38 |
Correct |
102 ms |
36428 KB |
Output is correct |
39 |
Correct |
56 ms |
35944 KB |
Output is correct |
40 |
Correct |
80 ms |
30076 KB |
Output is correct |
41 |
Correct |
221 ms |
45760 KB |
Output is correct |
42 |
Correct |
811 ms |
128740 KB |
Output is correct |
43 |
Correct |
291 ms |
121420 KB |
Output is correct |
44 |
Correct |
849 ms |
154604 KB |
Output is correct |
45 |
Correct |
956 ms |
151040 KB |
Output is correct |
46 |
Correct |
688 ms |
78164 KB |
Output is correct |
47 |
Correct |
927 ms |
78984 KB |
Output is correct |
48 |
Correct |
630 ms |
144548 KB |
Output is correct |
49 |
Correct |
658 ms |
79376 KB |
Output is correct |
50 |
Correct |
693 ms |
78828 KB |
Output is correct |
51 |
Correct |
315 ms |
110252 KB |
Output is correct |
52 |
Correct |
1030 ms |
128972 KB |
Output is correct |
53 |
Correct |
593 ms |
144852 KB |
Output is correct |
54 |
Correct |
1641 ms |
139392 KB |
Output is correct |
55 |
Correct |
1168 ms |
149860 KB |
Output is correct |