#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];
vector<pair<int,int> > edges;
int deg[N],sz;
bool mod;
void Init(int m){ n=m;Base.init(-1);}
void Push()
{
for(int i=0;i<edges.size();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.pb(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 Push()':
rings.cpp:39:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<edges.size();i++)
~^~~~~~~~~~~~~
rings.cpp: In function 'void Link(int, int)':
rings.cpp:64: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:73: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 |
24436 KB |
Output is correct |
3 |
Correct |
24 ms |
24472 KB |
Output is correct |
4 |
Correct |
24 ms |
24472 KB |
Output is correct |
5 |
Correct |
23 ms |
24472 KB |
Output is correct |
6 |
Correct |
26 ms |
24480 KB |
Output is correct |
7 |
Correct |
22 ms |
24528 KB |
Output is correct |
8 |
Correct |
23 ms |
24528 KB |
Output is correct |
9 |
Correct |
24 ms |
24728 KB |
Output is correct |
10 |
Correct |
25 ms |
24752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
547 ms |
52676 KB |
Output is correct |
2 |
Correct |
3433 ms |
105724 KB |
Output is correct |
3 |
Execution timed out |
4011 ms |
121440 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
24436 KB |
Output is correct |
3 |
Correct |
24 ms |
24472 KB |
Output is correct |
4 |
Correct |
24 ms |
24472 KB |
Output is correct |
5 |
Correct |
23 ms |
24472 KB |
Output is correct |
6 |
Correct |
26 ms |
24480 KB |
Output is correct |
7 |
Correct |
22 ms |
24528 KB |
Output is correct |
8 |
Correct |
23 ms |
24528 KB |
Output is correct |
9 |
Correct |
24 ms |
24728 KB |
Output is correct |
10 |
Correct |
25 ms |
24752 KB |
Output is correct |
11 |
Correct |
25 ms |
121440 KB |
Output is correct |
12 |
Correct |
28 ms |
121440 KB |
Output is correct |
13 |
Correct |
29 ms |
121440 KB |
Output is correct |
14 |
Correct |
28 ms |
121440 KB |
Output is correct |
15 |
Correct |
26 ms |
121440 KB |
Output is correct |
16 |
Correct |
26 ms |
121440 KB |
Output is correct |
17 |
Correct |
29 ms |
121440 KB |
Output is correct |
18 |
Correct |
29 ms |
121440 KB |
Output is correct |
19 |
Correct |
24 ms |
121440 KB |
Output is correct |
20 |
Correct |
29 ms |
121440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
24436 KB |
Output is correct |
3 |
Correct |
24 ms |
24472 KB |
Output is correct |
4 |
Correct |
24 ms |
24472 KB |
Output is correct |
5 |
Correct |
23 ms |
24472 KB |
Output is correct |
6 |
Correct |
26 ms |
24480 KB |
Output is correct |
7 |
Correct |
22 ms |
24528 KB |
Output is correct |
8 |
Correct |
23 ms |
24528 KB |
Output is correct |
9 |
Correct |
24 ms |
24728 KB |
Output is correct |
10 |
Correct |
25 ms |
24752 KB |
Output is correct |
11 |
Correct |
25 ms |
121440 KB |
Output is correct |
12 |
Correct |
28 ms |
121440 KB |
Output is correct |
13 |
Correct |
29 ms |
121440 KB |
Output is correct |
14 |
Correct |
28 ms |
121440 KB |
Output is correct |
15 |
Correct |
26 ms |
121440 KB |
Output is correct |
16 |
Correct |
26 ms |
121440 KB |
Output is correct |
17 |
Correct |
29 ms |
121440 KB |
Output is correct |
18 |
Correct |
29 ms |
121440 KB |
Output is correct |
19 |
Correct |
24 ms |
121440 KB |
Output is correct |
20 |
Correct |
29 ms |
121440 KB |
Output is correct |
21 |
Correct |
42 ms |
121440 KB |
Output is correct |
22 |
Correct |
56 ms |
121440 KB |
Output is correct |
23 |
Correct |
63 ms |
121440 KB |
Output is correct |
24 |
Correct |
91 ms |
121440 KB |
Output is correct |
25 |
Correct |
42 ms |
121440 KB |
Output is correct |
26 |
Correct |
105 ms |
121440 KB |
Output is correct |
27 |
Correct |
80 ms |
121440 KB |
Output is correct |
28 |
Correct |
114 ms |
121440 KB |
Output is correct |
29 |
Correct |
84 ms |
121440 KB |
Output is correct |
30 |
Correct |
96 ms |
121440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
24436 KB |
Output is correct |
3 |
Correct |
24 ms |
24472 KB |
Output is correct |
4 |
Correct |
24 ms |
24472 KB |
Output is correct |
5 |
Correct |
23 ms |
24472 KB |
Output is correct |
6 |
Correct |
26 ms |
24480 KB |
Output is correct |
7 |
Correct |
22 ms |
24528 KB |
Output is correct |
8 |
Correct |
23 ms |
24528 KB |
Output is correct |
9 |
Correct |
24 ms |
24728 KB |
Output is correct |
10 |
Correct |
25 ms |
24752 KB |
Output is correct |
11 |
Correct |
547 ms |
52676 KB |
Output is correct |
12 |
Correct |
3433 ms |
105724 KB |
Output is correct |
13 |
Execution timed out |
4011 ms |
121440 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |