#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;
int N;
struct dsu {
int other[1000010];
int len[1000010];
int cycle_cnt;
int cycle_sum;
bool bad;
int no_of_nodes;
int del_node;
void init (int n) {
no_of_nodes = n;
del_node = -1;
for(int i = 0; i < no_of_nodes; i++) {
other[i] = i;
len[i] = 1;
}
cycle_cnt = 0;
cycle_sum = 0;
bad = false;
}
void add(int x, int y) {
if(del_node == x || del_node == y) return ;
if(bad) return ;
if(other[x] == y) {
other[x] = -1;
other[y] = -1;
cycle_cnt += 1;
cycle_sum += len[x];
} else {
if(other[x] == -1 || other[y] == -1) {
bad = true;
return ;
}
int p = other[y];
int q = other[x];
other[p] = q;
other[q] = p;
int sum = len[x] + len[y];
len[p] = len[q] = sum;
if(x != p && x != q) {
other[x] = -1;
}
if(y != p && y != q) {
other[y] = -1;
}
}
}
int count() {
if(bad) return 0;
if(cycle_cnt <= 1) {
if(cycle_cnt == 1) return cycle_sum;
else return no_of_nodes;
}
return 0;
}
} T[4];
int deg[1000010];
vector <int> l, r;
bool found_cand;
void Init(int N_) {
N = N_;
T[0].init(N);
for(int i = 0; i < N; i++) {
deg[i] = 0;
}
l.clear(); r.clear();
found_cand = false;
}
void Link(int A, int B) {
if(found_cand) {
for(int i = 0; i < 4; i++) {
T[i].add(A, B);
}
return ;
}
++deg[A]; ++deg[B];
l.emplace_back(A);
r.emplace_back(B);
if(deg[A] == 3 || deg[B] == 3) {
vector <int> v;
int can = deg[A] == 3 ? A : B;
v.emplace_back(can);
for(int i = 0; i < (int) l.size(); i++) {
if(l[i] == can || r[i] == can) {
v.emplace_back(l[i] ^ r[i] ^ can);
}
}
for(int i = 0; i < 4; i++) {
T[i].init(N);
T[i].del_node = v[i];
}
found_cand = true;
} else {
T[0].add(A, B);
}
if(found_cand) {
for(int i = 0; i < (int)l.size(); i++) {
Link(l[i], r[i]);
}
l.clear(); r.clear();
}
}
int CountCritical() {
if(found_cand) {
int ans = 0;
for(int i = 0; i < 4; i++) {
if(T[i].cycle_cnt == 0 && !T[i].bad) {
++ans;
}
}
return ans;
}
return T[0].count();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
148 ms |
10756 KB |
Output is correct |
2 |
Correct |
335 ms |
30968 KB |
Output is correct |
3 |
Correct |
179 ms |
35668 KB |
Output is correct |
4 |
Correct |
374 ms |
20136 KB |
Output is correct |
5 |
Correct |
346 ms |
20036 KB |
Output is correct |
6 |
Correct |
336 ms |
19736 KB |
Output is correct |
7 |
Correct |
181 ms |
35668 KB |
Output is correct |
8 |
Correct |
550 ms |
38504 KB |
Output is correct |
9 |
Correct |
658 ms |
42344 KB |
Output is correct |
10 |
Correct |
270 ms |
19636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
3 ms |
852 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
19 |
Correct |
3 ms |
596 KB |
Output is correct |
20 |
Correct |
3 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
3 ms |
852 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
19 |
Correct |
3 ms |
596 KB |
Output is correct |
20 |
Correct |
3 ms |
852 KB |
Output is correct |
21 |
Correct |
11 ms |
1416 KB |
Output is correct |
22 |
Correct |
18 ms |
2128 KB |
Output is correct |
23 |
Correct |
26 ms |
2508 KB |
Output is correct |
24 |
Correct |
21 ms |
3748 KB |
Output is correct |
25 |
Correct |
10 ms |
4052 KB |
Output is correct |
26 |
Correct |
23 ms |
3816 KB |
Output is correct |
27 |
Correct |
28 ms |
2468 KB |
Output is correct |
28 |
Correct |
19 ms |
3596 KB |
Output is correct |
29 |
Correct |
20 ms |
4180 KB |
Output is correct |
30 |
Correct |
26 ms |
2548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
148 ms |
10756 KB |
Output is correct |
12 |
Correct |
335 ms |
30968 KB |
Output is correct |
13 |
Correct |
179 ms |
35668 KB |
Output is correct |
14 |
Correct |
374 ms |
20136 KB |
Output is correct |
15 |
Correct |
346 ms |
20036 KB |
Output is correct |
16 |
Correct |
336 ms |
19736 KB |
Output is correct |
17 |
Correct |
181 ms |
35668 KB |
Output is correct |
18 |
Correct |
550 ms |
38504 KB |
Output is correct |
19 |
Correct |
658 ms |
42344 KB |
Output is correct |
20 |
Correct |
270 ms |
19636 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
3 ms |
852 KB |
Output is correct |
23 |
Correct |
3 ms |
852 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
2 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
724 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
3 ms |
852 KB |
Output is correct |
31 |
Correct |
11 ms |
1416 KB |
Output is correct |
32 |
Correct |
18 ms |
2128 KB |
Output is correct |
33 |
Correct |
26 ms |
2508 KB |
Output is correct |
34 |
Correct |
21 ms |
3748 KB |
Output is correct |
35 |
Correct |
10 ms |
4052 KB |
Output is correct |
36 |
Correct |
23 ms |
3816 KB |
Output is correct |
37 |
Correct |
28 ms |
2468 KB |
Output is correct |
38 |
Correct |
19 ms |
3596 KB |
Output is correct |
39 |
Correct |
20 ms |
4180 KB |
Output is correct |
40 |
Correct |
26 ms |
2548 KB |
Output is correct |
41 |
Correct |
117 ms |
10188 KB |
Output is correct |
42 |
Correct |
317 ms |
34752 KB |
Output is correct |
43 |
Correct |
178 ms |
34636 KB |
Output is correct |
44 |
Correct |
180 ms |
33132 KB |
Output is correct |
45 |
Correct |
232 ms |
36304 KB |
Output is correct |
46 |
Correct |
290 ms |
17756 KB |
Output is correct |
47 |
Correct |
346 ms |
18096 KB |
Output is correct |
48 |
Correct |
189 ms |
37164 KB |
Output is correct |
49 |
Correct |
268 ms |
20412 KB |
Output is correct |
50 |
Correct |
290 ms |
19896 KB |
Output is correct |
51 |
Correct |
162 ms |
29532 KB |
Output is correct |
52 |
Correct |
180 ms |
26788 KB |
Output is correct |
53 |
Correct |
169 ms |
36564 KB |
Output is correct |
54 |
Correct |
493 ms |
32796 KB |
Output is correct |
55 |
Correct |
274 ms |
31684 KB |
Output is correct |