기록하는 개발자

[백준][JAVA] 2606 : 바이러스 본문

Algorithm

[백준][JAVA] 2606 : 바이러스

밍맹030 2021. 6. 3. 16:28
728x90

import java.io.IOException;
import java.util.Scanner;

public class Main{
    static int[][] adjacent; //컴퓨터 연결상태
    static boolean[] checked; //확인 여부
    static int n,m,num=0;
    public static void main(String[] args) throws IOException {
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        m = s.nextInt(); 
        adjacent = new int[n+1][n+1]; 
        checked = new boolean[n+1]; 
        for(int i = 0; i < m; i++) {
            int x = s.nextInt();
            int y = s.nextInt();
            adjacent[x][y] = adjacent[y][x] = 1;
        }
        dfs(1); 
        System.out.println(num);
    }
    public static void dfs(int i) { 
        checked[i]=true;
        for (int j = 1; j <= n; j++) {
            if (adjacent[i][j] == 1 && checked[j] == false) {
                num++;
                dfs(j);
            }
        }
    }
}

- 풀이는 이전에 올린 1260번 DFS와 BFS 풀이법의 DFS와 동일하므로 아래 링크를 참조하면 된다.

https://mingmeng030.tistory.com/109

728x90