#include <iostream>
#include <vector>
using namespace std;
const int nmax=1000*1000+5;
int n;
pair<int,int> muchii[nmax];
vector<int> v[nmax];
int grad[nmax],special[nmax],valid[nmax];
int nodspecial[10],auxiliare[10];
int maimari,busit,mme,gradmx,speciale,nr,auxi;
struct dsu
{
int tt[nmax],rg[nmax];
void ini()
{
for(int i=0;i<n;i++)
tt[i]=i,rg[i]=1;
}
int finds(int x)
{
int y=x,aux=0;
while(x!=tt[x])
x=tt[x];
while(y!=x)
{
aux=tt[y];
tt[y]=x;
y=aux;
}
return x;
}
void unite(int A,int B)
{
if(A==B) return;
if(rg[A]>rg[B]) {tt[B]=A;rg[A]+=rg[B];}
else {tt[A]=B;rg[B]+=rg[A];}
}
};
struct linker
{
dsu dsu1;
int ciclu,wciclu,mxout,gr[nmax];
void uneste(int x,int y)
{
gr[x]++;gr[y]++;
if(gr[x]>mxout) mxout=gr[x];
if(gr[y]>mxout) mxout=gr[y];
if(dsu1.finds(x)==dsu1.finds(y))
ciclu+=1,wciclu=dsu1.rg[dsu1.finds(x)];
else dsu1.unite(dsu1.finds(x),dsu1.finds(y));
}
}li[7],aux[10];
void rezolva(int x)
{
if(grad[x]>=3||(valid[x])) return;
for(int i=0;i<v[x].size();i++)
if(grad[v[x][i]]==3)
{
valid[x]=1;
}
if(!valid[x]) return;
auxi++;
auxiliare[auxi]=x;
aux[auxi].dsu1.ini();
for(int i=1;i<=nr;i++)
if(muchii[i].first!=x&&muchii[i].second!=x)
aux[auxi].uneste(muchii[i].first,muchii[i].second);
}
void spec(int x)
{
if(grad[x]==4)
{
maimari++;
if(maimari>1)
{
busit=1;
return;
}
}
if(grad[x]>=gradmx&&grad[x]>=3)
{
if(grad[x]==gradmx)
{
speciale++;
}
else
{
speciale=1;
gradmx=grad[x];
}
nodspecial[speciale]=x;
if(special[x]!=speciale)
{
li[speciale].mxout=0;li[speciale].ciclu=0;li[speciale].wciclu=0;
li[speciale].dsu1.ini();
for(int i=0;i<n;i++)
li[speciale].gr[i]=0;
for(int i=1;i<=nr;i++)
if(muchii[i].first!=x&&muchii[i].second!=x)
li[speciale].uneste(muchii[i].first,muchii[i].second);
special[x]=speciale;
}
}
if(gradmx==3&&speciale>4)
{
busit=1;
}
if(gradmx==3&&speciale<3)
{
for(int i=0;i<v[x].size();i++)
rezolva(v[x][i]);
}
}
void Init(int N_) {
n = N_;
li[0].dsu1.ini();
}
void Link(int A, int B) {
if(busit) return;
grad[A]++;
grad[B]++;
v[A].push_back(B);
v[B].push_back(A);
spec(A);
spec(B);
muchii[++nr]={A,B};
for(int i=0;i<=speciale;i++)
if(i==0||(nodspecial[i]!=A&&nodspecial[i]!=B))
li[i].uneste(A,B);
if(gradmx==3&&speciale<=2)
for(int i=1;i<=auxi;i++)
if(auxiliare[i]!=A&&auxiliare[i]!=B)
aux[i].uneste(A,B);
}
int CountCritical() {
if(busit)
{
return 0;
}
if(gradmx<3)
{
if(li[0].ciclu>1)
{
busit=1;
return 0;
}
if(li[0].ciclu==1)
return li[0].wciclu;
return n;
}
int ret=0;
for(int i=1;i<=speciale;i++)
{
if((!li[i].ciclu)&&li[i].mxout<3)
ret++;
}
if(gradmx==3&&speciale<=2)
{
for(int i=1;i<=auxi;i++)
if((!aux[i].ciclu)&&aux[i].mxout<3&&(!special[auxiliare[i]]))
ret++;
}
if(ret==0) busit=1;
return ret;
}
Compilation message
rings.cpp: In function 'void rezolva(int)':
rings.cpp:56:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
rings.cpp: In function 'void spec(int)':
rings.cpp:111:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
24436 KB |
Output is correct |
3 |
Correct |
26 ms |
24684 KB |
Output is correct |
4 |
Correct |
22 ms |
24684 KB |
Output is correct |
5 |
Correct |
22 ms |
24684 KB |
Output is correct |
6 |
Correct |
24 ms |
24684 KB |
Output is correct |
7 |
Correct |
24 ms |
25180 KB |
Output is correct |
8 |
Correct |
28 ms |
25180 KB |
Output is correct |
9 |
Correct |
28 ms |
25180 KB |
Output is correct |
10 |
Correct |
27 ms |
25180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
532 ms |
52664 KB |
Output is correct |
2 |
Correct |
1351 ms |
105680 KB |
Output is correct |
3 |
Correct |
497 ms |
147040 KB |
Output is correct |
4 |
Correct |
1459 ms |
147040 KB |
Output is correct |
5 |
Correct |
1379 ms |
147040 KB |
Output is correct |
6 |
Correct |
1278 ms |
147040 KB |
Output is correct |
7 |
Correct |
447 ms |
169952 KB |
Output is correct |
8 |
Correct |
3173 ms |
169952 KB |
Output is correct |
9 |
Correct |
2682 ms |
169952 KB |
Output is correct |
10 |
Correct |
959 ms |
169952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
24436 KB |
Output is correct |
3 |
Correct |
26 ms |
24684 KB |
Output is correct |
4 |
Correct |
22 ms |
24684 KB |
Output is correct |
5 |
Correct |
22 ms |
24684 KB |
Output is correct |
6 |
Correct |
24 ms |
24684 KB |
Output is correct |
7 |
Correct |
24 ms |
25180 KB |
Output is correct |
8 |
Correct |
28 ms |
25180 KB |
Output is correct |
9 |
Correct |
28 ms |
25180 KB |
Output is correct |
10 |
Correct |
27 ms |
25180 KB |
Output is correct |
11 |
Correct |
26 ms |
169952 KB |
Output is correct |
12 |
Correct |
32 ms |
169952 KB |
Output is correct |
13 |
Correct |
30 ms |
169952 KB |
Output is correct |
14 |
Correct |
28 ms |
169952 KB |
Output is correct |
15 |
Correct |
28 ms |
169952 KB |
Output is correct |
16 |
Correct |
28 ms |
169952 KB |
Output is correct |
17 |
Correct |
30 ms |
169952 KB |
Output is correct |
18 |
Correct |
28 ms |
169952 KB |
Output is correct |
19 |
Correct |
28 ms |
169952 KB |
Output is correct |
20 |
Correct |
32 ms |
169952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
24436 KB |
Output is correct |
3 |
Correct |
26 ms |
24684 KB |
Output is correct |
4 |
Correct |
22 ms |
24684 KB |
Output is correct |
5 |
Correct |
22 ms |
24684 KB |
Output is correct |
6 |
Correct |
24 ms |
24684 KB |
Output is correct |
7 |
Correct |
24 ms |
25180 KB |
Output is correct |
8 |
Correct |
28 ms |
25180 KB |
Output is correct |
9 |
Correct |
28 ms |
25180 KB |
Output is correct |
10 |
Correct |
27 ms |
25180 KB |
Output is correct |
11 |
Correct |
26 ms |
169952 KB |
Output is correct |
12 |
Correct |
32 ms |
169952 KB |
Output is correct |
13 |
Correct |
30 ms |
169952 KB |
Output is correct |
14 |
Correct |
28 ms |
169952 KB |
Output is correct |
15 |
Correct |
28 ms |
169952 KB |
Output is correct |
16 |
Correct |
28 ms |
169952 KB |
Output is correct |
17 |
Correct |
30 ms |
169952 KB |
Output is correct |
18 |
Correct |
28 ms |
169952 KB |
Output is correct |
19 |
Correct |
28 ms |
169952 KB |
Output is correct |
20 |
Correct |
32 ms |
169952 KB |
Output is correct |
21 |
Correct |
48 ms |
169952 KB |
Output is correct |
22 |
Correct |
62 ms |
169952 KB |
Output is correct |
23 |
Correct |
75 ms |
169952 KB |
Output is correct |
24 |
Correct |
75 ms |
169952 KB |
Output is correct |
25 |
Correct |
41 ms |
169952 KB |
Output is correct |
26 |
Correct |
79 ms |
169952 KB |
Output is correct |
27 |
Correct |
77 ms |
169952 KB |
Output is correct |
28 |
Correct |
56 ms |
169952 KB |
Output is correct |
29 |
Correct |
58 ms |
169952 KB |
Output is correct |
30 |
Correct |
97 ms |
169952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
24436 KB |
Output is correct |
3 |
Correct |
26 ms |
24684 KB |
Output is correct |
4 |
Correct |
22 ms |
24684 KB |
Output is correct |
5 |
Correct |
22 ms |
24684 KB |
Output is correct |
6 |
Correct |
24 ms |
24684 KB |
Output is correct |
7 |
Correct |
24 ms |
25180 KB |
Output is correct |
8 |
Correct |
28 ms |
25180 KB |
Output is correct |
9 |
Correct |
28 ms |
25180 KB |
Output is correct |
10 |
Correct |
27 ms |
25180 KB |
Output is correct |
11 |
Correct |
532 ms |
52664 KB |
Output is correct |
12 |
Correct |
1351 ms |
105680 KB |
Output is correct |
13 |
Correct |
497 ms |
147040 KB |
Output is correct |
14 |
Correct |
1459 ms |
147040 KB |
Output is correct |
15 |
Correct |
1379 ms |
147040 KB |
Output is correct |
16 |
Correct |
1278 ms |
147040 KB |
Output is correct |
17 |
Correct |
447 ms |
169952 KB |
Output is correct |
18 |
Correct |
3173 ms |
169952 KB |
Output is correct |
19 |
Correct |
2682 ms |
169952 KB |
Output is correct |
20 |
Correct |
959 ms |
169952 KB |
Output is correct |
21 |
Correct |
26 ms |
169952 KB |
Output is correct |
22 |
Correct |
32 ms |
169952 KB |
Output is correct |
23 |
Correct |
30 ms |
169952 KB |
Output is correct |
24 |
Correct |
28 ms |
169952 KB |
Output is correct |
25 |
Correct |
28 ms |
169952 KB |
Output is correct |
26 |
Correct |
28 ms |
169952 KB |
Output is correct |
27 |
Correct |
30 ms |
169952 KB |
Output is correct |
28 |
Correct |
28 ms |
169952 KB |
Output is correct |
29 |
Correct |
28 ms |
169952 KB |
Output is correct |
30 |
Correct |
32 ms |
169952 KB |
Output is correct |
31 |
Correct |
48 ms |
169952 KB |
Output is correct |
32 |
Correct |
62 ms |
169952 KB |
Output is correct |
33 |
Correct |
75 ms |
169952 KB |
Output is correct |
34 |
Correct |
75 ms |
169952 KB |
Output is correct |
35 |
Correct |
41 ms |
169952 KB |
Output is correct |
36 |
Correct |
79 ms |
169952 KB |
Output is correct |
37 |
Correct |
77 ms |
169952 KB |
Output is correct |
38 |
Correct |
56 ms |
169952 KB |
Output is correct |
39 |
Correct |
58 ms |
169952 KB |
Output is correct |
40 |
Correct |
97 ms |
169952 KB |
Output is correct |
41 |
Correct |
278 ms |
169952 KB |
Output is correct |
42 |
Correct |
831 ms |
169952 KB |
Output is correct |
43 |
Correct |
337 ms |
169952 KB |
Output is correct |
44 |
Correct |
399 ms |
169952 KB |
Output is correct |
45 |
Correct |
723 ms |
169952 KB |
Output is correct |
46 |
Correct |
885 ms |
169952 KB |
Output is correct |
47 |
Correct |
1128 ms |
169952 KB |
Output is correct |
48 |
Correct |
373 ms |
169952 KB |
Output is correct |
49 |
Correct |
834 ms |
169952 KB |
Output is correct |
50 |
Correct |
903 ms |
169952 KB |
Output is correct |
51 |
Correct |
394 ms |
169952 KB |
Output is correct |
52 |
Correct |
351 ms |
169952 KB |
Output is correct |
53 |
Correct |
352 ms |
169952 KB |
Output is correct |
54 |
Correct |
3034 ms |
169952 KB |
Output is correct |
55 |
Correct |
1442 ms |
169952 KB |
Output is correct |