티스토리 뷰
//알고리즘
1. dfs탐색 문제
2. 시작점에서 dfs탐색을 시작하여 도달할 수 있는 최대 개수를 구하면됨.
더보기
import java.util.ArrayList;
import java.util.Scanner;
public class 바이러스 {
static int N, M, count;
static boolean[] v;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
v = new boolean[N + 1];
ArrayList<Integer>[] adjList = new ArrayList[N + 1];
for (int i = 0; i < adjList.length; i++) {
adjList[i] = new ArrayList<Integer>();
}
for (int i = 0; i < M; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
adjList[from].add(to);
adjList[to].add(from); //양방향 중요!!
}
dfs(adjList, 1);
System.out.println(count - 1); // 1번 컴퓨터 제외
}
private static void dfs(ArrayList<Integer>[] adjList, int i) {
v[i] = true;
count++;
int size = adjList[i].size();
for (int j = 0; j < size; j++) {
int n = adjList[i].get(j);
if (!v[n]) {
dfs(adjList, n);
}
}
}
}
'Algorithm' 카테고리의 다른 글
SW Expert Academy_점심 식사시간 (0) | 2020.11.14 |
---|---|
백준_경잭적전염_18405 (0) | 2020.11.13 |
백준_적록색약_10026 (0) | 2020.11.11 |
백준_녹색옷입은애가 젤다지_4485 (0) | 2020.11.10 |
백준_벽부수고 이동하기_2206 (0) | 2020.11.09 |