BOJ 문제풀이
백준 2606 c++ 풀이
koreasunoo
2021. 7. 20. 22:47
SMALL
알고리즘 분류: 그래프 이론, 그래프 탐색, dfs, bfs
코드:
#include <iostream>
#include <stdlib.h>
int map[101][101] = {0};
int visit[101] = {0};
int computer_num, ans = 0;
void dfs(int n){
ans++;
visit[n] = 1;
for (int i=1; i<=computer_num; i++){
if (map[n][i] && !visit[i])
dfs(i);
}
}
int main(){
int n;
int x, y;
cin>>computer_num>>n;
for (int i=0; i<n; i++){
cin>>x>>y;
map[x][y] = map[y][x] = 1;
}
dfs(1);
printf("%d\n", ans - 1);
}
LIST