#include <bits/stdc++.h>
using namespace std;
int n;
struct UFDS{
int n;
vector<int> parent;
vector<int> edges;
vector<int> vertices;
vector<int> vcnt;
int numCycles = 0;
int cycleVertices = 0;
bool isLinear;
UFDS(int _n){
n=_n;
parent.resize(n);
edges.resize(n,0);
vertices.resize(n,1);
vcnt.resize(n,0);
isLinear = true;
for (int i = 0; i<n; i++){
parent[i]=i;
}
}
int root(int x){
if (x==parent[x]){return x;}
return parent[x]=root(parent[x]);
}
void connect(int a, int b){
vcnt[a]++;
vcnt[b]++;
if (vcnt[a]>2||vcnt[b]>2){isLinear = false;}
int ra = root(a);
int rb = root(b);
if (ra!=rb){
//rb is new root
parent[ra]=rb;
vertices[rb]+=vertices[ra];
edges[rb]+=edges[ra]+1;
} else {
//todo: what happens if we connect in existing cycle
edges[ra]++;
numCycles++;
isLinear = false;
cycleVertices+=vertices[rb];
}
}
bool connected(int a, int b){
return root(a)==root(a);
}
int getEdges(int x){
return edges[root(x)];
}
int getVertices(int x){
return vertices[root(x)];
}
};
vector<vector<int>> adjlist;
vector<UFDS> threeormore;
vector<int> nodesremove;
UFDS mainufds(0);
int numcritical;
vector<pair<int,int>> links;
void Init(int N_) {
n = N_;
adjlist.resize(n);
mainufds = UFDS(n);
numcritical = n;
}
void Link(int a, int b) {
if (threeormore.size()>0){
//just check all four
numcritical = 0;
for (int i = 0; i<threeormore.size(); i++){
if (!(nodesremove[i]==a||nodesremove[i]==b)){
threeormore[i].connect(a,b);
}
if (threeormore[i].isLinear){
//cout<<i<<' '<<a<<' '<<b<<' '<<'\n';
numcritical++;
}
}
} else {
//have we just made three things
if (adjlist[a].size()!=2&&adjlist[b].size()==2){
swap(a,b);
}
if (adjlist[a].size()==2){
//vector<int> nodesremove;
nodesremove.push_back(a);
for (int i : adjlist[a]){
nodesremove.push_back(i);
}
nodesremove.push_back(b);
//make new ufds without these nodes
for (int i = 0; i<4; i++){
threeormore.push_back(UFDS(n));
for (auto l : links){
if (l.first==nodesremove[i]||l.second==nodesremove[i]){continue;}
threeormore[i].connect(l.first, l.second);
}
}
Link(a,b);
return;
} else {
links.push_back({a,b});
mainufds.connect(a,b);
adjlist[a].push_back(b);
adjlist[b].push_back(a);
if (mainufds.numCycles==0){
numcritical = n;
} else if (mainufds.numCycles==1){
numcritical = mainufds.cycleVertices;
} else {
numcritical = 0;
}
}
}
}
int CountCritical() {
return numcritical;
}
Compilation message
rings.cpp: In function 'void Link(int, int)':
rings.cpp:88:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<UFDS>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int i = 0; i<threeormore.size(); i++){
| ~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
896 KB |
Output is correct |
3 |
Correct |
3 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
485 ms |
47912 KB |
Output is correct |
2 |
Correct |
1345 ms |
108640 KB |
Output is correct |
3 |
Correct |
2124 ms |
115352 KB |
Output is correct |
4 |
Correct |
1113 ms |
91988 KB |
Output is correct |
5 |
Correct |
1117 ms |
92244 KB |
Output is correct |
6 |
Correct |
1078 ms |
90196 KB |
Output is correct |
7 |
Correct |
1908 ms |
115320 KB |
Output is correct |
8 |
Correct |
1716 ms |
139724 KB |
Output is correct |
9 |
Correct |
1934 ms |
152404 KB |
Output is correct |
10 |
Correct |
830 ms |
89952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
896 KB |
Output is correct |
3 |
Correct |
3 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1024 KB |
Output is correct |
11 |
Correct |
3 ms |
1024 KB |
Output is correct |
12 |
Correct |
7 ms |
1920 KB |
Output is correct |
13 |
Correct |
7 ms |
1664 KB |
Output is correct |
14 |
Correct |
4 ms |
1408 KB |
Output is correct |
15 |
Correct |
5 ms |
2432 KB |
Output is correct |
16 |
Correct |
5 ms |
1280 KB |
Output is correct |
17 |
Correct |
5 ms |
1536 KB |
Output is correct |
18 |
Correct |
7 ms |
2560 KB |
Output is correct |
19 |
Correct |
6 ms |
1280 KB |
Output is correct |
20 |
Correct |
6 ms |
1792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
896 KB |
Output is correct |
3 |
Correct |
3 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1024 KB |
Output is correct |
11 |
Correct |
3 ms |
1024 KB |
Output is correct |
12 |
Correct |
7 ms |
1920 KB |
Output is correct |
13 |
Correct |
7 ms |
1664 KB |
Output is correct |
14 |
Correct |
4 ms |
1408 KB |
Output is correct |
15 |
Correct |
5 ms |
2432 KB |
Output is correct |
16 |
Correct |
5 ms |
1280 KB |
Output is correct |
17 |
Correct |
5 ms |
1536 KB |
Output is correct |
18 |
Correct |
7 ms |
2560 KB |
Output is correct |
19 |
Correct |
6 ms |
1280 KB |
Output is correct |
20 |
Correct |
6 ms |
1792 KB |
Output is correct |
21 |
Correct |
24 ms |
3996 KB |
Output is correct |
22 |
Correct |
36 ms |
6000 KB |
Output is correct |
23 |
Correct |
48 ms |
7664 KB |
Output is correct |
24 |
Correct |
69 ms |
10744 KB |
Output is correct |
25 |
Correct |
23 ms |
10812 KB |
Output is correct |
26 |
Correct |
68 ms |
11128 KB |
Output is correct |
27 |
Correct |
58 ms |
8044 KB |
Output is correct |
28 |
Correct |
77 ms |
10616 KB |
Output is correct |
29 |
Correct |
51 ms |
11512 KB |
Output is correct |
30 |
Correct |
74 ms |
9196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
896 KB |
Output is correct |
3 |
Correct |
3 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1024 KB |
Output is correct |
11 |
Correct |
485 ms |
47912 KB |
Output is correct |
12 |
Correct |
1345 ms |
108640 KB |
Output is correct |
13 |
Correct |
2124 ms |
115352 KB |
Output is correct |
14 |
Correct |
1113 ms |
91988 KB |
Output is correct |
15 |
Correct |
1117 ms |
92244 KB |
Output is correct |
16 |
Correct |
1078 ms |
90196 KB |
Output is correct |
17 |
Correct |
1908 ms |
115320 KB |
Output is correct |
18 |
Correct |
1716 ms |
139724 KB |
Output is correct |
19 |
Correct |
1934 ms |
152404 KB |
Output is correct |
20 |
Correct |
830 ms |
89952 KB |
Output is correct |
21 |
Correct |
3 ms |
1024 KB |
Output is correct |
22 |
Correct |
7 ms |
1920 KB |
Output is correct |
23 |
Correct |
7 ms |
1664 KB |
Output is correct |
24 |
Correct |
4 ms |
1408 KB |
Output is correct |
25 |
Correct |
5 ms |
2432 KB |
Output is correct |
26 |
Correct |
5 ms |
1280 KB |
Output is correct |
27 |
Correct |
5 ms |
1536 KB |
Output is correct |
28 |
Correct |
7 ms |
2560 KB |
Output is correct |
29 |
Correct |
6 ms |
1280 KB |
Output is correct |
30 |
Correct |
6 ms |
1792 KB |
Output is correct |
31 |
Correct |
24 ms |
3996 KB |
Output is correct |
32 |
Correct |
36 ms |
6000 KB |
Output is correct |
33 |
Correct |
48 ms |
7664 KB |
Output is correct |
34 |
Correct |
69 ms |
10744 KB |
Output is correct |
35 |
Correct |
23 ms |
10812 KB |
Output is correct |
36 |
Correct |
68 ms |
11128 KB |
Output is correct |
37 |
Correct |
58 ms |
8044 KB |
Output is correct |
38 |
Correct |
77 ms |
10616 KB |
Output is correct |
39 |
Correct |
51 ms |
11512 KB |
Output is correct |
40 |
Correct |
74 ms |
9196 KB |
Output is correct |
41 |
Correct |
246 ms |
33664 KB |
Output is correct |
42 |
Correct |
753 ms |
107840 KB |
Output is correct |
43 |
Correct |
352 ms |
99784 KB |
Output is correct |
44 |
Correct |
1152 ms |
106872 KB |
Output is correct |
45 |
Correct |
1342 ms |
112844 KB |
Output is correct |
46 |
Correct |
778 ms |
76756 KB |
Output is correct |
47 |
Correct |
980 ms |
78164 KB |
Output is correct |
48 |
Correct |
668 ms |
110840 KB |
Output is correct |
49 |
Correct |
741 ms |
81116 KB |
Output is correct |
50 |
Correct |
788 ms |
80212 KB |
Output is correct |
51 |
Correct |
396 ms |
85864 KB |
Output is correct |
52 |
Correct |
1025 ms |
86204 KB |
Output is correct |
53 |
Correct |
669 ms |
109544 KB |
Output is correct |
54 |
Correct |
1475 ms |
115744 KB |
Output is correct |
55 |
Correct |
1408 ms |
102600 KB |
Output is correct |