#include <iostream>
#include <vector>
#include <algorithm>
int const nmax = 100000;
struct Dsu{
std::vector<int> mult;
std::vector<int> sz;
Dsu(int n = 0){
mult.resize(1 + n);
sz.resize(1 + n);
for(int i = 1;i <= n; i++) {
mult[i] = i;
sz[i] = 1;
}
}
int jump(int gala){
if(mult[gala] != gala)
mult[gala] = jump(mult[gala]);
return mult[gala];
}
void unite(int gala, int galb){
gala = jump(gala);
galb = jump(galb);
if(gala == galb)
return ;
if(sz[galb] < sz[gala])
std::swap(gala, galb);
sz[galb] += sz[gala];
mult[gala] = galb;
}
int getsize(int gala){
return sz[jump(gala)];
}
bool connected(int gala, int galb){
return (jump(gala) == jump(galb));
}
};
struct Ring{
int avoid;
std::vector<int> grade;
int iscycle, isgreat;
int cyclesize = 0;
int n;
Dsu mp;
Ring(int n_ = 0, int avoid_ = 0){
n = n_;
grade.resize(1 + n);
avoid = avoid_;
iscycle = isgreat = 0;
mp = Dsu(n);
}
void unite(int gala, int galb){
if(gala == avoid || galb == avoid)
return ;
if(mp.connected(gala, galb) == 1) {
iscycle++;
cyclesize = mp.getsize(gala);
}
mp.unite(gala, galb);
grade[gala]++;
grade[galb]++;
if(grade[gala] == 3 || grade[galb] == 3)
isgreat = 1;
}
bool valid(){
return ((0 == iscycle) && (0 == isgreat));
}
};
int N, phase = 1;
Ring universal;
int result;
void Init(int N_) {
N = N_;
result = N;
universal = Ring(N, 0);
}
std::vector<std::pair<int,int>> edges;
std::vector<int> g[1 + nmax];
std::vector<std::pair<int,Ring> > cand;
void Link(int a, int b) {
++a;
++b;
edges.push_back({a, b});
g[a].push_back(b);
g[b].push_back(a);
universal.unite(a, b);
if(phase < 3 && universal.isgreat == 1){
phase = 3;
std::vector<int> temp;
for(int h = 0; h < g[a].size(); h++)
temp.push_back(g[a][h]);
for(int h = 0; h < g[b].size(); h++)
temp.push_back(g[b][h]);
std::sort(temp.begin(), temp.end());
temp.erase(unique(temp.begin(), temp.end()), temp.end());
for(int i = 0; i < temp.size(); i++){
cand.push_back({temp[i], Ring(N, temp[i])});
for(int j = 0; j < edges.size(); j++)
cand[i].second.unite(edges[j].first, edges[j].second);
}
} else if(phase == 3){
for(int i = 0; i < cand.size(); i++)
cand[i].second.unite(a, b);
} else if(1 < universal.iscycle){
result = 0;
} else if(phase == 1 && 1 == universal.iscycle){
phase = 2;
result = universal.cyclesize;
}
}
int CountCritical() {
if(phase < 3)
return result;
else {
result = 0;
for(int i = 0; i < cand.size(); i++)
result += cand[i].second.valid();
return result;
}
}
Compilation message
rings.cpp: In function 'void Link(int, int)':
rings.cpp:112:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[a].size(); h++)
~~^~~~~~~~~~~~~
rings.cpp:114:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[b].size(); h++)
~~^~~~~~~~~~~~~
rings.cpp:119:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < temp.size(); i++){
~~^~~~~~~~~~~~~
rings.cpp:121:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < edges.size(); j++)
~~^~~~~~~~~~~~~~
rings.cpp:126:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < cand.size(); i++)
~~^~~~~~~~~~~~~
rings.cpp: In function 'int CountCritical()':
rings.cpp:141:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < cand.size(); i++)
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
8 ms |
3200 KB |
Output is correct |
3 |
Correct |
9 ms |
3328 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
7 ms |
2816 KB |
Output is correct |
6 |
Correct |
9 ms |
3056 KB |
Output is correct |
7 |
Correct |
6 ms |
3072 KB |
Output is correct |
8 |
Correct |
9 ms |
2944 KB |
Output is correct |
9 |
Correct |
9 ms |
3328 KB |
Output is correct |
10 |
Correct |
9 ms |
3328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
20 ms |
17792 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
8 ms |
3200 KB |
Output is correct |
3 |
Correct |
9 ms |
3328 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
7 ms |
2816 KB |
Output is correct |
6 |
Correct |
9 ms |
3056 KB |
Output is correct |
7 |
Correct |
6 ms |
3072 KB |
Output is correct |
8 |
Correct |
9 ms |
2944 KB |
Output is correct |
9 |
Correct |
9 ms |
3328 KB |
Output is correct |
10 |
Correct |
9 ms |
3328 KB |
Output is correct |
11 |
Correct |
10 ms |
3328 KB |
Output is correct |
12 |
Correct |
12 ms |
3840 KB |
Output is correct |
13 |
Correct |
15 ms |
3968 KB |
Output is correct |
14 |
Correct |
11 ms |
3584 KB |
Output is correct |
15 |
Correct |
11 ms |
4096 KB |
Output is correct |
16 |
Correct |
10 ms |
3328 KB |
Output is correct |
17 |
Correct |
13 ms |
3840 KB |
Output is correct |
18 |
Correct |
14 ms |
4480 KB |
Output is correct |
19 |
Correct |
11 ms |
3328 KB |
Output is correct |
20 |
Correct |
13 ms |
3840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
8 ms |
3200 KB |
Output is correct |
3 |
Correct |
9 ms |
3328 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
7 ms |
2816 KB |
Output is correct |
6 |
Correct |
9 ms |
3056 KB |
Output is correct |
7 |
Correct |
6 ms |
3072 KB |
Output is correct |
8 |
Correct |
9 ms |
2944 KB |
Output is correct |
9 |
Correct |
9 ms |
3328 KB |
Output is correct |
10 |
Correct |
9 ms |
3328 KB |
Output is correct |
11 |
Correct |
10 ms |
3328 KB |
Output is correct |
12 |
Correct |
12 ms |
3840 KB |
Output is correct |
13 |
Correct |
15 ms |
3968 KB |
Output is correct |
14 |
Correct |
11 ms |
3584 KB |
Output is correct |
15 |
Correct |
11 ms |
4096 KB |
Output is correct |
16 |
Correct |
10 ms |
3328 KB |
Output is correct |
17 |
Correct |
13 ms |
3840 KB |
Output is correct |
18 |
Correct |
14 ms |
4480 KB |
Output is correct |
19 |
Correct |
11 ms |
3328 KB |
Output is correct |
20 |
Correct |
13 ms |
3840 KB |
Output is correct |
21 |
Correct |
27 ms |
4884 KB |
Output is correct |
22 |
Correct |
39 ms |
6132 KB |
Output is correct |
23 |
Correct |
52 ms |
7152 KB |
Output is correct |
24 |
Correct |
99 ms |
10992 KB |
Output is correct |
25 |
Correct |
27 ms |
9088 KB |
Output is correct |
26 |
Correct |
87 ms |
11888 KB |
Output is correct |
27 |
Correct |
66 ms |
8172 KB |
Output is correct |
28 |
Correct |
137 ms |
12532 KB |
Output is correct |
29 |
Correct |
72 ms |
10996 KB |
Output is correct |
30 |
Correct |
88 ms |
8812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
8 ms |
3200 KB |
Output is correct |
3 |
Correct |
9 ms |
3328 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
7 ms |
2816 KB |
Output is correct |
6 |
Correct |
9 ms |
3056 KB |
Output is correct |
7 |
Correct |
6 ms |
3072 KB |
Output is correct |
8 |
Correct |
9 ms |
2944 KB |
Output is correct |
9 |
Correct |
9 ms |
3328 KB |
Output is correct |
10 |
Correct |
9 ms |
3328 KB |
Output is correct |
11 |
Runtime error |
20 ms |
17792 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Halted |
0 ms |
0 KB |
- |