#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N,inde_t4=0,inde_t3=0;
vector<vector<int>> gra;
set<int> ciclos;
set<int> t3,t4;
ll res=-1;
vector<int> dsu;
vector<bool> visited;
bool cambio=false,xd=false;
int find(int a){
if(dsu[a]==a){
return a;
}
return dsu[a]=find(dsu[a]);
}
bool unir(int a, int b){
a=find(a);b=find(b);
if(a==b){
return false;
}
if(ciclos.find(a)==ciclos.end()){
dsu[a]=b;
}else{
dsu[b]=a;
}
return true;
}
void Init(int N_) {
N=N_;
dsu.resize(N);
for(int i=0;i<N;i++){
dsu[i]=i;
}
gra.resize(N);
res=N;
}
void Link(int A, int B) {
if(!unir(A,B)){
ciclos.insert(find(A));
cambio=true;
}
gra[A].push_back(B);
if(gra[A].size()>=3){
t3.insert(A);
inde_t3=A;
cambio=true;
}
if(gra[A].size()>=4){
inde_t4=A;
t4.insert(A);
cambio=true;
}
gra[B].push_back(A);
if(gra[B].size()>=3){
t3.insert(B);
inde_t3=B;
cambio=true;
}
if(gra[B].size()>=4){
inde_t4=B;
t4.insert(B);
cambio=true;
}
}
void dfs(int now,int nooo,int ante){
int conta=0;
if(xd){
return;
}
if(visited[now]){
xd=true;
return;
}
visited[now]=true;
for(auto u:gra[now]){
if(u==nooo || u==ante){
continue;
}
dfs(u,nooo,now);
conta++;
if(conta>1){
xd=true;
}
if(xd){
return;
}
}
}
int CountCritical(){
if(/*!cambio || */res==0){
// no paso nada
}else if(t4.size()>=2){
res=0;
}/*else if(t3>=5){
res=0;
}*/else if(ciclos.size()>=2){
res=0;
}else if(t4.size()==1){
// si quito inde_t4 es un chain?
xd=false;
res=1;
visited.assign(N,false);
for(int i=0;i<N;i++){
if(visited[i] || i==inde_t4){
continue;
}
if(gra[i].size()==1){
dfs(i,inde_t4,-1);
}else if(gra[i].size()==2){
if(gra[i][0]==inde_t4 || gra[i][1]==inde_t4){
dfs(i,inde_t4,-1);
}
}
if(xd){
res=0;
break;
}
}
if(!xd){
for(int i=0;i<N;i++){
if(visited[i] || i==inde_t4 || dsu[i]==i){
continue;
}else{
xd=true;
res--;
break;
}
}
}
}else if(t3.size()>=1){
// si quito el inde_3 o sus vecinos sera un chain?
xd=false;
visited.assign(N,false);
res=4;
for(int i=0;i<N;i++){
if(visited[i] || i==inde_t3){
continue;
}
if(gra[i].size()==1){
dfs(i,inde_t3,-1);
}else if(gra[i].size()==2){
if(gra[i][0]==inde_t3 || gra[i][1]==inde_t3){
dfs(i,inde_t3,-1);
}
}
if(xd){
res--;
break;
}
}
if(!xd){
for(int i=0;i<N;i++){
if(visited[i] || i==inde_t3 || dsu[i]==i){
continue;
}else{
xd=true;
res--;
break;
}
}
}
for(auto uu:gra[inde_t3]){
xd=false;
visited.assign(N,false);
for(int i=0;i<N;i++){
if(visited[i] || i==uu){
continue;
}
if(gra[i].size()==1){
dfs(i,uu,-1);
}else if(gra[i].size()==2){
if(gra[i][0]==uu || gra[i][1]==uu){
dfs(i,uu,-1);
}
}
if(xd){
res--;
break;
}
}
if(!xd){
for(int i=0;i<N;i++){
if(visited[i] || i==uu || dsu[i]==i){
continue;
}else{
xd=true;
res--;
break;
}
}
}
}
}else if(ciclos.size()==1){
//cuantos elementos pertenecen a este ciclo?
int com=*ciclos.begin(),cont=0;
for(int i=0;i<N;i++){
if(find(i)==com){
cont++;
}
}
res=cont;
}
cambio=false;
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
848 KB |
Output is correct |
10 |
Correct |
3 ms |
848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
227 ms |
30788 KB |
Output is correct |
2 |
Correct |
491 ms |
47700 KB |
Output is correct |
3 |
Correct |
870 ms |
86968 KB |
Output is correct |
4 |
Correct |
498 ms |
72264 KB |
Output is correct |
5 |
Correct |
476 ms |
72376 KB |
Output is correct |
6 |
Correct |
435 ms |
70728 KB |
Output is correct |
7 |
Correct |
741 ms |
84296 KB |
Output is correct |
8 |
Correct |
955 ms |
68400 KB |
Output is correct |
9 |
Correct |
724 ms |
73596 KB |
Output is correct |
10 |
Correct |
366 ms |
70196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
848 KB |
Output is correct |
10 |
Correct |
3 ms |
848 KB |
Output is correct |
11 |
Correct |
9 ms |
760 KB |
Output is correct |
12 |
Correct |
8 ms |
1104 KB |
Output is correct |
13 |
Correct |
29 ms |
1148 KB |
Output is correct |
14 |
Correct |
13 ms |
848 KB |
Output is correct |
15 |
Correct |
17 ms |
1528 KB |
Output is correct |
16 |
Correct |
5 ms |
1104 KB |
Output is correct |
17 |
Correct |
4 ms |
1104 KB |
Output is correct |
18 |
Correct |
8 ms |
1616 KB |
Output is correct |
19 |
Correct |
4 ms |
1104 KB |
Output is correct |
20 |
Correct |
29 ms |
1180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
848 KB |
Output is correct |
10 |
Correct |
3 ms |
848 KB |
Output is correct |
11 |
Correct |
9 ms |
760 KB |
Output is correct |
12 |
Correct |
8 ms |
1104 KB |
Output is correct |
13 |
Correct |
29 ms |
1148 KB |
Output is correct |
14 |
Correct |
13 ms |
848 KB |
Output is correct |
15 |
Correct |
17 ms |
1528 KB |
Output is correct |
16 |
Correct |
5 ms |
1104 KB |
Output is correct |
17 |
Correct |
4 ms |
1104 KB |
Output is correct |
18 |
Correct |
8 ms |
1616 KB |
Output is correct |
19 |
Correct |
4 ms |
1104 KB |
Output is correct |
20 |
Correct |
29 ms |
1180 KB |
Output is correct |
21 |
Correct |
14 ms |
3080 KB |
Output is correct |
22 |
Correct |
19 ms |
4688 KB |
Output is correct |
23 |
Correct |
22 ms |
5968 KB |
Output is correct |
24 |
Execution timed out |
4073 ms |
3676 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
848 KB |
Output is correct |
10 |
Correct |
3 ms |
848 KB |
Output is correct |
11 |
Correct |
227 ms |
30788 KB |
Output is correct |
12 |
Correct |
491 ms |
47700 KB |
Output is correct |
13 |
Correct |
870 ms |
86968 KB |
Output is correct |
14 |
Correct |
498 ms |
72264 KB |
Output is correct |
15 |
Correct |
476 ms |
72376 KB |
Output is correct |
16 |
Correct |
435 ms |
70728 KB |
Output is correct |
17 |
Correct |
741 ms |
84296 KB |
Output is correct |
18 |
Correct |
955 ms |
68400 KB |
Output is correct |
19 |
Correct |
724 ms |
73596 KB |
Output is correct |
20 |
Correct |
366 ms |
70196 KB |
Output is correct |
21 |
Correct |
9 ms |
760 KB |
Output is correct |
22 |
Correct |
8 ms |
1104 KB |
Output is correct |
23 |
Correct |
29 ms |
1148 KB |
Output is correct |
24 |
Correct |
13 ms |
848 KB |
Output is correct |
25 |
Correct |
17 ms |
1528 KB |
Output is correct |
26 |
Correct |
5 ms |
1104 KB |
Output is correct |
27 |
Correct |
4 ms |
1104 KB |
Output is correct |
28 |
Correct |
8 ms |
1616 KB |
Output is correct |
29 |
Correct |
4 ms |
1104 KB |
Output is correct |
30 |
Correct |
29 ms |
1180 KB |
Output is correct |
31 |
Correct |
14 ms |
3080 KB |
Output is correct |
32 |
Correct |
19 ms |
4688 KB |
Output is correct |
33 |
Correct |
22 ms |
5968 KB |
Output is correct |
34 |
Execution timed out |
4073 ms |
3676 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |