import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
static ArrayList<Integer>[] al;
static boolean[] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
al = new ArrayList[N + 1];
visited = new boolean[N + 1];
for (int i = 1; i <= N; i++)
al[i] = new ArrayList<>();
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
al[start].add(end);
al[end].add(start);
}
int count = 0;
for (int i = 1; i <= N; i++) {
if (!visited[i]) {
count++;
dfs(i);
}
}
System.out.println(count);
}
private static void dfs(int k) {
if (visited[k]) return;
visited[k] = true;
for (int i : al[k]) {
if (!visited[i]) dfs(i);
}
}
}
'Coding Test > 백준' 카테고리의 다른 글
[백준/자바] 13023 - ABCDE (0) | 2023.08.04 |
---|---|
[백준/자바] 2023 - 신기한 소수 (0) | 2023.08.04 |
[백준/자바] 1978 - 소수 찾기 (0) | 2023.08.04 |
[백준/자바] 1517 - 버블 소트 (0) | 2023.08.03 |
[백준/자바] 11004 - K번째 수 (0) | 2023.08.03 |