📍 문제
https://www.acmicpc.net/problem/2606
📍 전체 코드
인접행렬을 통한 풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class boj_2606 {
static int n, m;
static int[][] computers;
static boolean[] visited;
static int result = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
m = Integer.parseInt(br.readLine());
computers = new int[n + 1][n + 1];
visited = new boolean[n + 1];
for (int i = 0; i < m; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
computers[a][b] = computers[b][a] = 1;
}
dfs(1);
System.out.println(result);
}
public static void dfs(int node) {
visited[node] = true;
for (int i = 1; i < computers[node].length; i++) {
// 방문하지 않았고, 연결되어있다면
if(!visited[i] && computers[node][i] == 1) {
result++;
dfs(i);
}
}
}
}
'코딩테스트 > 백준' 카테고리의 다른 글
[백준 18870] 좌표 압축 (S2) (2) | 2025.01.27 |
---|---|
[백준 2579] 계단 오르기 (S3) (0) | 2025.01.22 |
[백준 2839] 설탕 배달 (S4) (0) | 2025.01.18 |
[백준 10026] 적록색약 (G5) (0) | 2025.01.12 |
[BOJ 11286] 절댓값 힙 (S1) (0) | 2025.01.11 |