#include<bits/stdc++.h>
using namespace std;
int nbCycles = 0;
int szCycle= 0;
struct DSU{
int nbGroups =0;
vector<int> anc;
vector<int> sz;
vector<pair<int, pair<int, int>>> op_buffer;
DSU(){};
DSU(int l){
anc.resize(l);
sz.resize(l);
for(int i = 0; i<l; i++){
anc[i] = i;
sz[i] = 1;
}
nbGroups = l;
}
int c(int u){
op_buffer.push_back({u, {anc[u], sz[u]}});
if(anc[u] == u){
return u;
}
int v= c(anc[u]);
anc[u] =v;
return v;
}
void merge(int a, int b){
//cerr<<"merging "<<a<<" "<<b<<endl;
a = c(a);
b= c(b);
if(a==b){
nbCycles ++;
szCycle = sz[a];
return;
}
nbGroups--;
op_buffer.push_back({a, {anc[a], sz[a]}});
op_buffer.push_back({b, {anc[b], sz[b]}});
if(sz[a]>sz[b]){
swap(a, b);
}
anc[a] = b;
sz[b] += sz[a];
}
void undo_buffer(){
while(op_buffer.size()>0){
auto e= op_buffer.back();
if(anc[e.first] == e.first){
nbGroups--;
}
op_buffer.pop_back();
anc[e.first] = e.second.first;
sz[e.first] = e.second.second;
if(anc[e.first] == e.first){
nbGroups++;
}
}
}
};
int N;
const int MAX_N= 1e6;
vector<int> adj[MAX_N];
vector<int> interesting;
bool isInteresting[MAX_N];
int deg[MAX_N], oc[MAX_N];
DSU curDSU;
int above3= 0;
int above4 = 0;
DSU recalcDSU(){
DSU newDSU(N);
for(int iV = 0; iV<N; iV++){
for(auto u: adj[iV]){
if(!(isInteresting[u] || isInteresting[iV])){
newDSU.merge(iV, u);
}
}
}
newDSU.op_buffer.clear();
return newDSU;
}
bool lost3 = false;
int count3(int u){
int res= 0;
for(auto e: adj[u]){
if(deg[e]>=3){
res++;
}
}
return res;
}
void changeDeg(int u, int d){
if(deg[u]>=3){
above3--;
}
if(deg[u] >= 4){
above4--;
}
oc[deg[u]]--;
deg[u] +=d;
oc[deg[u]] ++;
if(deg[u]>=3){
above3++;
}
if(deg[u] >= 4){
above4++;
}
}
void Init(int N_) {
N = N_;
fill(&oc[0], &oc[N], 0);
oc[0] =N;
curDSU = DSU(N);
}
bool check_possible(){
if(interesting.size() == 0){
return true;
}
unordered_map<int, int> m;
int target = 0;
for(auto e: interesting){
if(deg[e] == 3){
for(auto ee: adj[e]){
m[ee]++;
}
m[e]++;
target++;
}
}
for(auto e: m){
if(e.second == target){
return true;
}
}
return false;
}
bool changed = true;
void clear_interesting(){
vector<int> new_interesting;
for(auto e: interesting){
if(deg[e] != 4){
isInteresting[e] = false;
}
else{
new_interesting.push_back(e);
}
}
for(auto e: interesting){
for(auto ee: adj[e]){
if(!(isInteresting[ee] ||isInteresting[e])){
curDSU.merge(e, ee);
}
}
}
swap(interesting, new_interesting);
}
void Link(int A, int B) {
changed = true;
if(above4>1 || lost3){
return;
}
adj[A].push_back(B);
changeDeg(A, +1);
adj[B].push_back(A);
changeDeg(B, +1);
if(above4 == 0 && (deg[A]== 3 || deg[B]==3)){
if(deg[A] != 3){
swap(A, B);
}
if(!isInteresting[A]){
isInteresting[A] = true;
interesting.push_back(A);
}
for(auto e: adj[A]){
if(!isInteresting[e]){
isInteresting[e] = true;
interesting.push_back(e);
}
}
if(deg[B] == 3){
if(!isInteresting[B]){
isInteresting[B] = true;
interesting.push_back(B);
}
for(auto e: adj[B]){
if(!isInteresting[e]){
isInteresting[e] = true;
interesting.push_back(e);
}
}
}
curDSU = recalcDSU();
}
else if(isInteresting[A] || isInteresting[B]){
}
else{
curDSU.merge(A, B);
curDSU.op_buffer.clear();
}
if((deg[A] == 4 || deg[B]== 4) && above4 == 1){
clear_interesting();
}
if(deg[A]>=4){
if(count3(A)<above3-1){
lost3 = true;
}
}
if(deg[B]>=4){
if(count3(B)<above3-1){
lost3 = true;
}
}
if(above4 == 0){
if(!check_possible()){
lost3 = true;
}
}
}
int prev_res= 0;
int calc(){
if(above4>1 || lost3){
return 0;
}
else{
if(above3>=1){
int res= 0;
for(auto iAdd: interesting){
for(auto e: adj[iAdd]){
if(!isInteresting[e]){
changeDeg(e, -1);
}
changeDeg(iAdd, -1);
}
}
for(auto iRem: interesting){
if(deg[iRem]>=4 || above4 ==0){
for(auto iAdd: interesting){
if(iAdd!=iRem){
for(auto e: adj[iAdd]){
if(e!=iRem){
curDSU.merge(e, iAdd);
if(!isInteresting[e]){
changeDeg(e, 1);
}
changeDeg(iAdd, 1);
}
}
}
}
if((oc[0]-1)*2 + oc[1] == (curDSU.nbGroups-1)*2 && above3 ==0){
res++;
}
for(auto iAdd: interesting){
if(iAdd!=iRem){
for(auto e: adj[iAdd]){
if(e!=iRem){
if(!isInteresting[e]){
changeDeg(e, -1);
}
changeDeg(iAdd, -1);
}
}
}
}
curDSU.undo_buffer();
}
}
for(auto iAdd: interesting){
for(auto e: adj[iAdd]){
if(!isInteresting[e]){
changeDeg(e, 1);
}
changeDeg(iAdd, 1);
}
}
return res;
}
else{
if(nbCycles == 0){
return N;
}
else if(nbCycles == 1){
return szCycle;
}
else{
return 0;
}
}
}
}
int CountCritical() {
if(!changed){
return prev_res;
}
prev_res = calc();
return prev_res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
13 ms |
24424 KB |
Output is correct |
3 |
Correct |
12 ms |
24140 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
24108 KB |
Output is correct |
6 |
Correct |
12 ms |
24156 KB |
Output is correct |
7 |
Correct |
12 ms |
24152 KB |
Output is correct |
8 |
Correct |
12 ms |
24156 KB |
Output is correct |
9 |
Correct |
12 ms |
24852 KB |
Output is correct |
10 |
Correct |
12 ms |
25340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
244 ms |
55100 KB |
Output is correct |
2 |
Correct |
583 ms |
94988 KB |
Output is correct |
3 |
Correct |
195 ms |
53904 KB |
Output is correct |
4 |
Correct |
759 ms |
84216 KB |
Output is correct |
5 |
Correct |
795 ms |
84048 KB |
Output is correct |
6 |
Correct |
823 ms |
82664 KB |
Output is correct |
7 |
Correct |
182 ms |
54460 KB |
Output is correct |
8 |
Correct |
1274 ms |
241880 KB |
Output is correct |
9 |
Correct |
1489 ms |
262144 KB |
Output is correct |
10 |
Correct |
495 ms |
81620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
13 ms |
24424 KB |
Output is correct |
3 |
Correct |
12 ms |
24140 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
24108 KB |
Output is correct |
6 |
Correct |
12 ms |
24156 KB |
Output is correct |
7 |
Correct |
12 ms |
24152 KB |
Output is correct |
8 |
Correct |
12 ms |
24156 KB |
Output is correct |
9 |
Correct |
12 ms |
24852 KB |
Output is correct |
10 |
Correct |
12 ms |
25340 KB |
Output is correct |
11 |
Correct |
20 ms |
25840 KB |
Output is correct |
12 |
Correct |
19 ms |
28212 KB |
Output is correct |
13 |
Correct |
18 ms |
27876 KB |
Output is correct |
14 |
Correct |
24 ms |
24212 KB |
Output is correct |
15 |
Correct |
29 ms |
24408 KB |
Output is correct |
16 |
Correct |
14 ms |
24408 KB |
Output is correct |
17 |
Correct |
13 ms |
24408 KB |
Output is correct |
18 |
Correct |
13 ms |
24668 KB |
Output is correct |
19 |
Correct |
14 ms |
24532 KB |
Output is correct |
20 |
Correct |
16 ms |
26196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
13 ms |
24424 KB |
Output is correct |
3 |
Correct |
12 ms |
24140 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
24108 KB |
Output is correct |
6 |
Correct |
12 ms |
24156 KB |
Output is correct |
7 |
Correct |
12 ms |
24152 KB |
Output is correct |
8 |
Correct |
12 ms |
24156 KB |
Output is correct |
9 |
Correct |
12 ms |
24852 KB |
Output is correct |
10 |
Correct |
12 ms |
25340 KB |
Output is correct |
11 |
Correct |
20 ms |
25840 KB |
Output is correct |
12 |
Correct |
19 ms |
28212 KB |
Output is correct |
13 |
Correct |
18 ms |
27876 KB |
Output is correct |
14 |
Correct |
24 ms |
24212 KB |
Output is correct |
15 |
Correct |
29 ms |
24408 KB |
Output is correct |
16 |
Correct |
14 ms |
24408 KB |
Output is correct |
17 |
Correct |
13 ms |
24408 KB |
Output is correct |
18 |
Correct |
13 ms |
24668 KB |
Output is correct |
19 |
Correct |
14 ms |
24532 KB |
Output is correct |
20 |
Correct |
16 ms |
26196 KB |
Output is correct |
21 |
Correct |
22 ms |
25944 KB |
Output is correct |
22 |
Correct |
31 ms |
27228 KB |
Output is correct |
23 |
Correct |
37 ms |
28220 KB |
Output is correct |
24 |
Execution timed out |
4046 ms |
27832 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
13 ms |
24424 KB |
Output is correct |
3 |
Correct |
12 ms |
24140 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
24108 KB |
Output is correct |
6 |
Correct |
12 ms |
24156 KB |
Output is correct |
7 |
Correct |
12 ms |
24152 KB |
Output is correct |
8 |
Correct |
12 ms |
24156 KB |
Output is correct |
9 |
Correct |
12 ms |
24852 KB |
Output is correct |
10 |
Correct |
12 ms |
25340 KB |
Output is correct |
11 |
Correct |
244 ms |
55100 KB |
Output is correct |
12 |
Correct |
583 ms |
94988 KB |
Output is correct |
13 |
Correct |
195 ms |
53904 KB |
Output is correct |
14 |
Correct |
759 ms |
84216 KB |
Output is correct |
15 |
Correct |
795 ms |
84048 KB |
Output is correct |
16 |
Correct |
823 ms |
82664 KB |
Output is correct |
17 |
Correct |
182 ms |
54460 KB |
Output is correct |
18 |
Correct |
1274 ms |
241880 KB |
Output is correct |
19 |
Correct |
1489 ms |
262144 KB |
Output is correct |
20 |
Correct |
495 ms |
81620 KB |
Output is correct |
21 |
Correct |
20 ms |
25840 KB |
Output is correct |
22 |
Correct |
19 ms |
28212 KB |
Output is correct |
23 |
Correct |
18 ms |
27876 KB |
Output is correct |
24 |
Correct |
24 ms |
24212 KB |
Output is correct |
25 |
Correct |
29 ms |
24408 KB |
Output is correct |
26 |
Correct |
14 ms |
24408 KB |
Output is correct |
27 |
Correct |
13 ms |
24408 KB |
Output is correct |
28 |
Correct |
13 ms |
24668 KB |
Output is correct |
29 |
Correct |
14 ms |
24532 KB |
Output is correct |
30 |
Correct |
16 ms |
26196 KB |
Output is correct |
31 |
Correct |
22 ms |
25944 KB |
Output is correct |
32 |
Correct |
31 ms |
27228 KB |
Output is correct |
33 |
Correct |
37 ms |
28220 KB |
Output is correct |
34 |
Execution timed out |
4046 ms |
27832 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |