#include <bits/stdc++.h>
using namespace std;
set<array<int, 2>> S2;
vector<vector<int>> adj;
struct UnionFind {
vector<int> root;
int cypt = -1;
bool deg3 = false;
int pt = -1;
void init(int N) {
root.resize(N);
fill(root.begin(),root.end(),-1);
}
void bye() {
root.clear();
root.shrink_to_fit();
}
int Find(int n) {
if(root[n]<0) return n;
int r = Find(root[n]);
root[n] = r;
return r;
}
void Merge(int x, int y) {
if(deg3) return;
if(pt!=-1&&cypt!=-1) return;
if(cypt==-2) return;
int d1 = adj[x].size() - (S2.find({min(pt, x), max(pt, x)})!=S2.end()?1:0);
int d2 = adj[y].size() - (S2.find({min(pt, y), max(pt, y)})!=S2.end()?1:0);
if(d1>=3) deg3 = true;
if(d2>=3) deg3 = true;
if(deg3) bye();
if(deg3) return;
x = Find(x), y = Find(y);
if(x==y) {
if(cypt==-1) cypt = x;
else cypt = -2;
if(cypt!=-1&&pt!=-1) bye();
return;
}
if(root[x]>root[y]) swap(x, y);
root[x] += root[y];
root[y] = x;
}
};
int N;
int cnt;
set<int> S;
UnionFind U;
UnionFind UF[4];
map<int, int> id;
int id2[5];
bool idcnt = false;
void Init(int _N) {
N = _N;
adj.resize(N);
int i;
U.init(N);
}
void Link(int x, int y) {
adj[x].push_back(y);
adj[y].push_back(x);
S2.insert({min(x, y), max(x, y)});
if(adj[x].size() == 3) {
set<int> S2;
S2.insert(x);
for(int n : adj[x]) S2.insert(n);
if(!idcnt) S = S2;
vector<int> v;
for(int n : S) {
if(S2.find(n) == S2.end()) v.push_back(n);
}
for(int n : v) S.erase(n);
}
if(adj[x].size() >= 4) {
if(!idcnt) S = set<int>{y};
if(S.find(x) == S.end()) S = set<int> {};
else S = set<int>{x};
}
if(adj[y].size() == 3) {
set<int> S2;
S2.insert(y);
for(int n : adj[y]) S2.insert(n);
if(!idcnt) S = S2;
vector<int> v;
for(int n : S) {
if(S2.find(n) == S2.end()) v.push_back(n);
}
for(int n : v) S.erase(n);
}
if(adj[y].size() >= 4) {
if(!idcnt) S = set<int>{y};
if(S.find(y) == S.end()) S = set<int> {};
else S = set<int>{y};
}
bool now = false;
if(S.size()>0&&!idcnt) {
idcnt = true;
now = true;
int ck = 0;
U.bye();
for(int n : S) {
id[n] = ck;
id2[ck] = n;
UF[ck].pt = n;
UF[ck].init(N);
for(auto k : S2) {
if(k[0] != n && k[1] != n) {
UF[ck].Merge(k[0], k[1]);
}
}
ck++;
}
}
if(!idcnt) U.Merge(x, y);
if(idcnt && !now) {
for(int s : S) {
if(x != s && y != s) {
UF[id[s]].Merge(x, y);
}
}
}
}
int CountCritical() {
if(!idcnt) {
//assert(!U.deg3);
if(U.cypt==-1) return N;
else if(U.cypt==-2) return 0;
else return -U.root[U.Find(U.cypt)];
}
else {
int cnt = 0;
for(int n : S) {
if(!UF[id[n]].deg3 && UF[id[n]].cypt == -1) cnt++;
}
return cnt;
}
}
Compilation message
rings.cpp: In function 'void Init(int)':
rings.cpp:57:9: warning: unused variable 'i' [-Wunused-variable]
57 | int i;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
812 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
636 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
6 ms |
924 KB |
Output is correct |
10 |
Correct |
6 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
885 ms |
55172 KB |
Output is correct |
2 |
Correct |
2450 ms |
94200 KB |
Output is correct |
3 |
Correct |
1857 ms |
110996 KB |
Output is correct |
4 |
Correct |
2153 ms |
105848 KB |
Output is correct |
5 |
Correct |
2183 ms |
106200 KB |
Output is correct |
6 |
Correct |
2090 ms |
103328 KB |
Output is correct |
7 |
Correct |
1741 ms |
108700 KB |
Output is correct |
8 |
Correct |
3351 ms |
110140 KB |
Output is correct |
9 |
Correct |
3760 ms |
117616 KB |
Output is correct |
10 |
Correct |
1549 ms |
114144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
812 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
636 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
6 ms |
924 KB |
Output is correct |
10 |
Correct |
6 ms |
852 KB |
Output is correct |
11 |
Correct |
6 ms |
820 KB |
Output is correct |
12 |
Correct |
13 ms |
1460 KB |
Output is correct |
13 |
Correct |
17 ms |
1492 KB |
Output is correct |
14 |
Correct |
8 ms |
1236 KB |
Output is correct |
15 |
Correct |
6 ms |
1492 KB |
Output is correct |
16 |
Correct |
7 ms |
1400 KB |
Output is correct |
17 |
Correct |
7 ms |
1352 KB |
Output is correct |
18 |
Correct |
8 ms |
2004 KB |
Output is correct |
19 |
Correct |
7 ms |
1364 KB |
Output is correct |
20 |
Correct |
17 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
812 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
636 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
6 ms |
924 KB |
Output is correct |
10 |
Correct |
6 ms |
852 KB |
Output is correct |
11 |
Correct |
6 ms |
820 KB |
Output is correct |
12 |
Correct |
13 ms |
1460 KB |
Output is correct |
13 |
Correct |
17 ms |
1492 KB |
Output is correct |
14 |
Correct |
8 ms |
1236 KB |
Output is correct |
15 |
Correct |
6 ms |
1492 KB |
Output is correct |
16 |
Correct |
7 ms |
1400 KB |
Output is correct |
17 |
Correct |
7 ms |
1352 KB |
Output is correct |
18 |
Correct |
8 ms |
2004 KB |
Output is correct |
19 |
Correct |
7 ms |
1364 KB |
Output is correct |
20 |
Correct |
17 ms |
1492 KB |
Output is correct |
21 |
Correct |
28 ms |
3940 KB |
Output is correct |
22 |
Correct |
45 ms |
6112 KB |
Output is correct |
23 |
Correct |
57 ms |
7552 KB |
Output is correct |
24 |
Correct |
76 ms |
8008 KB |
Output is correct |
25 |
Correct |
17 ms |
4816 KB |
Output is correct |
26 |
Correct |
72 ms |
9180 KB |
Output is correct |
27 |
Correct |
90 ms |
9300 KB |
Output is correct |
28 |
Correct |
86 ms |
9248 KB |
Output is correct |
29 |
Correct |
43 ms |
7252 KB |
Output is correct |
30 |
Correct |
120 ms |
10808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
812 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
636 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
6 ms |
924 KB |
Output is correct |
10 |
Correct |
6 ms |
852 KB |
Output is correct |
11 |
Correct |
885 ms |
55172 KB |
Output is correct |
12 |
Correct |
2450 ms |
94200 KB |
Output is correct |
13 |
Correct |
1857 ms |
110996 KB |
Output is correct |
14 |
Correct |
2153 ms |
105848 KB |
Output is correct |
15 |
Correct |
2183 ms |
106200 KB |
Output is correct |
16 |
Correct |
2090 ms |
103328 KB |
Output is correct |
17 |
Correct |
1741 ms |
108700 KB |
Output is correct |
18 |
Correct |
3351 ms |
110140 KB |
Output is correct |
19 |
Correct |
3760 ms |
117616 KB |
Output is correct |
20 |
Correct |
1549 ms |
114144 KB |
Output is correct |
21 |
Correct |
6 ms |
820 KB |
Output is correct |
22 |
Correct |
13 ms |
1460 KB |
Output is correct |
23 |
Correct |
17 ms |
1492 KB |
Output is correct |
24 |
Correct |
8 ms |
1236 KB |
Output is correct |
25 |
Correct |
6 ms |
1492 KB |
Output is correct |
26 |
Correct |
7 ms |
1400 KB |
Output is correct |
27 |
Correct |
7 ms |
1352 KB |
Output is correct |
28 |
Correct |
8 ms |
2004 KB |
Output is correct |
29 |
Correct |
7 ms |
1364 KB |
Output is correct |
30 |
Correct |
17 ms |
1492 KB |
Output is correct |
31 |
Correct |
28 ms |
3940 KB |
Output is correct |
32 |
Correct |
45 ms |
6112 KB |
Output is correct |
33 |
Correct |
57 ms |
7552 KB |
Output is correct |
34 |
Correct |
76 ms |
8008 KB |
Output is correct |
35 |
Correct |
17 ms |
4816 KB |
Output is correct |
36 |
Correct |
72 ms |
9180 KB |
Output is correct |
37 |
Correct |
90 ms |
9300 KB |
Output is correct |
38 |
Correct |
86 ms |
9248 KB |
Output is correct |
39 |
Correct |
43 ms |
7252 KB |
Output is correct |
40 |
Correct |
120 ms |
10808 KB |
Output is correct |
41 |
Correct |
339 ms |
35496 KB |
Output is correct |
42 |
Correct |
1614 ms |
76296 KB |
Output is correct |
43 |
Correct |
275 ms |
46608 KB |
Output is correct |
44 |
Correct |
1264 ms |
103832 KB |
Output is correct |
45 |
Correct |
1378 ms |
93532 KB |
Output is correct |
46 |
Correct |
1499 ms |
98892 KB |
Output is correct |
47 |
Correct |
1828 ms |
100664 KB |
Output is correct |
48 |
Correct |
702 ms |
77680 KB |
Output is correct |
49 |
Correct |
1294 ms |
96612 KB |
Output is correct |
50 |
Correct |
1356 ms |
95852 KB |
Output is correct |
51 |
Correct |
384 ms |
46140 KB |
Output is correct |
52 |
Correct |
1042 ms |
84476 KB |
Output is correct |
53 |
Correct |
709 ms |
79252 KB |
Output is correct |
54 |
Correct |
2873 ms |
106168 KB |
Output is correct |
55 |
Correct |
2204 ms |
115724 KB |
Output is correct |