#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 100000;
int N;
vector<int> g[1 + nmax];
vector<pair<int,int>> edge;
namespace Dsu{
int mult[1 + nmax];
int sz[1 + nmax];
void initialize(int n){
for(int i = 0;i < n; i++) {
mult[i] = i;
sz[i] = 1;
}
}
int jump(int a){
if(mult[a] != a)
mult[a] = jump(mult[a]);
return mult[a];
}
void unite(int gala, int galb){
gala = jump(gala);
galb = jump(galb);
if(sz[galb] < sz[gala])
swap(gala, galb);
if(gala != galb) {
sz[galb] += sz[gala];
mult[gala] = galb;
}
}
}
vector<int> candidates;
int deg[1 + nmax];
void Init(int N_) {
N = N_;
Dsu::initialize(N);
for(int i = 0; i < N; i++)
candidates.push_back(i);
}
bool test(int deleted){
Dsu::initialize(N);
for(int i = 0;i < N; i++)
deg[i] = 0;
for(int i = 0; i < edge.size(); i++){
int x = edge[i].first;
int y = edge[i].second;
if(x != deleted && y != deleted){
if(deg[x] < 2 && deg[y] < 2 && Dsu::jump(x) != Dsu::jump(y))
Dsu::unite(x, y);
else
return false;
deg[x]++;
deg[y]++;
}
}
return true;
}
void clearcandidates(){
vector<int> newcand;
for(int i = 0; i < candidates.size(); i++)
if(test(candidates[i]) == 1)
newcand.push_back(candidates[i]);
candidates = newcand;
Dsu::initialize(N);
for(int i = 0; i < edge.size(); i++){
int x = edge[i].first;
int y = edge[i].second;
Dsu::unite(x, y);
}
}
void Link(int a, int b) {
if(candidates.size() == 0)
return ;
if(g[b].size() < g[a].size())
swap(a, b);
edge.push_back({a, b});
g[a].push_back(b);
g[b].push_back(a);
if(g[a].size() <= 2 && g[b].size() <= 2) {
if(Dsu::jump(a) == Dsu::jump(b) && Dsu::jump(a) == Dsu::jump(candidates[0]))
clearcandidates();
Dsu::unite(a, b);
return ;
} else if(candidates.size() == 1 && candidates[0] == b) {
Dsu::unite(a, b);
return ;
} else if(candidates.size() == N){
candidates.clear();
for(int i = 0; i < g[a].size(); i++)
candidates.push_back(g[a][i]);
for(int i = 0; i < g[b].size(); i++)
candidates.push_back(g[b][i]);
sort(candidates.begin(), candidates.end());
candidates.erase(unique(candidates.begin(), candidates.end()), candidates.end());
Dsu::unite(a, b);
}
clearcandidates();
}
int CountCritical() {
std::cout << "result " << candidates.size() << '\n';
return candidates.size();
}
Compilation message
rings.cpp: In function 'bool test(int)':
rings.cpp:58:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < edge.size(); i++){
~~^~~~~~~~~~~~~
rings.cpp: In function 'void clearcandidates()':
rings.cpp:75:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < candidates.size(); i++)
~~^~~~~~~~~~~~~~~~~~~
rings.cpp:81:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < edge.size(); i++){
~~^~~~~~~~~~~~~
rings.cpp: In function 'void Link(int, int)':
rings.cpp:107:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
} else if(candidates.size() == N){
~~~~~~~~~~~~~~~~~~^~~~
rings.cpp:109:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[a].size(); i++)
~~^~~~~~~~~~~~~
rings.cpp:111:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[b].size(); i++)
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
18 ms |
11496 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 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |