BOJ 문제풀이

백준 2606 c++ 풀이

koreasunoo 2021. 7. 20. 22:47

알고리즘 분류: 그래프 이론, 그래프 탐색, 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);
 
 
}

'BOJ 문제풀이' 카테고리의 다른 글

백준 2178번 c++ 풀이  (0) 2021.07.20
백준 1987번 c++ 풀이  (0) 2021.07.20
백준 2667번 c++ 풀이  (0) 2021.07.20
백준 1260번 c++ 풀이  (0) 2021.07.20
백준 2981 c++ 풀이  (0) 2021.07.20