-
[백준] 2606 바이러스ALGORITHM/BOJ 2020. 11. 6. 23:04
2020-11-06
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { public static int N, K, count, arr[][]; public static boolean vtd[]; public static Queue<Integer> q; public static void solve() { while(!q.isEmpty()) { int num = q.poll(); vtd[num] = true; for(int i = 0; i < N; i++) { if((arr[num][i] == 1 || arr[i][num] == 1) && !vtd[i]) { q.add(i); vtd[i] = true; count++; } } } } public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine().trim()); N = Integer.parseInt(st.nextToken()); st = new StringTokenizer(bf.readLine().trim()); K = Integer.parseInt(st.nextToken()); arr = new int[N][N]; vtd = new boolean[N]; count = 0; q = new LinkedList<>(); for(int i = 0; i < K; i++) { st = new StringTokenizer(bf.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); arr[x-1][y-1] = 1; } q.add(new Integer(0)); solve(); System.out.println(count); } }
'ALGORITHM > BOJ' 카테고리의 다른 글
[백준] 1197 최소 스패닝 트리 (0) 2020.11.07 [백준] 1149 RGB거리 (0) 2020.11.07 [백준] 13300 방 배정 (0) 2020.11.06 [백준] 14503 로봇 청소기 (0) 2020.11.03 [백준] 9095 1, 2, 3 더하기 (0) 2020.11.02