내가 정리하려고 했던 느낌으로 정리한 분이 계셔서 링크 첨부
https://codingnojam.tistory.com/44 (DFS) => 브루트포스,백트래킹 문제에 자주 사용
https://codingnojam.tistory.com/41 (BFS) => 주로 최단거리 문제 풀 때, 다익스트라 등등
import java.util.*;
import java.io.*;
class Main{
static int computerNum;
static int connectedCom;
static int inffectedNum = 0;
static int[][] graph;
static boolean[] visited;
public static void main(String args[])throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
computerNum = Integer.parseInt(br.readLine());
connectedCom = Integer.parseInt(br.readLine());
graph = new int[computerNum+1][computerNum+1];
visited = new boolean[computerNum+1];
int startIndex = 1;
for(int i=1; i<=connectedCom ;i++){
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
graph[n][m] = graph[m][n]= 1;
}
dfs(startIndex);
System.out.println(inffectedNum-1);
}
public static void dfs(int startIndex){
visited[startIndex]=true;
inffectedNum++;
for(int i=1; i<=computerNum ; i++){
if(graph[startIndex][i]==1 && !visited[i]){
dfs(i);
}
}
}
}
얻어갈 점: