#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;
vector<set<pii, greater<pii>>> degs;
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);
degs.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i, sz[i] = 1;
degs[i].insert({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;
for (pii p : degs[pb]) {
degs[pa].insert(p);
}
degs[pb].clear();
}
void Add(int a, int b) {
if (a == erased_node || b == erased_node) return;
Union(a, b);
int pa = Find(a);
degs[pa].erase(degs[pa].find({deg[a], a}));
degs[pa].erase(degs[pa].find({deg[b], b}));
deg[a]++, deg[b]++;
degs[pa].insert({deg[a], a});
degs[pa].insert({deg[b], b});
if (deg[a] > 2 || deg[b] > 2) {
bad_comps.insert(pa);
good = false;
}
}
};
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.degs[node].begin();
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:110:13: warning: unused variable 'cnt' [-Wunused-variable]
110 | int cnt = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23764 KB |
Output is correct |
2 |
Correct |
20 ms |
24888 KB |
Output is correct |
3 |
Correct |
26 ms |
25044 KB |
Output is correct |
4 |
Correct |
14 ms |
23892 KB |
Output is correct |
5 |
Correct |
21 ms |
24196 KB |
Output is correct |
6 |
Correct |
23 ms |
24660 KB |
Output is correct |
7 |
Correct |
16 ms |
24276 KB |
Output is correct |
8 |
Correct |
17 ms |
24332 KB |
Output is correct |
9 |
Correct |
33 ms |
26656 KB |
Output is correct |
10 |
Correct |
33 ms |
26724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1461 ms |
99352 KB |
Output is correct |
2 |
Execution timed out |
4074 ms |
224508 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23764 KB |
Output is correct |
2 |
Correct |
20 ms |
24888 KB |
Output is correct |
3 |
Correct |
26 ms |
25044 KB |
Output is correct |
4 |
Correct |
14 ms |
23892 KB |
Output is correct |
5 |
Correct |
21 ms |
24196 KB |
Output is correct |
6 |
Correct |
23 ms |
24660 KB |
Output is correct |
7 |
Correct |
16 ms |
24276 KB |
Output is correct |
8 |
Correct |
17 ms |
24332 KB |
Output is correct |
9 |
Correct |
33 ms |
26656 KB |
Output is correct |
10 |
Correct |
33 ms |
26724 KB |
Output is correct |
11 |
Correct |
36 ms |
26744 KB |
Output is correct |
12 |
Correct |
56 ms |
29672 KB |
Output is correct |
13 |
Correct |
55 ms |
29644 KB |
Output is correct |
14 |
Correct |
21 ms |
26268 KB |
Output is correct |
15 |
Correct |
27 ms |
28336 KB |
Output is correct |
16 |
Correct |
24 ms |
25328 KB |
Output is correct |
17 |
Correct |
22 ms |
25296 KB |
Output is correct |
18 |
Correct |
20 ms |
26460 KB |
Output is correct |
19 |
Correct |
24 ms |
25424 KB |
Output is correct |
20 |
Correct |
48 ms |
29592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23764 KB |
Output is correct |
2 |
Correct |
20 ms |
24888 KB |
Output is correct |
3 |
Correct |
26 ms |
25044 KB |
Output is correct |
4 |
Correct |
14 ms |
23892 KB |
Output is correct |
5 |
Correct |
21 ms |
24196 KB |
Output is correct |
6 |
Correct |
23 ms |
24660 KB |
Output is correct |
7 |
Correct |
16 ms |
24276 KB |
Output is correct |
8 |
Correct |
17 ms |
24332 KB |
Output is correct |
9 |
Correct |
33 ms |
26656 KB |
Output is correct |
10 |
Correct |
33 ms |
26724 KB |
Output is correct |
11 |
Correct |
36 ms |
26744 KB |
Output is correct |
12 |
Correct |
56 ms |
29672 KB |
Output is correct |
13 |
Correct |
55 ms |
29644 KB |
Output is correct |
14 |
Correct |
21 ms |
26268 KB |
Output is correct |
15 |
Correct |
27 ms |
28336 KB |
Output is correct |
16 |
Correct |
24 ms |
25328 KB |
Output is correct |
17 |
Correct |
22 ms |
25296 KB |
Output is correct |
18 |
Correct |
20 ms |
26460 KB |
Output is correct |
19 |
Correct |
24 ms |
25424 KB |
Output is correct |
20 |
Correct |
48 ms |
29592 KB |
Output is correct |
21 |
Correct |
52 ms |
30592 KB |
Output is correct |
22 |
Correct |
84 ms |
34448 KB |
Output is correct |
23 |
Correct |
105 ms |
37152 KB |
Output is correct |
24 |
Correct |
125 ms |
45612 KB |
Output is correct |
25 |
Correct |
105 ms |
76832 KB |
Output is correct |
26 |
Correct |
447 ms |
76860 KB |
Output is correct |
27 |
Correct |
124 ms |
36300 KB |
Output is correct |
28 |
Correct |
681 ms |
74364 KB |
Output is correct |
29 |
Correct |
211 ms |
79148 KB |
Output is correct |
30 |
Correct |
212 ms |
38612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23764 KB |
Output is correct |
2 |
Correct |
20 ms |
24888 KB |
Output is correct |
3 |
Correct |
26 ms |
25044 KB |
Output is correct |
4 |
Correct |
14 ms |
23892 KB |
Output is correct |
5 |
Correct |
21 ms |
24196 KB |
Output is correct |
6 |
Correct |
23 ms |
24660 KB |
Output is correct |
7 |
Correct |
16 ms |
24276 KB |
Output is correct |
8 |
Correct |
17 ms |
24332 KB |
Output is correct |
9 |
Correct |
33 ms |
26656 KB |
Output is correct |
10 |
Correct |
33 ms |
26724 KB |
Output is correct |
11 |
Correct |
1461 ms |
99352 KB |
Output is correct |
12 |
Execution timed out |
4074 ms |
224508 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |