#include <bits/stdc++.h>
using namespace std;
const int k_N = 1e6 + 3;
struct DSU{
int p[k_N], sz[k_N];
int mx[k_N], cycles[k_N];
int d[k_N];
int cycles_all, mx_all;
int cycle_node;
DSU(){
}
void update_d(int u){
d[u]++;
mx[get_p(u)] = max(mx[get_p(u)], d[u]);
}
void resize(int n){
cycles_all = 0;
mx_all = 0;
cycle_node = -1;
for(int i = 0; i < n; ++i){
p[i] = i;
sz[i] = 1;
cycles[i] = 0;
mx[i] = 0;
d[i] = 0;
}
}
int get_p(int u){
if(p[u] == u) return u;
return p[u] = get_p(p[u]);
}
bool unite(int u, int v){
if(get_p(u) == get_p(v))
return false;
if(sz[p[u]] < sz[p[v]])
swap(u, v);
sz[p[u]] += sz[p[v]];
cycles[p[u]] += cycles[p[v]];
mx[p[u]] = max(mx[p[u]], mx[p[v]]);
p[p[v]] = p[u];
return true;
}
void add(int a, int b){
update_d(a);
update_d(b);
mx_all = max(mx_all, max(d[a], d[b]));
if(!unite(a, b)){
cycle_node = a;
cycles_all++;
cycles[get_p(a)]++;
}
}
};
int n;
vector<pair<int, int>> edges;
vector<int> adj[k_N];
DSU dsu, dsu_cand[4];
int cand[4];
void Init(int n_){
n = n_;
dsu.resize(n);
}
void do_cand(int a){
cand[0] = a;
for(int i = 0; i < adj[a].size(); ++i)
cand[i + 1] = adj[a][i];
for(int i = 0; i < 4; ++i){
dsu_cand[i].resize(n);
for(auto [u, v]: edges){
if(u == a || v == a) continue;
dsu_cand[i].add(u, v);
}
}
}
void Link(int a, int b){
edges.push_back({a, b});
adj[a].push_back(b);
adj[b].push_back(a);
bool curr = false;
if(dsu.mx_all < 3 && adj[a].size() == 3){
do_cand(a);
curr = true;
}
else if(dsu.mx_all < 3 && adj[b].size() == 3){
do_cand(b);
curr = true;
}
dsu.add(a, b);
if(dsu.mx_all >= 3 && !curr){
for(int i = 0; i < 4; ++i){
if(a == cand[i] || b == cand[i]) continue;
dsu_cand[i].add(a, b);
}
}
}
int CountCritical(){
if(dsu.mx_all <= 2){
if(dsu.cycles_all >= 2)
return 0;
if(dsu.cycles_all == 1)
return dsu.sz[dsu.get_p(dsu.cycle_node)];
return n;
}
int ans = 0;
for(int i = 0; i < 4; ++i){
if(dsu_cand[i].mx_all < 3 && dsu_cand[i].cycles_all == 0)
ans++;
}
return ans;
}
Compilation message
rings.cpp: In function 'void do_cand(int)':
rings.cpp:87:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[a].size(); ++i)
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23808 KB |
Output is correct |
2 |
Incorrect |
17 ms |
24576 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
528 ms |
54796 KB |
Output is correct |
2 |
Correct |
2143 ms |
133900 KB |
Output is correct |
3 |
Correct |
2444 ms |
169140 KB |
Output is correct |
4 |
Correct |
1323 ms |
95956 KB |
Output is correct |
5 |
Correct |
1370 ms |
96084 KB |
Output is correct |
6 |
Correct |
1383 ms |
94344 KB |
Output is correct |
7 |
Correct |
2412 ms |
167780 KB |
Output is correct |
8 |
Incorrect |
2417 ms |
164820 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23808 KB |
Output is correct |
2 |
Incorrect |
17 ms |
24576 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23808 KB |
Output is correct |
2 |
Incorrect |
17 ms |
24576 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23808 KB |
Output is correct |
2 |
Incorrect |
17 ms |
24576 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |