#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
const int MAX_N = 1e6 + 5;
struct Dsu {
vi par, sz, deg;
vii max_deg;
set<int> bad_comps;
int erased_node;
bool good = true;
Dsu() {}
Dsu(int n, int node): erased_node(node) {
par.resize(n), sz.resize(n), deg.resize(n);
max_deg.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i, sz[i] = 1;
max_deg[i] = {0, i};
}
}
int Find(int i) {
return par[i] == i ? i : par[i] = Find(par[i]);
}
void Union(int a, int b) {
int pa = Find(a), pb = Find(b);
if (pa == pb) {
bad_comps.insert(pa);
good = false;
return;
}
if (sz[pa] < sz[pb]) swap(pa, pb);
if (bad_comps.count(pb)) {
bad_comps.erase(bad_comps.find(pb));
bad_comps.insert(pa);
good = false;
}
sz[pa] += sz[pb];
par[pb] = pa;
chkmax(max_deg[pa], max_deg[pb]);
}
void Add(int a, int b) {
if (a == erased_node || b == erased_node) return;
Union(a, b);
int pa = Find(a);
deg[a]++, deg[b]++;
if (deg[a] > 2 || deg[b] > 2) {
bad_comps.insert(pa);
good = false;
}
chkmax(max_deg[pa], pii(deg[a], a));
chkmax(max_deg[pa], pii(deg[b], b));
}
};
int n;
Dsu d1;
bool changed = false;
vi graph[MAX_N];
void Init(int N_) {
n = N_;
d1 = Dsu(n, -1);
}
vector<Dsu> dsu;
vii edges;
void Link(int a, int b) {
graph[a].push_back(b);
graph[b].push_back(a);
edges.push_back({a, b});
if (!changed) {
d1.Add(a, b);
} else {
for (auto &d : dsu) {
d.Add(a, b);
}
}
}
int CountCritical() {
if (changed) {
int cnt = 0;
for (auto &d : dsu) cnt += d.good;
return cnt;
}
if (d1.good) return n;
if (d1.bad_comps.size() == 1) {
int node = *d1.bad_comps.begin();
auto [max_deg, v] = d1.max_deg[node];
if (max_deg == 2) return d1.sz[node];
changed = true;
int cnt = 0;
dsu.push_back(Dsu(n, v));
if (max_deg <= 3) {
for (int neighbor : graph[v]) {
dsu.push_back(Dsu(n, neighbor));
}
}
for (auto &d : dsu) {
for (pii p : edges) {
d.Add(p.x, p.y);
}
}
return CountCritical();
}
changed = true;
return 0;
}
Compilation message
rings.cpp: In function 'int CountCritical()':
rings.cpp:105:13: warning: unused variable 'cnt' [-Wunused-variable]
105 | int cnt = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
18 ms |
24136 KB |
Output is correct |
3 |
Correct |
15 ms |
24148 KB |
Output is correct |
4 |
Correct |
14 ms |
23796 KB |
Output is correct |
5 |
Correct |
14 ms |
23892 KB |
Output is correct |
6 |
Correct |
15 ms |
24084 KB |
Output is correct |
7 |
Correct |
13 ms |
23928 KB |
Output is correct |
8 |
Correct |
15 ms |
24020 KB |
Output is correct |
9 |
Correct |
20 ms |
24404 KB |
Output is correct |
10 |
Correct |
14 ms |
24476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
449 ms |
54460 KB |
Output is correct |
2 |
Correct |
1042 ms |
86484 KB |
Output is correct |
3 |
Correct |
1240 ms |
90900 KB |
Output is correct |
4 |
Correct |
1102 ms |
95792 KB |
Output is correct |
5 |
Correct |
1141 ms |
95860 KB |
Output is correct |
6 |
Correct |
1066 ms |
94136 KB |
Output is correct |
7 |
Correct |
1196 ms |
89420 KB |
Output is correct |
8 |
Correct |
1819 ms |
164464 KB |
Output is correct |
9 |
Correct |
1997 ms |
174128 KB |
Output is correct |
10 |
Correct |
793 ms |
92860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
18 ms |
24136 KB |
Output is correct |
3 |
Correct |
15 ms |
24148 KB |
Output is correct |
4 |
Correct |
14 ms |
23796 KB |
Output is correct |
5 |
Correct |
14 ms |
23892 KB |
Output is correct |
6 |
Correct |
15 ms |
24084 KB |
Output is correct |
7 |
Correct |
13 ms |
23928 KB |
Output is correct |
8 |
Correct |
15 ms |
24020 KB |
Output is correct |
9 |
Correct |
20 ms |
24404 KB |
Output is correct |
10 |
Correct |
14 ms |
24476 KB |
Output is correct |
11 |
Correct |
15 ms |
24532 KB |
Output is correct |
12 |
Correct |
21 ms |
25200 KB |
Output is correct |
13 |
Correct |
20 ms |
25188 KB |
Output is correct |
14 |
Correct |
20 ms |
24464 KB |
Output is correct |
15 |
Correct |
21 ms |
24796 KB |
Output is correct |
16 |
Correct |
20 ms |
24460 KB |
Output is correct |
17 |
Correct |
18 ms |
24360 KB |
Output is correct |
18 |
Correct |
22 ms |
24736 KB |
Output is correct |
19 |
Correct |
17 ms |
24380 KB |
Output is correct |
20 |
Correct |
20 ms |
25184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
18 ms |
24136 KB |
Output is correct |
3 |
Correct |
15 ms |
24148 KB |
Output is correct |
4 |
Correct |
14 ms |
23796 KB |
Output is correct |
5 |
Correct |
14 ms |
23892 KB |
Output is correct |
6 |
Correct |
15 ms |
24084 KB |
Output is correct |
7 |
Correct |
13 ms |
23928 KB |
Output is correct |
8 |
Correct |
15 ms |
24020 KB |
Output is correct |
9 |
Correct |
20 ms |
24404 KB |
Output is correct |
10 |
Correct |
14 ms |
24476 KB |
Output is correct |
11 |
Correct |
15 ms |
24532 KB |
Output is correct |
12 |
Correct |
21 ms |
25200 KB |
Output is correct |
13 |
Correct |
20 ms |
25188 KB |
Output is correct |
14 |
Correct |
20 ms |
24464 KB |
Output is correct |
15 |
Correct |
21 ms |
24796 KB |
Output is correct |
16 |
Correct |
20 ms |
24460 KB |
Output is correct |
17 |
Correct |
18 ms |
24360 KB |
Output is correct |
18 |
Correct |
22 ms |
24736 KB |
Output is correct |
19 |
Correct |
17 ms |
24380 KB |
Output is correct |
20 |
Correct |
20 ms |
25184 KB |
Output is correct |
21 |
Correct |
29 ms |
25936 KB |
Output is correct |
22 |
Correct |
42 ms |
27204 KB |
Output is correct |
23 |
Correct |
46 ms |
28236 KB |
Output is correct |
24 |
Correct |
61 ms |
29376 KB |
Output is correct |
25 |
Correct |
38 ms |
33816 KB |
Output is correct |
26 |
Correct |
80 ms |
35752 KB |
Output is correct |
27 |
Correct |
57 ms |
28864 KB |
Output is correct |
28 |
Correct |
107 ms |
35916 KB |
Output is correct |
29 |
Correct |
82 ms |
35788 KB |
Output is correct |
30 |
Correct |
94 ms |
29552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
18 ms |
24136 KB |
Output is correct |
3 |
Correct |
15 ms |
24148 KB |
Output is correct |
4 |
Correct |
14 ms |
23796 KB |
Output is correct |
5 |
Correct |
14 ms |
23892 KB |
Output is correct |
6 |
Correct |
15 ms |
24084 KB |
Output is correct |
7 |
Correct |
13 ms |
23928 KB |
Output is correct |
8 |
Correct |
15 ms |
24020 KB |
Output is correct |
9 |
Correct |
20 ms |
24404 KB |
Output is correct |
10 |
Correct |
14 ms |
24476 KB |
Output is correct |
11 |
Correct |
449 ms |
54460 KB |
Output is correct |
12 |
Correct |
1042 ms |
86484 KB |
Output is correct |
13 |
Correct |
1240 ms |
90900 KB |
Output is correct |
14 |
Correct |
1102 ms |
95792 KB |
Output is correct |
15 |
Correct |
1141 ms |
95860 KB |
Output is correct |
16 |
Correct |
1066 ms |
94136 KB |
Output is correct |
17 |
Correct |
1196 ms |
89420 KB |
Output is correct |
18 |
Correct |
1819 ms |
164464 KB |
Output is correct |
19 |
Correct |
1997 ms |
174128 KB |
Output is correct |
20 |
Correct |
793 ms |
92860 KB |
Output is correct |
21 |
Correct |
15 ms |
24532 KB |
Output is correct |
22 |
Correct |
21 ms |
25200 KB |
Output is correct |
23 |
Correct |
20 ms |
25188 KB |
Output is correct |
24 |
Correct |
20 ms |
24464 KB |
Output is correct |
25 |
Correct |
21 ms |
24796 KB |
Output is correct |
26 |
Correct |
20 ms |
24460 KB |
Output is correct |
27 |
Correct |
18 ms |
24360 KB |
Output is correct |
28 |
Correct |
22 ms |
24736 KB |
Output is correct |
29 |
Correct |
17 ms |
24380 KB |
Output is correct |
30 |
Correct |
20 ms |
25184 KB |
Output is correct |
31 |
Correct |
29 ms |
25936 KB |
Output is correct |
32 |
Correct |
42 ms |
27204 KB |
Output is correct |
33 |
Correct |
46 ms |
28236 KB |
Output is correct |
34 |
Correct |
61 ms |
29376 KB |
Output is correct |
35 |
Correct |
38 ms |
33816 KB |
Output is correct |
36 |
Correct |
80 ms |
35752 KB |
Output is correct |
37 |
Correct |
57 ms |
28864 KB |
Output is correct |
38 |
Correct |
107 ms |
35916 KB |
Output is correct |
39 |
Correct |
82 ms |
35788 KB |
Output is correct |
40 |
Correct |
94 ms |
29552 KB |
Output is correct |
41 |
Correct |
226 ms |
47192 KB |
Output is correct |
42 |
Correct |
855 ms |
132060 KB |
Output is correct |
43 |
Correct |
326 ms |
121824 KB |
Output is correct |
44 |
Correct |
1333 ms |
161344 KB |
Output is correct |
45 |
Correct |
1494 ms |
157928 KB |
Output is correct |
46 |
Correct |
724 ms |
84400 KB |
Output is correct |
47 |
Correct |
933 ms |
85372 KB |
Output is correct |
48 |
Correct |
934 ms |
150708 KB |
Output is correct |
49 |
Correct |
688 ms |
84928 KB |
Output is correct |
50 |
Correct |
735 ms |
84288 KB |
Output is correct |
51 |
Correct |
429 ms |
110988 KB |
Output is correct |
52 |
Correct |
1123 ms |
134696 KB |
Output is correct |
53 |
Correct |
970 ms |
151660 KB |
Output is correct |
54 |
Correct |
1563 ms |
145524 KB |
Output is correct |
55 |
Correct |
1845 ms |
156596 KB |
Output is correct |