728x90
반응형
문제
제출한 답안
package BOJ;
import java.util.Scanner;
public class BOJ_11724 {
private static int[][] area;
private static boolean[] isVisited;
private static int N, M;
private static int count = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
area = new int[1001][1001];
isVisited = new boolean[1001];
for (int i = 0; i < M; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
area[u][v] = area[v][u] = 1;
}
sc.close();
for (int i = 1; i <= N; i++) {
if (!isVisited[i]){
dfs(i);
count++;
}
}
System.out.println(count);
}
private static void dfs(int vertex){
isVisited[vertex] = true;
for (int i = 1; i <= N; i++) {
if (area[vertex][i] == 1 && !isVisited[i]){
dfs(i);
}
}
}
}
728x90
반응형
'Algorithm' 카테고리의 다른 글
[BOJ 4963] 섬의 개수 ( with Java ) (0) | 2023.04.16 |
---|---|
[BOJ 10026] 적록색약 ( with Java ) (0) | 2023.04.16 |
[BOJ 1012] 유기농 배추 ( with Java ) (0) | 2023.04.16 |
[BOJ 2667] 단지번호붙이기 ( with Java ) (0) | 2023.04.16 |
[BOJ 2606] 바이러스 ( with Java ) (0) | 2023.04.16 |