#include <bits/stdc++.h>
using namespace std;
#define PB push_back
int n,par[3][1000005],v1=-1,v2=-1,ins,rps,ino[1000005],rpo[1000005],in[1000005],ts,ans,nx[1000005],vis[1000005],fr=-1,tr[1000005],trs;
bool zr,rpt,rpt2,ok1,ok2;
vector<int>g[1000005];
int find(int q,int x){
if(par[q][x]==x)return x;
return par[q][x]=find(q,par[q][x]);
}
bool same(int q,int x,int y){
return find(q,x)==find(q,y);
}
int unit(int q,int x,int y){
par[q][find(q,y)]=find(q,x);
}
void Init(int N){
n=N;
for(int i=0;i<n;i++)par[0][i]=i,par[1][i]=i,par[2][i]=i,nx[i]=-1;
ans=n;
}
void check(int v){
if(ino[v]==ins&&rpo[v]==rps)ans++;
}
void up(int v){
in[v]++;
if(in[v]==3){
trs++;
tr[v]++;
for(int i=0;i<g[v].size();i++)tr[g[v][i]]++;
ans=0;
ins++;
if(fr!=-1){
if(tr[fr]==trs){
ino[fr]=ins;
check(fr);
}
}else{
if(tr[v]==trs)ino[v]=ins;
check(v);
for(int i=0;i<g[v].size();i++){
int u=g[v][i];
if(tr[u]==trs)ino[u]=ins;
check(u);
}
}
}else if(in[v]==4){
if(fr==-1){
fr=v;
if(tr[v]==trs){
ins++;
ino[v]=ins;
ans=0;
check(v);
}else zr=true;
}else zr=true;
}
}
void dfs1(int v,int p){
nx[v]=p;
for(int i=0;i<g[v].size();i++){
if(g[v][i]!=p)dfs1(g[v][i],v);
}
}
void Link(int a,int b){
if(zr)return;
if(rpt2){
ans=0;
rps++;
if(same(1,a,b))ok1=false;
if(same(2,a,b))ok2=false;
if(v1!=a&&v1!=b)unit(1,a,b);
if(v2!=a&&v2!=b)unit(2,a,b);
if(ok1)rpo[v1]=rps;
if(ok2)rpo[v2]=rps;
check(v1);
if(v2!=-1)check(v2);
}else if(!same(0,a,b)){
unit(0,a,b);
if(nx[b]==-1)swap(a,b);
if(nx[a]==-1&&nx[b]!=-1)dfs1(a,b);
}else{
if(!rpt){
rpt=true;
dfs1(a,a);
ans=0;
rps++;
int v=b;
while(1){
vis[v]=rps;
rpo[v]=rps;
check(v);
if(v==a)break;
v=nx[v];
}
}else{
rpt2=true;
if(nx[a]==-1||nx[b]==-1)zr=true;
else{
ans=0;
rps++;
int v=a,u=b;
while(vis[v]==0){
vis[v]=rps;
v=nx[v];
}
while(vis[u]==0){
vis[u]=rps;
u=nx[u];
}
if(vis[u]==rps)zr=true;
if(rpo[v]==rps-1)rpo[v]=rps,ok1=true;
check(v);
v1=v;
if(v!=u){
v2=u;
if(rpo[u]==rps-1)rpo[u]=rps,ok2=true;
check(u);
}
if(v1!=-1){
for(int i=0;i<n;i++){
for(int j=0;j<g[i].size();j++){
if(i!=v1&&g[i][j]!=v1)unit(1,i,g[i][j]);
}
}
}
if(v2!=-1){
for(int i=0;i<n;i++){
for(int j=0;j<g[i].size();j++){
if(i!=v2&&g[i][j]!=v2)unit(2,i,g[i][j]);
}
}
}
}
}
}
g[a].PB(b);
g[b].PB(a);
up(a);
up(b);
}
int CountCritical() {
if(zr)return 0;
return ans;
}
Compilation message
rings.cpp: In function 'int unit(int, int, int)':
rings.cpp:16:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
rings.cpp: In function 'void up(int)':
rings.cpp:30:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++)tr[g[v][i]]++;
~^~~~~~~~~~~~
rings.cpp:41:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++){
~^~~~~~~~~~~~
rings.cpp: In function 'void dfs1(int, int)':
rings.cpp:61:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++){
~^~~~~~~~~~~~
rings.cpp: In function 'void Link(int, int)':
rings.cpp:122:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<g[i].size();j++){
~^~~~~~~~~~~~
rings.cpp:129:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<g[i].size();j++){
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
24056 KB |
Output is correct |
3 |
Correct |
25 ms |
24056 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
24 ms |
24184 KB |
Output is correct |
6 |
Correct |
25 ms |
24312 KB |
Output is correct |
7 |
Correct |
23 ms |
24056 KB |
Output is correct |
8 |
Correct |
24 ms |
24184 KB |
Output is correct |
9 |
Correct |
25 ms |
24184 KB |
Output is correct |
10 |
Correct |
26 ms |
24184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
510 ms |
50364 KB |
Output is correct |
2 |
Correct |
1018 ms |
64632 KB |
Output is correct |
3 |
Correct |
326 ms |
44380 KB |
Output is correct |
4 |
Correct |
1309 ms |
77816 KB |
Output is correct |
5 |
Correct |
1312 ms |
83704 KB |
Output is correct |
6 |
Correct |
1451 ms |
104176 KB |
Output is correct |
7 |
Correct |
315 ms |
45304 KB |
Output is correct |
8 |
Correct |
1206 ms |
71588 KB |
Output is correct |
9 |
Correct |
1256 ms |
74744 KB |
Output is correct |
10 |
Correct |
880 ms |
76592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
24056 KB |
Output is correct |
3 |
Correct |
25 ms |
24056 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
24 ms |
24184 KB |
Output is correct |
6 |
Correct |
25 ms |
24312 KB |
Output is correct |
7 |
Correct |
23 ms |
24056 KB |
Output is correct |
8 |
Correct |
24 ms |
24184 KB |
Output is correct |
9 |
Correct |
25 ms |
24184 KB |
Output is correct |
10 |
Correct |
26 ms |
24184 KB |
Output is correct |
11 |
Correct |
25 ms |
24184 KB |
Output is correct |
12 |
Correct |
27 ms |
24568 KB |
Output is correct |
13 |
Correct |
28 ms |
24552 KB |
Output is correct |
14 |
Correct |
26 ms |
24312 KB |
Output is correct |
15 |
Correct |
26 ms |
24568 KB |
Output is correct |
16 |
Correct |
29 ms |
24696 KB |
Output is correct |
17 |
Correct |
25 ms |
24312 KB |
Output is correct |
18 |
Correct |
26 ms |
24568 KB |
Output is correct |
19 |
Correct |
27 ms |
24568 KB |
Output is correct |
20 |
Correct |
28 ms |
24568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
24056 KB |
Output is correct |
3 |
Correct |
25 ms |
24056 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
24 ms |
24184 KB |
Output is correct |
6 |
Correct |
25 ms |
24312 KB |
Output is correct |
7 |
Correct |
23 ms |
24056 KB |
Output is correct |
8 |
Correct |
24 ms |
24184 KB |
Output is correct |
9 |
Correct |
25 ms |
24184 KB |
Output is correct |
10 |
Correct |
26 ms |
24184 KB |
Output is correct |
11 |
Correct |
25 ms |
24184 KB |
Output is correct |
12 |
Correct |
27 ms |
24568 KB |
Output is correct |
13 |
Correct |
28 ms |
24552 KB |
Output is correct |
14 |
Correct |
26 ms |
24312 KB |
Output is correct |
15 |
Correct |
26 ms |
24568 KB |
Output is correct |
16 |
Correct |
29 ms |
24696 KB |
Output is correct |
17 |
Correct |
25 ms |
24312 KB |
Output is correct |
18 |
Correct |
26 ms |
24568 KB |
Output is correct |
19 |
Correct |
27 ms |
24568 KB |
Output is correct |
20 |
Correct |
28 ms |
24568 KB |
Output is correct |
21 |
Correct |
44 ms |
26360 KB |
Output is correct |
22 |
Correct |
59 ms |
27640 KB |
Output is correct |
23 |
Correct |
70 ms |
28664 KB |
Output is correct |
24 |
Correct |
77 ms |
28408 KB |
Output is correct |
25 |
Correct |
41 ms |
26744 KB |
Output is correct |
26 |
Correct |
75 ms |
29560 KB |
Output is correct |
27 |
Correct |
75 ms |
29048 KB |
Output is correct |
28 |
Correct |
50 ms |
27000 KB |
Output is correct |
29 |
Correct |
50 ms |
27384 KB |
Output is correct |
30 |
Correct |
97 ms |
31352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
24056 KB |
Output is correct |
3 |
Correct |
25 ms |
24056 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
24 ms |
24184 KB |
Output is correct |
6 |
Correct |
25 ms |
24312 KB |
Output is correct |
7 |
Correct |
23 ms |
24056 KB |
Output is correct |
8 |
Correct |
24 ms |
24184 KB |
Output is correct |
9 |
Correct |
25 ms |
24184 KB |
Output is correct |
10 |
Correct |
26 ms |
24184 KB |
Output is correct |
11 |
Correct |
510 ms |
50364 KB |
Output is correct |
12 |
Correct |
1018 ms |
64632 KB |
Output is correct |
13 |
Correct |
326 ms |
44380 KB |
Output is correct |
14 |
Correct |
1309 ms |
77816 KB |
Output is correct |
15 |
Correct |
1312 ms |
83704 KB |
Output is correct |
16 |
Correct |
1451 ms |
104176 KB |
Output is correct |
17 |
Correct |
315 ms |
45304 KB |
Output is correct |
18 |
Correct |
1206 ms |
71588 KB |
Output is correct |
19 |
Correct |
1256 ms |
74744 KB |
Output is correct |
20 |
Correct |
880 ms |
76592 KB |
Output is correct |
21 |
Correct |
25 ms |
24184 KB |
Output is correct |
22 |
Correct |
27 ms |
24568 KB |
Output is correct |
23 |
Correct |
28 ms |
24552 KB |
Output is correct |
24 |
Correct |
26 ms |
24312 KB |
Output is correct |
25 |
Correct |
26 ms |
24568 KB |
Output is correct |
26 |
Correct |
29 ms |
24696 KB |
Output is correct |
27 |
Correct |
25 ms |
24312 KB |
Output is correct |
28 |
Correct |
26 ms |
24568 KB |
Output is correct |
29 |
Correct |
27 ms |
24568 KB |
Output is correct |
30 |
Correct |
28 ms |
24568 KB |
Output is correct |
31 |
Correct |
44 ms |
26360 KB |
Output is correct |
32 |
Correct |
59 ms |
27640 KB |
Output is correct |
33 |
Correct |
70 ms |
28664 KB |
Output is correct |
34 |
Correct |
77 ms |
28408 KB |
Output is correct |
35 |
Correct |
41 ms |
26744 KB |
Output is correct |
36 |
Correct |
75 ms |
29560 KB |
Output is correct |
37 |
Correct |
75 ms |
29048 KB |
Output is correct |
38 |
Correct |
50 ms |
27000 KB |
Output is correct |
39 |
Correct |
50 ms |
27384 KB |
Output is correct |
40 |
Correct |
97 ms |
31352 KB |
Output is correct |
41 |
Correct |
274 ms |
46008 KB |
Output is correct |
42 |
Correct |
547 ms |
63324 KB |
Output is correct |
43 |
Correct |
293 ms |
52768 KB |
Output is correct |
44 |
Correct |
308 ms |
57712 KB |
Output is correct |
45 |
Correct |
523 ms |
69596 KB |
Output is correct |
46 |
Correct |
831 ms |
78456 KB |
Output is correct |
47 |
Correct |
1278 ms |
99224 KB |
Output is correct |
48 |
Correct |
280 ms |
52604 KB |
Output is correct |
49 |
Correct |
828 ms |
79468 KB |
Output is correct |
50 |
Correct |
877 ms |
79060 KB |
Output is correct |
51 |
Correct |
338 ms |
51380 KB |
Output is correct |
52 |
Correct |
290 ms |
51068 KB |
Output is correct |
53 |
Correct |
263 ms |
52728 KB |
Output is correct |
54 |
Correct |
1040 ms |
76920 KB |
Output is correct |
55 |
Correct |
1158 ms |
81276 KB |
Output is correct |