자료구조, 알고리즘/문제풀이
[문제] 웜바이러스 2606번
민쓰
2018. 9. 5. 23:13
문제
https://www.acmicpc.net/problem/2606
소스코드
import java.util.Scanner;
public class Virus {
private static int v;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
v = sc.nextInt(); // node, vertex
int e = sc.nextInt(); // edge
sc.nextLine();
int[][] graph = new int[v][v];
int[] check = new int[v];
for(int i=0; i<e; i++){
String[] input = sc.nextLine().split(" ");
int a = Integer.parseInt(input[0]);
int b = Integer.parseInt(input[1]);
graph[a-1][b-1] = 1; // 인접행렬을 통해 관계 표시
graph[b-1][a-1] = 1;
}
dfs(0, graph, check);
int cnt = 0;
for(int i=1; i<v; i++){
if(check[i] == 1)
cnt++;
}
System.out.println(cnt);
}
private static void dfs(int i, int[][] graph, int[] check) {
check[i] = 1;
for(int j=0; j<v; j++){
if(graph[i][j] == 1 && check[j] !=1){
dfs(j, graph, check);
}
}
}
}
풀이
-