#include <bits/stdc++.h>
using namespace std;
#define synchronize {ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define ll long long
#define pii pair<int, int>
const int nmax = 1e6 + 5;
int theta, n, q;
int u, v, cyc, res;
bool check = false;
int par[nmax], sz[nmax], deg[nmax];
vector<pii> edges;
vector<int> adj[nmax];
struct DSU {
int root, ans;
vector<int> par, deg;
DSU() {
ans = 1;
root = -1;
par.resize(nmax + 1);
deg.resize(nmax + 1);
for (int i = 1; i <= nmax; i++) {
par[i] = i;
deg[i] = 0;
}
}
void init(int r) {
ans = 1; root = r;
for (int i = 1; i <= n; i++) {
par[i] = i;
// deg[i] = 0;
}
for (auto [u, v] : edges) {
merge(u, v);
}
}
int find(int u) {
if (u == par[u]) return u;
return par[u] = find(par[u]);
}
void merge(int u, int v) {
if (u == root || v == root || ans == 0) return;
if (++deg[u] == 3 || ++deg[v] == 3) {
ans = 0;
return;
}
u = find(u), v = find(v);
if (u == v) {
ans = 0;
return;
}
par[u] = v;
}
} ds[4];
int find(int u) {
if (u == par[u]) return u;
return par[u] = find(par[u]);
}
void Init(int _n) {
n = _n;
for (int i = 0; i <= n; i++) {
par[i] = i;
deg[i] = 0;
sz[i] = 1;
}
}
void Link(int u, int v) {
if (check) {
for (int i = 0; i < 4; i++)
ds[i].merge(u, v);
return;
}
edges.push_back({u, v});
adj[u].push_back(v);
adj[v].push_back(u);
if (deg[u] + 1 == 3 || deg[v] + 1 == 3) check = true;
if (++deg[u] == 3) {
ds[0].init(u);
for (int i = 1; i < 4; i++) ds[i].init(adj[u][i - 1]);
return;
}
if (++deg[v] == 3) {
ds[0].init(v);
for (int i = 1; i < 4; i++) ds[i].init(adj[v][i - 1]);
return;
}
u = find(u), v = find(v);
if (u == v) {
res = sz[u];
cyc++;
} else {
sz[u] += sz[v];
par[v] = u;
}
}
int CountCritical() {
if (cyc > 1) return 0;
if (check) return ds[0].ans + ds[1].ans + ds[2].ans + ds[3].ans;
if (cyc == 1) return res;
return n;
}
// int main() {
// synchronize;
// // cin >> theta;
// cin >> n >> q;
// init(n);
// for (int i = 1; i <= q; i++) {
// cin >> u;
// if (u == -1) {
// cout << count() << '\n';
// } else {
// cin >> v;
// merge(u, v);
// }
// }
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
60652 KB |
Output is correct |
2 |
Correct |
22 ms |
60752 KB |
Output is correct |
3 |
Correct |
23 ms |
60704 KB |
Output is correct |
4 |
Correct |
21 ms |
60496 KB |
Output is correct |
5 |
Correct |
22 ms |
60752 KB |
Output is correct |
6 |
Correct |
23 ms |
60752 KB |
Output is correct |
7 |
Correct |
21 ms |
60496 KB |
Output is correct |
8 |
Correct |
22 ms |
60920 KB |
Output is correct |
9 |
Correct |
24 ms |
60920 KB |
Output is correct |
10 |
Correct |
23 ms |
60752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
285 ms |
93096 KB |
Output is correct |
2 |
Correct |
351 ms |
95152 KB |
Output is correct |
3 |
Correct |
158 ms |
72548 KB |
Output is correct |
4 |
Correct |
799 ms |
119496 KB |
Output is correct |
5 |
Correct |
747 ms |
113280 KB |
Output is correct |
6 |
Correct |
654 ms |
112044 KB |
Output is correct |
7 |
Correct |
158 ms |
76904 KB |
Output is correct |
8 |
Correct |
585 ms |
111412 KB |
Output is correct |
9 |
Correct |
722 ms |
109364 KB |
Output is correct |
10 |
Correct |
499 ms |
111772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
60652 KB |
Output is correct |
2 |
Correct |
22 ms |
60752 KB |
Output is correct |
3 |
Correct |
23 ms |
60704 KB |
Output is correct |
4 |
Correct |
21 ms |
60496 KB |
Output is correct |
5 |
Correct |
22 ms |
60752 KB |
Output is correct |
6 |
Correct |
23 ms |
60752 KB |
Output is correct |
7 |
Correct |
21 ms |
60496 KB |
Output is correct |
8 |
Correct |
22 ms |
60920 KB |
Output is correct |
9 |
Correct |
24 ms |
60920 KB |
Output is correct |
10 |
Correct |
23 ms |
60752 KB |
Output is correct |
11 |
Correct |
31 ms |
60752 KB |
Output is correct |
12 |
Correct |
32 ms |
61008 KB |
Output is correct |
13 |
Correct |
24 ms |
61012 KB |
Output is correct |
14 |
Correct |
20 ms |
60752 KB |
Output is correct |
15 |
Correct |
28 ms |
60752 KB |
Output is correct |
16 |
Correct |
29 ms |
61008 KB |
Output is correct |
17 |
Correct |
22 ms |
60752 KB |
Output is correct |
18 |
Correct |
26 ms |
60772 KB |
Output is correct |
19 |
Correct |
24 ms |
61256 KB |
Output is correct |
20 |
Correct |
23 ms |
61008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
60652 KB |
Output is correct |
2 |
Correct |
22 ms |
60752 KB |
Output is correct |
3 |
Correct |
23 ms |
60704 KB |
Output is correct |
4 |
Correct |
21 ms |
60496 KB |
Output is correct |
5 |
Correct |
22 ms |
60752 KB |
Output is correct |
6 |
Correct |
23 ms |
60752 KB |
Output is correct |
7 |
Correct |
21 ms |
60496 KB |
Output is correct |
8 |
Correct |
22 ms |
60920 KB |
Output is correct |
9 |
Correct |
24 ms |
60920 KB |
Output is correct |
10 |
Correct |
23 ms |
60752 KB |
Output is correct |
11 |
Correct |
31 ms |
60752 KB |
Output is correct |
12 |
Correct |
32 ms |
61008 KB |
Output is correct |
13 |
Correct |
24 ms |
61012 KB |
Output is correct |
14 |
Correct |
20 ms |
60752 KB |
Output is correct |
15 |
Correct |
28 ms |
60752 KB |
Output is correct |
16 |
Correct |
29 ms |
61008 KB |
Output is correct |
17 |
Correct |
22 ms |
60752 KB |
Output is correct |
18 |
Correct |
26 ms |
60772 KB |
Output is correct |
19 |
Correct |
24 ms |
61256 KB |
Output is correct |
20 |
Correct |
23 ms |
61008 KB |
Output is correct |
21 |
Correct |
34 ms |
62324 KB |
Output is correct |
22 |
Correct |
44 ms |
63168 KB |
Output is correct |
23 |
Correct |
47 ms |
63680 KB |
Output is correct |
24 |
Correct |
40 ms |
61520 KB |
Output is correct |
25 |
Correct |
27 ms |
61008 KB |
Output is correct |
26 |
Correct |
38 ms |
61520 KB |
Output is correct |
27 |
Correct |
49 ms |
65040 KB |
Output is correct |
28 |
Correct |
36 ms |
61768 KB |
Output is correct |
29 |
Correct |
37 ms |
61492 KB |
Output is correct |
30 |
Correct |
55 ms |
65468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
60652 KB |
Output is correct |
2 |
Correct |
22 ms |
60752 KB |
Output is correct |
3 |
Correct |
23 ms |
60704 KB |
Output is correct |
4 |
Correct |
21 ms |
60496 KB |
Output is correct |
5 |
Correct |
22 ms |
60752 KB |
Output is correct |
6 |
Correct |
23 ms |
60752 KB |
Output is correct |
7 |
Correct |
21 ms |
60496 KB |
Output is correct |
8 |
Correct |
22 ms |
60920 KB |
Output is correct |
9 |
Correct |
24 ms |
60920 KB |
Output is correct |
10 |
Correct |
23 ms |
60752 KB |
Output is correct |
11 |
Correct |
285 ms |
93096 KB |
Output is correct |
12 |
Correct |
351 ms |
95152 KB |
Output is correct |
13 |
Correct |
158 ms |
72548 KB |
Output is correct |
14 |
Correct |
799 ms |
119496 KB |
Output is correct |
15 |
Correct |
747 ms |
113280 KB |
Output is correct |
16 |
Correct |
654 ms |
112044 KB |
Output is correct |
17 |
Correct |
158 ms |
76904 KB |
Output is correct |
18 |
Correct |
585 ms |
111412 KB |
Output is correct |
19 |
Correct |
722 ms |
109364 KB |
Output is correct |
20 |
Correct |
499 ms |
111772 KB |
Output is correct |
21 |
Correct |
31 ms |
60752 KB |
Output is correct |
22 |
Correct |
32 ms |
61008 KB |
Output is correct |
23 |
Correct |
24 ms |
61012 KB |
Output is correct |
24 |
Correct |
20 ms |
60752 KB |
Output is correct |
25 |
Correct |
28 ms |
60752 KB |
Output is correct |
26 |
Correct |
29 ms |
61008 KB |
Output is correct |
27 |
Correct |
22 ms |
60752 KB |
Output is correct |
28 |
Correct |
26 ms |
60772 KB |
Output is correct |
29 |
Correct |
24 ms |
61256 KB |
Output is correct |
30 |
Correct |
23 ms |
61008 KB |
Output is correct |
31 |
Correct |
34 ms |
62324 KB |
Output is correct |
32 |
Correct |
44 ms |
63168 KB |
Output is correct |
33 |
Correct |
47 ms |
63680 KB |
Output is correct |
34 |
Correct |
40 ms |
61520 KB |
Output is correct |
35 |
Correct |
27 ms |
61008 KB |
Output is correct |
36 |
Correct |
38 ms |
61520 KB |
Output is correct |
37 |
Correct |
49 ms |
65040 KB |
Output is correct |
38 |
Correct |
36 ms |
61768 KB |
Output is correct |
39 |
Correct |
37 ms |
61492 KB |
Output is correct |
40 |
Correct |
55 ms |
65468 KB |
Output is correct |
41 |
Correct |
140 ms |
80972 KB |
Output is correct |
42 |
Correct |
299 ms |
90196 KB |
Output is correct |
43 |
Correct |
141 ms |
72008 KB |
Output is correct |
44 |
Correct |
167 ms |
79176 KB |
Output is correct |
45 |
Correct |
176 ms |
78276 KB |
Output is correct |
46 |
Correct |
413 ms |
113064 KB |
Output is correct |
47 |
Correct |
599 ms |
113832 KB |
Output is correct |
48 |
Correct |
147 ms |
74312 KB |
Output is correct |
49 |
Correct |
434 ms |
108960 KB |
Output is correct |
50 |
Correct |
398 ms |
108456 KB |
Output is correct |
51 |
Correct |
114 ms |
71992 KB |
Output is correct |
52 |
Correct |
144 ms |
77640 KB |
Output is correct |
53 |
Correct |
123 ms |
73288 KB |
Output is correct |
54 |
Correct |
410 ms |
101552 KB |
Output is correct |
55 |
Correct |
200 ms |
79368 KB |
Output is correct |