#include <stdio.h>
int de=0,qu=1,com[1001]={1},max=0,am,co,in[101]={1},network[101][101]={};
int queue(int a){
if(a>0){
com[qu++]=a;
return 0;
}
if(de==qu)
return 0;
return com[de++];
}
int DFS(){
int a=queue(0);
if(a==0)
return 0;
for(int i = 1 ; i <= am ; i++){
if(network[a][i]==1&&in[i]!=1){
queue(i);
in[i]=1;
max++;
}
}
return 0;
}
int main(){
int a,b;
scanf("%d %d",&am,&co);
for(int i = 0;i<co;i++){
scanf("%d %d",&a,&b);
network[a][b]=1;
network[b][a]=1;
}
for(int i = 0 ; i < am ; i++)
DFS();
printf("%d",max-1);
return 0;
}
top of page

실제 작동 상태를 확인하려면 라이브 사이트로 이동하세요.
수정: 2023년 11월 14일
BFS 바이러스
BFS 바이러스
댓글 0개
좋아요
댓글(0)
bottom of page