보라코딩
백준 자바 :: 바이러스 본문
import java.util.*;
import java.io.*;
public class Main{
static int node;
static int line;
static int[][] array;
static boolean[] visited;
static int count;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
node = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
line = Integer.parseInt(st.nextToken());
array = new int[node+1][node+1];
visited = new boolean[node+1];
for(int i = 0; i < line; i++){
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
array[a][b] = array[b][a] = 1;
}
DFS(1);
System.out.println(count-1);
}
static void DFS(int start){
count++;
visited[start] = true;
for(int i = 1; i <= node; i++){
if(array[start][i] == 1 && !visited[i]){
DFS(i);
}
}
}
}
'백준(java)' 카테고리의 다른 글
백준 자바 :: 안전영역(DFS) (0) | 2024.07.22 |
---|---|
백준 자바 :: 단지번호붙이기(DFS) / 유기농배추(DFS) / 섬의개수(DFS) (0) | 2024.07.19 |
백준 자바 :: DFS와 BFS (0) | 2024.07.04 |
백준 자바 :: 신기한 소수 (DFS) (0) | 2024.07.02 |
백준 자바 :: 연결요소의 개수 (0) | 2024.07.01 |