#pragma GCC optimize("O3")
#include <bits/stdc++.h>
//#include "grader.h"
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
using namespace std;
const int MAXN = 1e6 + 5;
int par[MAXN], deg[MAXN], cycle = 0, cnt[MAXN], n = 0;
int mrk[MAXN];
vi cand, adj[MAXN];
struct DSU {
int par[MAXN], deg[MAXN], root = 0;
bool valid = true;
int qry(int x) {
if (!valid) return 0;
return x == par[x] ? x : par[x] = qry(par[x]);
}
void join(int u, int v) {
if (!valid) return;
u = qry(u), v = qry(v);
par[u] = v;
}
void init(int n) {
for (int i = 0; i < n; i++)
par[i] = i;
}
} e, f;
int qry(int x) {
return x == par[x] ? x : par[x] = qry(par[x]);
}
void join(int u, int v) {
u = qry(u), v = qry(v);
par[u] = v;
}
void Init(int N) {
n = N;
for (int i = 0; i < N; i++) {
par[i] = i;
cand.pb(i);
mrk[i] = 1;
}
}
void do3(int b) {
vi tmp;
for (int v: adj[b])
if (mrk[v])
tmp.pb(v);
if (mrk[b])
tmp.pb(b);
for (int v: cand)
mrk[v] = 0;
for (int v: tmp)
mrk[v] = 1;
cand = tmp;
}
int par2[MAXN];
bool fin = false;
vii edg;
void dfs(int u, int p) {
par2[u] = p;
for (int v: adj[u])
if (v != p)
dfs(v, u);
}
void addE(int u, int v) {
if (u == e.root || v == e.root || e.valid == false) return;
e.deg[u]++, e.deg[v]++;
if (e.qry(u) == e.qry(v) || e.deg[u] > 2 || e.deg[v] > 2)
e.valid = false;
e.join(u, v);
}
void addF(int u, int v) {
if (u == f.root || v == f.root || f.valid == false) return;
f.deg[u]++, f.deg[v]++;
if (f.qry(u) == f.qry(v) || f.deg[u] > 2 || f.deg[v] > 2)
f.valid = false;
f.join(u, v);
}
void Link(int a, int b) {
if (cand.empty()) return;
if (fin) {
addE(a, b);
addF(a, b);
return;
}
edg.pb({a, b});
int delta = 0;
if (qry(a) == qry(b) && cycle == 0) {
dfs(a, -1);
vi tmp = {a};
int u = b;
while (u != a) {
tmp.pb(u);
u = par2[u];
}
vi nx;
for (int i: tmp)
if (mrk[i] == 1) {
nx.pb(i);
mrk[i] = 2;
}
for (int i: cand)
mrk[i]--;
cand = nx;
}
adj[a].pb(b);
adj[b].pb(a);
if (qry(a) == qry(b)) {
delta++;
cycle++;
}
cnt[deg[a]]--;
cnt[deg[b]]--;
deg[a]++, deg[b]++;
cnt[deg[a]]++;
cnt[deg[b]]++;
if (cnt[4] > 1) {
cand = {};
return;
}
if (deg[a] > deg[b])
swap(a, b);
if (deg[b] == 3) do3(b);
if (deg[a] == 3) do3(a);
if (deg[b] == 4) {
if (!mrk[b]) {
cand = {};
return;
}
memset(mrk, 0, sizeof mrk);
mrk[b] = 1;
cand = {b};
}
join(a, b);
if (cand.empty()) return;
if (cycle > 1 && delta > 0) {
if (qry(a) != qry(cand[0])) {
cand = {};
return;
}
if (mrk[a] && mrk[b]) {
cand = {a, b};
memset(mrk, 0, sizeof mrk);
mrk[a] = 1, mrk[b] = 1;
} else if (mrk[a] || mrk[b]) {
if (mrk[a]) {
memset(mrk, 0, sizeof mrk);
mrk[a] = 1;
cand = {a};
} else {
memset(mrk, 0, sizeof mrk);
mrk[b] = 1;
cand = {b};
}
}
fin = true;
if (cand.size() == 1) {
f.valid = false;
e.init(n);
e.root = cand[0];
for (int i = 0; i < edg.size(); i++) {
int u, v;
tie(u, v) = edg[i];
if (u == cand[0] || v == cand[0]) continue;
e.deg[u]++, e.deg[v]++;
if (e.qry(u) == e.qry(v) || e.deg[u] > 2 || e.deg[v] > 2) {
cand = {};
return;
}
e.join(u, v);
}
} else {
e.init(n);
e.root = cand[0];
for (int i = 0; e.valid && i < edg.size(); i++) {
int u, v;
tie(u, v) = edg[i];
if (u == cand[0] || v == cand[0]) continue;
e.deg[u]++, e.deg[v]++;
if (e.qry(u) == e.qry(v) || e.deg[u] > 2 || e.deg[v] > 2)
e.valid = false;
e.join(u, v);
}
f.init(n);
f.root = cand[1];
for (int i = 0; f.valid && i < edg.size(); i++) {
int u, v;
tie(u, v) = edg[i];
if (u == cand[1] || v == cand[1]) continue;
f.deg[u]++, f.deg[v]++;
if (f.qry(u) == f.qry(v) || f.deg[u] > 2 || f.deg[v] > 2)
f.valid = false;
f.join(u, v);
}
}
}
}
int CountCritical() {
int ret = 0;
if (fin) {
ret += (e.valid ? 1 : 0);
ret += (f.valid ? 1 : 0);
return ret;
}
return cand.size();
}
Compilation message
rings.cpp: In function 'void Link(int, int)':
rings.cpp:174:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < edg.size(); i++) {
~~^~~~~~~~~~~~
rings.cpp:188:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; e.valid && i < edg.size(); i++) {
~~^~~~~~~~~~~~
rings.cpp:199:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; f.valid && i < edg.size(); i++) {
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
23808 KB |
Output is correct |
2 |
Correct |
35 ms |
28024 KB |
Output is correct |
3 |
Correct |
32 ms |
28160 KB |
Output is correct |
4 |
Correct |
31 ms |
23904 KB |
Output is correct |
5 |
Correct |
31 ms |
24056 KB |
Output is correct |
6 |
Correct |
34 ms |
24312 KB |
Output is correct |
7 |
Correct |
26 ms |
23936 KB |
Output is correct |
8 |
Correct |
30 ms |
24184 KB |
Output is correct |
9 |
Correct |
30 ms |
24184 KB |
Output is correct |
10 |
Correct |
32 ms |
24184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
643 ms |
52396 KB |
Output is correct |
2 |
Correct |
1337 ms |
68608 KB |
Output is correct |
3 |
Correct |
410 ms |
40220 KB |
Output is correct |
4 |
Correct |
1614 ms |
80332 KB |
Output is correct |
5 |
Correct |
1630 ms |
83784 KB |
Output is correct |
6 |
Correct |
1944 ms |
101076 KB |
Output is correct |
7 |
Correct |
335 ms |
40368 KB |
Output is correct |
8 |
Correct |
1503 ms |
75436 KB |
Output is correct |
9 |
Correct |
1676 ms |
78796 KB |
Output is correct |
10 |
Correct |
1037 ms |
78492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
23808 KB |
Output is correct |
2 |
Correct |
35 ms |
28024 KB |
Output is correct |
3 |
Correct |
32 ms |
28160 KB |
Output is correct |
4 |
Correct |
31 ms |
23904 KB |
Output is correct |
5 |
Correct |
31 ms |
24056 KB |
Output is correct |
6 |
Correct |
34 ms |
24312 KB |
Output is correct |
7 |
Correct |
26 ms |
23936 KB |
Output is correct |
8 |
Correct |
30 ms |
24184 KB |
Output is correct |
9 |
Correct |
30 ms |
24184 KB |
Output is correct |
10 |
Correct |
32 ms |
24184 KB |
Output is correct |
11 |
Correct |
34 ms |
28152 KB |
Output is correct |
12 |
Correct |
36 ms |
24600 KB |
Output is correct |
13 |
Correct |
38 ms |
28412 KB |
Output is correct |
14 |
Correct |
35 ms |
28200 KB |
Output is correct |
15 |
Correct |
36 ms |
28568 KB |
Output is correct |
16 |
Correct |
31 ms |
24576 KB |
Output is correct |
17 |
Correct |
29 ms |
24184 KB |
Output is correct |
18 |
Correct |
29 ms |
24440 KB |
Output is correct |
19 |
Correct |
33 ms |
24748 KB |
Output is correct |
20 |
Correct |
34 ms |
24576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
23808 KB |
Output is correct |
2 |
Correct |
35 ms |
28024 KB |
Output is correct |
3 |
Correct |
32 ms |
28160 KB |
Output is correct |
4 |
Correct |
31 ms |
23904 KB |
Output is correct |
5 |
Correct |
31 ms |
24056 KB |
Output is correct |
6 |
Correct |
34 ms |
24312 KB |
Output is correct |
7 |
Correct |
26 ms |
23936 KB |
Output is correct |
8 |
Correct |
30 ms |
24184 KB |
Output is correct |
9 |
Correct |
30 ms |
24184 KB |
Output is correct |
10 |
Correct |
32 ms |
24184 KB |
Output is correct |
11 |
Correct |
34 ms |
28152 KB |
Output is correct |
12 |
Correct |
36 ms |
24600 KB |
Output is correct |
13 |
Correct |
38 ms |
28412 KB |
Output is correct |
14 |
Correct |
35 ms |
28200 KB |
Output is correct |
15 |
Correct |
36 ms |
28568 KB |
Output is correct |
16 |
Correct |
31 ms |
24576 KB |
Output is correct |
17 |
Correct |
29 ms |
24184 KB |
Output is correct |
18 |
Correct |
29 ms |
24440 KB |
Output is correct |
19 |
Correct |
33 ms |
24748 KB |
Output is correct |
20 |
Correct |
34 ms |
24576 KB |
Output is correct |
21 |
Correct |
51 ms |
26228 KB |
Output is correct |
22 |
Correct |
70 ms |
27380 KB |
Output is correct |
23 |
Correct |
81 ms |
28280 KB |
Output is correct |
24 |
Correct |
73 ms |
30624 KB |
Output is correct |
25 |
Correct |
48 ms |
30584 KB |
Output is correct |
26 |
Correct |
86 ms |
32212 KB |
Output is correct |
27 |
Correct |
109 ms |
28788 KB |
Output is correct |
28 |
Correct |
55 ms |
25856 KB |
Output is correct |
29 |
Correct |
55 ms |
26100 KB |
Output is correct |
30 |
Correct |
185 ms |
30496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
23808 KB |
Output is correct |
2 |
Correct |
35 ms |
28024 KB |
Output is correct |
3 |
Correct |
32 ms |
28160 KB |
Output is correct |
4 |
Correct |
31 ms |
23904 KB |
Output is correct |
5 |
Correct |
31 ms |
24056 KB |
Output is correct |
6 |
Correct |
34 ms |
24312 KB |
Output is correct |
7 |
Correct |
26 ms |
23936 KB |
Output is correct |
8 |
Correct |
30 ms |
24184 KB |
Output is correct |
9 |
Correct |
30 ms |
24184 KB |
Output is correct |
10 |
Correct |
32 ms |
24184 KB |
Output is correct |
11 |
Correct |
643 ms |
52396 KB |
Output is correct |
12 |
Correct |
1337 ms |
68608 KB |
Output is correct |
13 |
Correct |
410 ms |
40220 KB |
Output is correct |
14 |
Correct |
1614 ms |
80332 KB |
Output is correct |
15 |
Correct |
1630 ms |
83784 KB |
Output is correct |
16 |
Correct |
1944 ms |
101076 KB |
Output is correct |
17 |
Correct |
335 ms |
40368 KB |
Output is correct |
18 |
Correct |
1503 ms |
75436 KB |
Output is correct |
19 |
Correct |
1676 ms |
78796 KB |
Output is correct |
20 |
Correct |
1037 ms |
78492 KB |
Output is correct |
21 |
Correct |
34 ms |
28152 KB |
Output is correct |
22 |
Correct |
36 ms |
24600 KB |
Output is correct |
23 |
Correct |
38 ms |
28412 KB |
Output is correct |
24 |
Correct |
35 ms |
28200 KB |
Output is correct |
25 |
Correct |
36 ms |
28568 KB |
Output is correct |
26 |
Correct |
31 ms |
24576 KB |
Output is correct |
27 |
Correct |
29 ms |
24184 KB |
Output is correct |
28 |
Correct |
29 ms |
24440 KB |
Output is correct |
29 |
Correct |
33 ms |
24748 KB |
Output is correct |
30 |
Correct |
34 ms |
24576 KB |
Output is correct |
31 |
Correct |
51 ms |
26228 KB |
Output is correct |
32 |
Correct |
70 ms |
27380 KB |
Output is correct |
33 |
Correct |
81 ms |
28280 KB |
Output is correct |
34 |
Correct |
73 ms |
30624 KB |
Output is correct |
35 |
Correct |
48 ms |
30584 KB |
Output is correct |
36 |
Correct |
86 ms |
32212 KB |
Output is correct |
37 |
Correct |
109 ms |
28788 KB |
Output is correct |
38 |
Correct |
55 ms |
25856 KB |
Output is correct |
39 |
Correct |
55 ms |
26100 KB |
Output is correct |
40 |
Correct |
185 ms |
30496 KB |
Output is correct |
41 |
Correct |
283 ms |
42100 KB |
Output is correct |
42 |
Correct |
668 ms |
56596 KB |
Output is correct |
43 |
Correct |
288 ms |
50560 KB |
Output is correct |
44 |
Correct |
318 ms |
40544 KB |
Output is correct |
45 |
Correct |
483 ms |
53216 KB |
Output is correct |
46 |
Correct |
903 ms |
69976 KB |
Output is correct |
47 |
Correct |
1522 ms |
86768 KB |
Output is correct |
48 |
Correct |
300 ms |
42208 KB |
Output is correct |
49 |
Correct |
938 ms |
70876 KB |
Output is correct |
50 |
Correct |
1069 ms |
70428 KB |
Output is correct |
51 |
Correct |
369 ms |
50736 KB |
Output is correct |
52 |
Correct |
295 ms |
37296 KB |
Output is correct |
53 |
Correct |
254 ms |
41436 KB |
Output is correct |
54 |
Correct |
1225 ms |
69024 KB |
Output is correct |
55 |
Correct |
1372 ms |
73188 KB |
Output is correct |