#include <iostream>
using namespace std;
int w,x,y,z,n,f[1000005][4],count[1000005][4],st[1000005][4],en[1000005][4],deg[1000005],tree[4],circle[4],cnt,a[1000005],b[1000005],c;
void Init(int N_) {
n = N_;
for(int i=0;i<n;i++)
{
for(int j=0;j<=3;j++)
{
f[i][j]=i;
st[i][j]=i;
en[i][j]=i;
count[i][j]=1;
}
}
}
int find(int x,int a)
{
if(f[x][a]==x)return x;
else return f[x][a]=find(f[x][a],a);
}
int link(int x,int y,int a)
{
int fx=find(x,a),fy=find(y,a);
if(fx==fy)
{
if((st[fx][a]==x && en[fx][a]==y) || (st[fx][a]==y && en[fx][a]==x))
{
if(circle[a]!=0)circle[a]=-1;
else circle[a]=count[fx][a];
}
else tree[a]=1;
return 0;
}
if((st[fx][a]!=x && en[fx][a]!=x) || (st[fy][a]!=y && en[fy][a]!=y))
{
tree[a]=1;
return 0;
}
f[fx][a]=fy;
count[fy][a]+=count[fx][a];
int ns,ne;
if(st[fy][a]==y)ns=en[fy][a];
else ns=st[fy][a];
if(st[fx][a]==x)ne=en[fx][a];
else ne=st[fx][a];
st[fy][a]=ns;
en[fy][a]=ne;
return 0;
}
void Link(int A, int B) {
cnt++;
a[cnt]=A;
b[cnt]=B;
deg[A]++;
deg[B]++;
if((deg[A]==3 || deg[B]==3) && c==0)
{
c=1;
w,x=-1,y=-1,z=-1;
if(deg[A]==3)w=A;
else w=B;
for(int i=1;i<=cnt;i++)
{
if(a[i]==w)
{
if(x==-1)x=b[i];
else if(y==-1)y=b[i];
else z=b[i];
}
else if(b[i]==w)
{
if(x==-1)x=a[i];
else if(y==-1)y=a[i];
else z=a[i];
}
}
for(int j=0;j<=3;j++)
{
tree[j]=0;
circle[j]=0;
for(int i=0;i<n;i++)
{
f[i][j]=i;
count[i][j]=1;
st[i][j]=i;
en[i][j]=i;
}
for(int i=1;i<=cnt;i++)
{
if(j==0 && (a[i]==w || b[i]==w))continue;
if(j==1 && (a[i]==x || b[i]==x))continue;
if(j==2 && (a[i]==y || b[i]==y))continue;
if(j==3 && (a[i]==z || b[i]==z))continue;
link(a[i],b[i],j);
}
}
}
else if(c==1)
{
for(int i=0;i<=3;i++)
{
if(i==0 && (a[cnt]==w || b[cnt]==w))continue;
if(i==1 && (a[cnt]==x || b[cnt]==x))continue;
if(i==2 && (a[cnt]==y || b[cnt]==y))continue;
if(i==3 && (a[cnt]==z || b[cnt]==z))continue;
link(a[cnt],b[cnt],i);
}
}
else link(a[cnt],b[cnt],0);
return ;
}
int CountCritical() {
if(c==1)
{
int ans=0;
if(circle[0]==0 && tree[0]==0)ans++;
if(circle[1]==0 && tree[1]==0)ans++;
if(circle[2]==0 && tree[2]==0)ans++;
if(circle[3]==0 && tree[3]==0)ans++;
return ans;
}
if(circle[0]==0)return n;
if(circle[0]==-1)return 0;
return circle[0];
}
Compilation message
rings.cpp: In function 'void Link(int, int)':
rings.cpp:62:3: warning: left operand of comma operator has no effect [-Wunused-value]
62 | w,x=-1,y=-1,z=-1;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
768 KB |
Output is correct |
8 |
Correct |
2 ms |
768 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
3 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
336 ms |
39232 KB |
Output is correct |
2 |
Correct |
863 ms |
59896 KB |
Output is correct |
3 |
Correct |
800 ms |
79252 KB |
Output is correct |
4 |
Correct |
785 ms |
79736 KB |
Output is correct |
5 |
Correct |
794 ms |
79608 KB |
Output is correct |
6 |
Correct |
752 ms |
78968 KB |
Output is correct |
7 |
Correct |
755 ms |
78968 KB |
Output is correct |
8 |
Correct |
1346 ms |
74872 KB |
Output is correct |
9 |
Correct |
1617 ms |
79892 KB |
Output is correct |
10 |
Correct |
678 ms |
87544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
768 KB |
Output is correct |
8 |
Correct |
2 ms |
768 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
3 ms |
768 KB |
Output is correct |
11 |
Correct |
3 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
1408 KB |
Output is correct |
13 |
Correct |
5 ms |
1280 KB |
Output is correct |
14 |
Correct |
4 ms |
1152 KB |
Output is correct |
15 |
Correct |
4 ms |
1920 KB |
Output is correct |
16 |
Correct |
4 ms |
1280 KB |
Output is correct |
17 |
Correct |
5 ms |
1280 KB |
Output is correct |
18 |
Correct |
6 ms |
1920 KB |
Output is correct |
19 |
Correct |
4 ms |
1280 KB |
Output is correct |
20 |
Correct |
5 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
768 KB |
Output is correct |
8 |
Correct |
2 ms |
768 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
3 ms |
768 KB |
Output is correct |
11 |
Correct |
3 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
1408 KB |
Output is correct |
13 |
Correct |
5 ms |
1280 KB |
Output is correct |
14 |
Correct |
4 ms |
1152 KB |
Output is correct |
15 |
Correct |
4 ms |
1920 KB |
Output is correct |
16 |
Correct |
4 ms |
1280 KB |
Output is correct |
17 |
Correct |
5 ms |
1280 KB |
Output is correct |
18 |
Correct |
6 ms |
1920 KB |
Output is correct |
19 |
Correct |
4 ms |
1280 KB |
Output is correct |
20 |
Correct |
5 ms |
1280 KB |
Output is correct |
21 |
Correct |
20 ms |
4480 KB |
Output is correct |
22 |
Correct |
36 ms |
6904 KB |
Output is correct |
23 |
Correct |
44 ms |
8696 KB |
Output is correct |
24 |
Correct |
48 ms |
7928 KB |
Output is correct |
25 |
Correct |
18 ms |
7424 KB |
Output is correct |
26 |
Correct |
47 ms |
8184 KB |
Output is correct |
27 |
Correct |
51 ms |
7800 KB |
Output is correct |
28 |
Correct |
52 ms |
8056 KB |
Output is correct |
29 |
Correct |
42 ms |
8056 KB |
Output is correct |
30 |
Correct |
58 ms |
8824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
768 KB |
Output is correct |
8 |
Correct |
2 ms |
768 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
3 ms |
768 KB |
Output is correct |
11 |
Correct |
336 ms |
39232 KB |
Output is correct |
12 |
Correct |
863 ms |
59896 KB |
Output is correct |
13 |
Correct |
800 ms |
79252 KB |
Output is correct |
14 |
Correct |
785 ms |
79736 KB |
Output is correct |
15 |
Correct |
794 ms |
79608 KB |
Output is correct |
16 |
Correct |
752 ms |
78968 KB |
Output is correct |
17 |
Correct |
755 ms |
78968 KB |
Output is correct |
18 |
Correct |
1346 ms |
74872 KB |
Output is correct |
19 |
Correct |
1617 ms |
79892 KB |
Output is correct |
20 |
Correct |
678 ms |
87544 KB |
Output is correct |
21 |
Correct |
3 ms |
768 KB |
Output is correct |
22 |
Correct |
5 ms |
1408 KB |
Output is correct |
23 |
Correct |
5 ms |
1280 KB |
Output is correct |
24 |
Correct |
4 ms |
1152 KB |
Output is correct |
25 |
Correct |
4 ms |
1920 KB |
Output is correct |
26 |
Correct |
4 ms |
1280 KB |
Output is correct |
27 |
Correct |
5 ms |
1280 KB |
Output is correct |
28 |
Correct |
6 ms |
1920 KB |
Output is correct |
29 |
Correct |
4 ms |
1280 KB |
Output is correct |
30 |
Correct |
5 ms |
1280 KB |
Output is correct |
31 |
Correct |
20 ms |
4480 KB |
Output is correct |
32 |
Correct |
36 ms |
6904 KB |
Output is correct |
33 |
Correct |
44 ms |
8696 KB |
Output is correct |
34 |
Correct |
48 ms |
7928 KB |
Output is correct |
35 |
Correct |
18 ms |
7424 KB |
Output is correct |
36 |
Correct |
47 ms |
8184 KB |
Output is correct |
37 |
Correct |
51 ms |
7800 KB |
Output is correct |
38 |
Correct |
52 ms |
8056 KB |
Output is correct |
39 |
Correct |
42 ms |
8056 KB |
Output is correct |
40 |
Correct |
58 ms |
8824 KB |
Output is correct |
41 |
Correct |
217 ms |
40944 KB |
Output is correct |
42 |
Correct |
648 ms |
68856 KB |
Output is correct |
43 |
Correct |
299 ms |
67832 KB |
Output is correct |
44 |
Correct |
652 ms |
81276 KB |
Output is correct |
45 |
Correct |
595 ms |
80760 KB |
Output is correct |
46 |
Correct |
641 ms |
73720 KB |
Output is correct |
47 |
Correct |
712 ms |
75040 KB |
Output is correct |
48 |
Correct |
457 ms |
77432 KB |
Output is correct |
49 |
Correct |
575 ms |
85240 KB |
Output is correct |
50 |
Correct |
598 ms |
83832 KB |
Output is correct |
51 |
Correct |
293 ms |
59000 KB |
Output is correct |
52 |
Correct |
585 ms |
65912 KB |
Output is correct |
53 |
Correct |
444 ms |
76920 KB |
Output is correct |
54 |
Correct |
1068 ms |
72312 KB |
Output is correct |
55 |
Correct |
791 ms |
78136 KB |
Output is correct |