#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(x) (int)(x.size())
#define fi first
#define se second
const int MX = (int)(1e6+3);
typedef pair<int,int> ii;
int N,deg[6][MX],p[6][MX],s[6][MX],pot[4],k,len;
vector<int> vec[MX],G;
vector<ii> edges;
multiset<int,greater<int> > myset;
bool flag2,flag3,flag4,st,cycle,ok[5];
bitset<MX> vis;
void Init(int N_) {
N = N_;
for (int j = 0; j < 6; ++j)
for (int i = 0; i < N; ++i)
p[j][i]=i,s[j][i]=1;
}
int findSet(int idx,int i){
if(p[idx][i]==i)return i;
return p[idx][i]=findSet(idx,p[idx][i]);
}
bool Union(int idx,int a,int b){
a=findSet(idx,a),b=findSet(idx,b);
if(a==b)return 0;
if(s[idx][a]>s[idx][b])swap(a,b);
p[idx][a]=b,s[idx][b]+=s[idx][a];
return 1;
}
void dfs(int node,int par){
vis[node]=1;G.pb(node);
for(auto it:vec[node]){
if(len)return;
if(it!=par){
if(vis[it]){
len=sz(G);
return;
}
dfs(it,node);
}
}
G.pop_back();
}
void Link(int A, int B) {
if(st)return;
edges.pb({A,B});
vec[A].pb(B),vec[B].pb(A);
deg[0][A]++,deg[0][B]++;
if(!flag3 && !flag4){
bool h=Union(0,A,B);
if(!h && cycle){st=1;return;}
else if(!h){cycle=1;dfs(A,A);}
}
if(flag3){
for (int i = 0; i < 4; ++i){
if(A==pot[i] || B==pot[i])continue;
if(!Union(i+1,A,B))ok[i]=0;
deg[i+1][A]++,deg[i+1][B]++;
if(deg[i+1][A]>=3 || deg[i+1][B]>=3)ok[i]=0;
}
}
if(k!=A && k!=B && flag4){
if(!Union(5,A,B)){st=1;return;}
deg[5][A]++,deg[5][B]++;
if(deg[5][A]>=3 || deg[5][B]>=3){st=1;return;}
}
if(!flag4 && (deg[0][A]==3 || deg[0][B]==3) && (deg[0][A]<=3) && (deg[0][B]<=3) && !flag3){
flag3=1;
if(deg[0][A]!=3)swap(A,B);
vec[A].pb(A);
for (int i = 0; i < 4; ++i){
bool f=1;
pot[i]=vec[A][i];
for(auto it:edges){
if(it.fi!=vec[A][i] && it.se!=vec[A][i]){
if(!Union(i+1,it.fi,it.se)){f=0;break;}
deg[i+1][it.fi]++,deg[i+1][it.se]++;
if(deg[i+1][it.fi]>=3 || deg[i+1][it.se]>=3){f=0;break;}
}
}
ok[i]=f;
}
vec[A].pop_back();
}
else if(deg[0][A]>=4 || deg[0][B]>=4){
if(deg[0][A]>=4 && deg[0][B]>=4)st=1;
else{
if(deg[0][A]<4)swap(A,B);
if(A==k)return;
flag4=1,flag3=0,k=A;
bool f=1;
for(auto it:edges)
if(it.fi!=A && it.se!=A){
if(!Union(5,it.fi,it.se)){f=0;break;}
deg[5][it.fi]++,deg[5][it.se]++;
if(deg[5][it.fi]>=3 || deg[5][it.se]>=3){f=0;break;}
}
if(!f)st=1;
}
}
}
int CountCritical() {
if(st)return 0;
if(!flag3 && !flag4){
if(!cycle)return N;
else return len;
}
if(flag3)return ok[0]+ok[1]+ok[2]+ok[3];
if(flag4)return 1;
return -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
23936 KB |
Output is correct |
2 |
Correct |
23 ms |
24448 KB |
Output is correct |
3 |
Correct |
21 ms |
24576 KB |
Output is correct |
4 |
Correct |
17 ms |
24064 KB |
Output is correct |
5 |
Correct |
19 ms |
24324 KB |
Output is correct |
6 |
Correct |
20 ms |
24704 KB |
Output is correct |
7 |
Correct |
21 ms |
24320 KB |
Output is correct |
8 |
Correct |
23 ms |
24320 KB |
Output is correct |
9 |
Correct |
24 ms |
24576 KB |
Output is correct |
10 |
Correct |
24 ms |
24576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
559 ms |
72032 KB |
Output is correct |
2 |
Correct |
1228 ms |
112928 KB |
Output is correct |
3 |
Correct |
371 ms |
95892 KB |
Output is correct |
4 |
Correct |
1306 ms |
114684 KB |
Output is correct |
5 |
Correct |
1286 ms |
116048 KB |
Output is correct |
6 |
Correct |
1414 ms |
147748 KB |
Output is correct |
7 |
Correct |
372 ms |
96372 KB |
Output is correct |
8 |
Correct |
1913 ms |
123344 KB |
Output is correct |
9 |
Correct |
1972 ms |
130072 KB |
Output is correct |
10 |
Correct |
851 ms |
112724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
23936 KB |
Output is correct |
2 |
Correct |
23 ms |
24448 KB |
Output is correct |
3 |
Correct |
21 ms |
24576 KB |
Output is correct |
4 |
Correct |
17 ms |
24064 KB |
Output is correct |
5 |
Correct |
19 ms |
24324 KB |
Output is correct |
6 |
Correct |
20 ms |
24704 KB |
Output is correct |
7 |
Correct |
21 ms |
24320 KB |
Output is correct |
8 |
Correct |
23 ms |
24320 KB |
Output is correct |
9 |
Correct |
24 ms |
24576 KB |
Output is correct |
10 |
Correct |
24 ms |
24576 KB |
Output is correct |
11 |
Correct |
20 ms |
24576 KB |
Output is correct |
12 |
Correct |
23 ms |
25216 KB |
Output is correct |
13 |
Correct |
24 ms |
25216 KB |
Output is correct |
14 |
Correct |
21 ms |
24960 KB |
Output is correct |
15 |
Correct |
29 ms |
25472 KB |
Output is correct |
16 |
Correct |
27 ms |
24960 KB |
Output is correct |
17 |
Correct |
30 ms |
25216 KB |
Output is correct |
18 |
Correct |
22 ms |
25600 KB |
Output is correct |
19 |
Correct |
32 ms |
25080 KB |
Output is correct |
20 |
Correct |
24 ms |
25216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
23936 KB |
Output is correct |
2 |
Correct |
23 ms |
24448 KB |
Output is correct |
3 |
Correct |
21 ms |
24576 KB |
Output is correct |
4 |
Correct |
17 ms |
24064 KB |
Output is correct |
5 |
Correct |
19 ms |
24324 KB |
Output is correct |
6 |
Correct |
20 ms |
24704 KB |
Output is correct |
7 |
Correct |
21 ms |
24320 KB |
Output is correct |
8 |
Correct |
23 ms |
24320 KB |
Output is correct |
9 |
Correct |
24 ms |
24576 KB |
Output is correct |
10 |
Correct |
24 ms |
24576 KB |
Output is correct |
11 |
Correct |
20 ms |
24576 KB |
Output is correct |
12 |
Correct |
23 ms |
25216 KB |
Output is correct |
13 |
Correct |
24 ms |
25216 KB |
Output is correct |
14 |
Correct |
21 ms |
24960 KB |
Output is correct |
15 |
Correct |
29 ms |
25472 KB |
Output is correct |
16 |
Correct |
27 ms |
24960 KB |
Output is correct |
17 |
Correct |
30 ms |
25216 KB |
Output is correct |
18 |
Correct |
22 ms |
25600 KB |
Output is correct |
19 |
Correct |
32 ms |
25080 KB |
Output is correct |
20 |
Correct |
24 ms |
25216 KB |
Output is correct |
21 |
Correct |
42 ms |
28148 KB |
Output is correct |
22 |
Correct |
54 ms |
30448 KB |
Output is correct |
23 |
Correct |
73 ms |
32240 KB |
Output is correct |
24 |
Correct |
78 ms |
31856 KB |
Output is correct |
25 |
Correct |
40 ms |
30328 KB |
Output is correct |
26 |
Correct |
73 ms |
34032 KB |
Output is correct |
27 |
Correct |
71 ms |
32240 KB |
Output is correct |
28 |
Correct |
108 ms |
34156 KB |
Output is correct |
29 |
Correct |
51 ms |
32248 KB |
Output is correct |
30 |
Correct |
111 ms |
34544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
23936 KB |
Output is correct |
2 |
Correct |
23 ms |
24448 KB |
Output is correct |
3 |
Correct |
21 ms |
24576 KB |
Output is correct |
4 |
Correct |
17 ms |
24064 KB |
Output is correct |
5 |
Correct |
19 ms |
24324 KB |
Output is correct |
6 |
Correct |
20 ms |
24704 KB |
Output is correct |
7 |
Correct |
21 ms |
24320 KB |
Output is correct |
8 |
Correct |
23 ms |
24320 KB |
Output is correct |
9 |
Correct |
24 ms |
24576 KB |
Output is correct |
10 |
Correct |
24 ms |
24576 KB |
Output is correct |
11 |
Correct |
559 ms |
72032 KB |
Output is correct |
12 |
Correct |
1228 ms |
112928 KB |
Output is correct |
13 |
Correct |
371 ms |
95892 KB |
Output is correct |
14 |
Correct |
1306 ms |
114684 KB |
Output is correct |
15 |
Correct |
1286 ms |
116048 KB |
Output is correct |
16 |
Correct |
1414 ms |
147748 KB |
Output is correct |
17 |
Correct |
372 ms |
96372 KB |
Output is correct |
18 |
Correct |
1913 ms |
123344 KB |
Output is correct |
19 |
Correct |
1972 ms |
130072 KB |
Output is correct |
20 |
Correct |
851 ms |
112724 KB |
Output is correct |
21 |
Correct |
20 ms |
24576 KB |
Output is correct |
22 |
Correct |
23 ms |
25216 KB |
Output is correct |
23 |
Correct |
24 ms |
25216 KB |
Output is correct |
24 |
Correct |
21 ms |
24960 KB |
Output is correct |
25 |
Correct |
29 ms |
25472 KB |
Output is correct |
26 |
Correct |
27 ms |
24960 KB |
Output is correct |
27 |
Correct |
30 ms |
25216 KB |
Output is correct |
28 |
Correct |
22 ms |
25600 KB |
Output is correct |
29 |
Correct |
32 ms |
25080 KB |
Output is correct |
30 |
Correct |
24 ms |
25216 KB |
Output is correct |
31 |
Correct |
42 ms |
28148 KB |
Output is correct |
32 |
Correct |
54 ms |
30448 KB |
Output is correct |
33 |
Correct |
73 ms |
32240 KB |
Output is correct |
34 |
Correct |
78 ms |
31856 KB |
Output is correct |
35 |
Correct |
40 ms |
30328 KB |
Output is correct |
36 |
Correct |
73 ms |
34032 KB |
Output is correct |
37 |
Correct |
71 ms |
32240 KB |
Output is correct |
38 |
Correct |
108 ms |
34156 KB |
Output is correct |
39 |
Correct |
51 ms |
32248 KB |
Output is correct |
40 |
Correct |
111 ms |
34544 KB |
Output is correct |
41 |
Correct |
268 ms |
60260 KB |
Output is correct |
42 |
Correct |
836 ms |
99700 KB |
Output is correct |
43 |
Correct |
297 ms |
81264 KB |
Output is correct |
44 |
Correct |
357 ms |
90104 KB |
Output is correct |
45 |
Correct |
557 ms |
101084 KB |
Output is correct |
46 |
Correct |
827 ms |
98860 KB |
Output is correct |
47 |
Correct |
1215 ms |
122068 KB |
Output is correct |
48 |
Correct |
326 ms |
97656 KB |
Output is correct |
49 |
Correct |
810 ms |
106832 KB |
Output is correct |
50 |
Correct |
878 ms |
105560 KB |
Output is correct |
51 |
Correct |
346 ms |
85076 KB |
Output is correct |
52 |
Correct |
1008 ms |
102224 KB |
Output is correct |
53 |
Correct |
308 ms |
97908 KB |
Output is correct |
54 |
Correct |
1619 ms |
110004 KB |
Output is correct |
55 |
Correct |
1218 ms |
121288 KB |
Output is correct |