#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
struct dsu {
vector<int> p;
vector<int> sz;
int n;
dsu(int _n) : n(_n) {
p = vector<int>(n);
iota(p.begin(), p.end(), 0);
sz = vector<int>(n, 1);
}
inline int get(int x) {
if (p[x] == x) {
return x;
} else {
return p[x] = get(p[x]);
}
}
inline bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x == y) {
return false;
}
p[x] = y;
sz[y] += sz[x];
return true;
}
inline bool same(int x, int y) {
return (get(x) == get(y));
}
inline int size(int x) {
return sz[get(x)];
}
inline bool root(int x) {
return (x == get(x));
}
};
int n;
int mx;
vector<vector<int>> g;
vector<int> deg;
dsu uf = 1;
struct info {
int ignore;
int ok;
vector<int> ideg;
dsu iuf = 1;
info(int ignore_) {
ignore = ignore_;
ok = 1;
ideg = vector<int>(n);
iuf = dsu(n);
for (int i = 0; i < n; i++) {
for (int j : g[i]) {
if (i == ignore || j == ignore || i > j) {
continue;
}
if (!iuf.unite(i, j)) {
ok = 0;
}
ideg[i]++;
ideg[j]++;
}
}
if (*max_element(ideg.begin(), ideg.end()) > 2) {
ok = 0;
}
}
void add(int x, int y) {
if (x == ignore || y == ignore || ok == 0) {
return;
}
ideg[x]++;
ideg[y]++;
if (max(ideg[x], ideg[y]) > 2) {
ok = 0;
return;
}
if (!iuf.unite(x, y)) {
ok = 0;
return;
}
}
};
vector<info> cands;
vector<int> loops;
void Init(int n_) {
n = n_;
g = vector<vector<int>>(n);
deg = vector<int>(n);
uf = dsu(n);
}
void Link(int x, int y) {
g[x].emplace_back(y);
g[y].emplace_back(x);
deg[x]++;
deg[y]++;
bool t = uf.unite(x, y);
int new_mx = max({mx, deg[x], deg[y]});
if (new_mx == 3 && mx == 2) {
if (deg[x] == 3) {
cands.emplace_back(info(x));
} else {
cands.emplace_back(info(y));
}
for (int z : g[cands[0].ignore]) {
cands.emplace_back(info(z));
}
} else if (mx <= 2) {
if (!t) {
loops.emplace_back(uf.get(x));
}
} else {
for (int i = 0; i < 4; i++) {
cands[i].add(x, y);
}
}
swap(mx, new_mx);
}
int CountCritical() {
int ans = 0;
if (mx >= 3) {
for (int i = 0; i < 4; i++) {
ans += cands[i].ok;
}
} else {
if (loops.size() == 0) {
ans = n;
} else if (loops.size() == 1) {
ans = uf.size(loops[0]);
}
}
return ans;
}
#ifdef tabr
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
Init(7);
cout << CountCritical() << endl; // 7
Link(1, 2);
cout << CountCritical() << endl; // 7
Link(0, 5);
cout << CountCritical() << endl; // 7
Link(2, 0);
cout << CountCritical() << endl; // 7
Link(3, 2);
cout << CountCritical() << endl; // 4
Link(3, 5);
cout << CountCritical() << endl; // 3
Link(4, 3);
cout << CountCritical() << endl; // 2
return 0;
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
4 ms |
928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
409 ms |
35172 KB |
Output is correct |
2 |
Correct |
1167 ms |
91428 KB |
Output is correct |
3 |
Correct |
1050 ms |
109964 KB |
Output is correct |
4 |
Correct |
1000 ms |
66952 KB |
Output is correct |
5 |
Correct |
975 ms |
67180 KB |
Output is correct |
6 |
Correct |
949 ms |
65856 KB |
Output is correct |
7 |
Correct |
1039 ms |
109532 KB |
Output is correct |
8 |
Correct |
1634 ms |
106624 KB |
Output is correct |
9 |
Correct |
1685 ms |
114092 KB |
Output is correct |
10 |
Correct |
675 ms |
65772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
4 ms |
928 KB |
Output is correct |
11 |
Correct |
3 ms |
852 KB |
Output is correct |
12 |
Correct |
6 ms |
1492 KB |
Output is correct |
13 |
Correct |
6 ms |
1492 KB |
Output is correct |
14 |
Correct |
5 ms |
1384 KB |
Output is correct |
15 |
Correct |
4 ms |
2132 KB |
Output is correct |
16 |
Correct |
5 ms |
1108 KB |
Output is correct |
17 |
Correct |
4 ms |
1492 KB |
Output is correct |
18 |
Correct |
6 ms |
2516 KB |
Output is correct |
19 |
Correct |
4 ms |
1108 KB |
Output is correct |
20 |
Correct |
5 ms |
1540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
4 ms |
928 KB |
Output is correct |
11 |
Correct |
3 ms |
852 KB |
Output is correct |
12 |
Correct |
6 ms |
1492 KB |
Output is correct |
13 |
Correct |
6 ms |
1492 KB |
Output is correct |
14 |
Correct |
5 ms |
1384 KB |
Output is correct |
15 |
Correct |
4 ms |
2132 KB |
Output is correct |
16 |
Correct |
5 ms |
1108 KB |
Output is correct |
17 |
Correct |
4 ms |
1492 KB |
Output is correct |
18 |
Correct |
6 ms |
2516 KB |
Output is correct |
19 |
Correct |
4 ms |
1108 KB |
Output is correct |
20 |
Correct |
5 ms |
1540 KB |
Output is correct |
21 |
Correct |
15 ms |
3368 KB |
Output is correct |
22 |
Correct |
25 ms |
5232 KB |
Output is correct |
23 |
Correct |
40 ms |
6592 KB |
Output is correct |
24 |
Correct |
63 ms |
10220 KB |
Output is correct |
25 |
Correct |
16 ms |
8916 KB |
Output is correct |
26 |
Correct |
40 ms |
11084 KB |
Output is correct |
27 |
Correct |
35 ms |
7040 KB |
Output is correct |
28 |
Correct |
41 ms |
11348 KB |
Output is correct |
29 |
Correct |
33 ms |
10664 KB |
Output is correct |
30 |
Correct |
59 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
4 ms |
928 KB |
Output is correct |
11 |
Correct |
409 ms |
35172 KB |
Output is correct |
12 |
Correct |
1167 ms |
91428 KB |
Output is correct |
13 |
Correct |
1050 ms |
109964 KB |
Output is correct |
14 |
Correct |
1000 ms |
66952 KB |
Output is correct |
15 |
Correct |
975 ms |
67180 KB |
Output is correct |
16 |
Correct |
949 ms |
65856 KB |
Output is correct |
17 |
Correct |
1039 ms |
109532 KB |
Output is correct |
18 |
Correct |
1634 ms |
106624 KB |
Output is correct |
19 |
Correct |
1685 ms |
114092 KB |
Output is correct |
20 |
Correct |
675 ms |
65772 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
6 ms |
1492 KB |
Output is correct |
23 |
Correct |
6 ms |
1492 KB |
Output is correct |
24 |
Correct |
5 ms |
1384 KB |
Output is correct |
25 |
Correct |
4 ms |
2132 KB |
Output is correct |
26 |
Correct |
5 ms |
1108 KB |
Output is correct |
27 |
Correct |
4 ms |
1492 KB |
Output is correct |
28 |
Correct |
6 ms |
2516 KB |
Output is correct |
29 |
Correct |
4 ms |
1108 KB |
Output is correct |
30 |
Correct |
5 ms |
1540 KB |
Output is correct |
31 |
Correct |
15 ms |
3368 KB |
Output is correct |
32 |
Correct |
25 ms |
5232 KB |
Output is correct |
33 |
Correct |
40 ms |
6592 KB |
Output is correct |
34 |
Correct |
63 ms |
10220 KB |
Output is correct |
35 |
Correct |
16 ms |
8916 KB |
Output is correct |
36 |
Correct |
40 ms |
11084 KB |
Output is correct |
37 |
Correct |
35 ms |
7040 KB |
Output is correct |
38 |
Correct |
41 ms |
11348 KB |
Output is correct |
39 |
Correct |
33 ms |
10664 KB |
Output is correct |
40 |
Correct |
59 ms |
8028 KB |
Output is correct |
41 |
Correct |
196 ms |
30028 KB |
Output is correct |
42 |
Correct |
765 ms |
91980 KB |
Output is correct |
43 |
Correct |
270 ms |
83296 KB |
Output is correct |
44 |
Correct |
750 ms |
116296 KB |
Output is correct |
45 |
Correct |
826 ms |
111904 KB |
Output is correct |
46 |
Correct |
635 ms |
67148 KB |
Output is correct |
47 |
Correct |
826 ms |
68448 KB |
Output is correct |
48 |
Correct |
482 ms |
105464 KB |
Output is correct |
49 |
Correct |
594 ms |
71496 KB |
Output is correct |
50 |
Correct |
644 ms |
70580 KB |
Output is correct |
51 |
Correct |
290 ms |
74232 KB |
Output is correct |
52 |
Correct |
559 ms |
93900 KB |
Output is correct |
53 |
Correct |
464 ms |
105604 KB |
Output is correct |
54 |
Correct |
1297 ms |
103320 KB |
Output is correct |
55 |
Correct |
1073 ms |
112444 KB |
Output is correct |