#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
#define mp make_pair
#define pb push_back
const int N=1000050;
int n;
struct DSU
{
int p[N],c[N],deg[N];
int cyc,my,sz;
bool ok;
DSU(){}
void init(int x)
{
my=x;cyc=sz=0;ok=1;
for(int i=0;i<n;i++) p[i]=i,c[i]=1,deg[i]=0;
}
int Find(int x){ return x==p[x]?x:p[x]=Find(p[x]);}
void Union(int x, int y)
{
if(x==my || y==my) return;
deg[x]++;deg[y]++;
if(deg[x]>2 || deg[y]>2) ok=0;
x=Find(x);y=Find(y);
if(x==y){ cyc++,sz=c[x],ok=0;return;}
p[x]=y;
c[y]+=c[x];
}
} Base,Try[4];
vector<int> E[N];
int esz;
pair<int,int> edges[N];
int deg[N],sz;
bool mod;
void Init(int m){ n=m;Base.init(-1);}
void Push()
{
for(int i=0;i<esz;i++)
{
for(int j=0;j<sz;j++)
{
Try[j].Union(edges[i].first,edges[i].second);
}
}
}
void Link(int a, int b)
{
deg[a]++;
deg[b]++;
Base.Union(a,b);
E[a].pb(b);
E[b].pb(a);
edges[esz++]=mp(a,b);
if(mod)
{
for(int i=0;i<sz;i++) Try[i].Union(a,b);
return;
}
if(deg[a]>2)
{
sz=E[a].size()+1;
Try[0].init(a);
for(int i=0;i<E[a].size();i++) Try[i+1].init(E[a][i]);
Push();
mod=1;
return;
}
if(deg[b]>2)
{
sz=E[b].size()+1;
Try[0].init(b);
for(int i=0;i<E[b].size();i++) Try[i+1].init(E[b][i]);
Push();
mod=1;
return;
}
}
int CountCritical()
{
if(mod)
{
int ans=0;
for(int i=0;i<sz;i++) if(Try[i].ok) ans++;
return ans;
}
if(Base.cyc>1) return 0;
if(Base.cyc==1) return Base.sz;
return n;
}
Compilation message
rings.cpp: In function 'void Link(int, int)':
rings.cpp:65:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<E[a].size();i++) Try[i+1].init(E[a][i]);
~^~~~~~~~~~~~
rings.cpp:74:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<E[b].size();i++) Try[i+1].init(E[b][i]);
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
24456 KB |
Output is correct |
3 |
Correct |
24 ms |
24600 KB |
Output is correct |
4 |
Correct |
22 ms |
24600 KB |
Output is correct |
5 |
Correct |
23 ms |
24600 KB |
Output is correct |
6 |
Correct |
25 ms |
24600 KB |
Output is correct |
7 |
Correct |
23 ms |
24600 KB |
Output is correct |
8 |
Correct |
23 ms |
24600 KB |
Output is correct |
9 |
Correct |
25 ms |
24708 KB |
Output is correct |
10 |
Correct |
26 ms |
24708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
489 ms |
52616 KB |
Output is correct |
2 |
Correct |
2887 ms |
105600 KB |
Output is correct |
3 |
Correct |
3448 ms |
121468 KB |
Output is correct |
4 |
Correct |
1382 ms |
121468 KB |
Output is correct |
5 |
Correct |
1384 ms |
121468 KB |
Output is correct |
6 |
Correct |
1333 ms |
121468 KB |
Output is correct |
7 |
Correct |
3364 ms |
121468 KB |
Output is correct |
8 |
Correct |
3655 ms |
121468 KB |
Output is correct |
9 |
Correct |
3900 ms |
125872 KB |
Output is correct |
10 |
Correct |
969 ms |
125872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
24456 KB |
Output is correct |
3 |
Correct |
24 ms |
24600 KB |
Output is correct |
4 |
Correct |
22 ms |
24600 KB |
Output is correct |
5 |
Correct |
23 ms |
24600 KB |
Output is correct |
6 |
Correct |
25 ms |
24600 KB |
Output is correct |
7 |
Correct |
23 ms |
24600 KB |
Output is correct |
8 |
Correct |
23 ms |
24600 KB |
Output is correct |
9 |
Correct |
25 ms |
24708 KB |
Output is correct |
10 |
Correct |
26 ms |
24708 KB |
Output is correct |
11 |
Correct |
25 ms |
125872 KB |
Output is correct |
12 |
Correct |
30 ms |
125872 KB |
Output is correct |
13 |
Correct |
29 ms |
125872 KB |
Output is correct |
14 |
Correct |
28 ms |
125872 KB |
Output is correct |
15 |
Correct |
27 ms |
125872 KB |
Output is correct |
16 |
Correct |
28 ms |
125872 KB |
Output is correct |
17 |
Correct |
28 ms |
125872 KB |
Output is correct |
18 |
Correct |
31 ms |
125872 KB |
Output is correct |
19 |
Correct |
27 ms |
125872 KB |
Output is correct |
20 |
Correct |
29 ms |
125872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
24456 KB |
Output is correct |
3 |
Correct |
24 ms |
24600 KB |
Output is correct |
4 |
Correct |
22 ms |
24600 KB |
Output is correct |
5 |
Correct |
23 ms |
24600 KB |
Output is correct |
6 |
Correct |
25 ms |
24600 KB |
Output is correct |
7 |
Correct |
23 ms |
24600 KB |
Output is correct |
8 |
Correct |
23 ms |
24600 KB |
Output is correct |
9 |
Correct |
25 ms |
24708 KB |
Output is correct |
10 |
Correct |
26 ms |
24708 KB |
Output is correct |
11 |
Correct |
25 ms |
125872 KB |
Output is correct |
12 |
Correct |
30 ms |
125872 KB |
Output is correct |
13 |
Correct |
29 ms |
125872 KB |
Output is correct |
14 |
Correct |
28 ms |
125872 KB |
Output is correct |
15 |
Correct |
27 ms |
125872 KB |
Output is correct |
16 |
Correct |
28 ms |
125872 KB |
Output is correct |
17 |
Correct |
28 ms |
125872 KB |
Output is correct |
18 |
Correct |
31 ms |
125872 KB |
Output is correct |
19 |
Correct |
27 ms |
125872 KB |
Output is correct |
20 |
Correct |
29 ms |
125872 KB |
Output is correct |
21 |
Correct |
43 ms |
125872 KB |
Output is correct |
22 |
Correct |
89 ms |
125872 KB |
Output is correct |
23 |
Correct |
68 ms |
125872 KB |
Output is correct |
24 |
Correct |
109 ms |
125872 KB |
Output is correct |
25 |
Correct |
43 ms |
125872 KB |
Output is correct |
26 |
Correct |
85 ms |
125872 KB |
Output is correct |
27 |
Correct |
75 ms |
125872 KB |
Output is correct |
28 |
Correct |
104 ms |
125872 KB |
Output is correct |
29 |
Correct |
75 ms |
125872 KB |
Output is correct |
30 |
Correct |
91 ms |
125872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
24456 KB |
Output is correct |
3 |
Correct |
24 ms |
24600 KB |
Output is correct |
4 |
Correct |
22 ms |
24600 KB |
Output is correct |
5 |
Correct |
23 ms |
24600 KB |
Output is correct |
6 |
Correct |
25 ms |
24600 KB |
Output is correct |
7 |
Correct |
23 ms |
24600 KB |
Output is correct |
8 |
Correct |
23 ms |
24600 KB |
Output is correct |
9 |
Correct |
25 ms |
24708 KB |
Output is correct |
10 |
Correct |
26 ms |
24708 KB |
Output is correct |
11 |
Correct |
489 ms |
52616 KB |
Output is correct |
12 |
Correct |
2887 ms |
105600 KB |
Output is correct |
13 |
Correct |
3448 ms |
121468 KB |
Output is correct |
14 |
Correct |
1382 ms |
121468 KB |
Output is correct |
15 |
Correct |
1384 ms |
121468 KB |
Output is correct |
16 |
Correct |
1333 ms |
121468 KB |
Output is correct |
17 |
Correct |
3364 ms |
121468 KB |
Output is correct |
18 |
Correct |
3655 ms |
121468 KB |
Output is correct |
19 |
Correct |
3900 ms |
125872 KB |
Output is correct |
20 |
Correct |
969 ms |
125872 KB |
Output is correct |
21 |
Correct |
25 ms |
125872 KB |
Output is correct |
22 |
Correct |
30 ms |
125872 KB |
Output is correct |
23 |
Correct |
29 ms |
125872 KB |
Output is correct |
24 |
Correct |
28 ms |
125872 KB |
Output is correct |
25 |
Correct |
27 ms |
125872 KB |
Output is correct |
26 |
Correct |
28 ms |
125872 KB |
Output is correct |
27 |
Correct |
28 ms |
125872 KB |
Output is correct |
28 |
Correct |
31 ms |
125872 KB |
Output is correct |
29 |
Correct |
27 ms |
125872 KB |
Output is correct |
30 |
Correct |
29 ms |
125872 KB |
Output is correct |
31 |
Correct |
43 ms |
125872 KB |
Output is correct |
32 |
Correct |
89 ms |
125872 KB |
Output is correct |
33 |
Correct |
68 ms |
125872 KB |
Output is correct |
34 |
Correct |
109 ms |
125872 KB |
Output is correct |
35 |
Correct |
43 ms |
125872 KB |
Output is correct |
36 |
Correct |
85 ms |
125872 KB |
Output is correct |
37 |
Correct |
75 ms |
125872 KB |
Output is correct |
38 |
Correct |
104 ms |
125872 KB |
Output is correct |
39 |
Correct |
75 ms |
125872 KB |
Output is correct |
40 |
Correct |
91 ms |
125872 KB |
Output is correct |
41 |
Correct |
274 ms |
125872 KB |
Output is correct |
42 |
Correct |
1215 ms |
125872 KB |
Output is correct |
43 |
Correct |
440 ms |
125872 KB |
Output is correct |
44 |
Correct |
1409 ms |
125872 KB |
Output is correct |
45 |
Correct |
2105 ms |
125872 KB |
Output is correct |
46 |
Correct |
890 ms |
125872 KB |
Output is correct |
47 |
Correct |
1157 ms |
125872 KB |
Output is correct |
48 |
Correct |
1075 ms |
125872 KB |
Output is correct |
49 |
Correct |
847 ms |
125872 KB |
Output is correct |
50 |
Correct |
936 ms |
125872 KB |
Output is correct |
51 |
Correct |
603 ms |
125872 KB |
Output is correct |
52 |
Correct |
1238 ms |
125872 KB |
Output is correct |
53 |
Correct |
1158 ms |
125872 KB |
Output is correct |
54 |
Correct |
3344 ms |
125872 KB |
Output is correct |
55 |
Correct |
3305 ms |
125872 KB |
Output is correct |