슈콩

[BOJ] 2606 바이러스 본문

Algorithms/Baekjoon

[BOJ] 2606 바이러스

shukong 2026. 1. 20. 09:10

[문제]

 

https://www.acmicpc.net/problem/2606

 

 

[소스 코드]

import java.util.*;
import java.io.*;

public class Solution {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int n = Integer.parseInt(br.readLine());
		int m = Integer.parseInt(br.readLine());
		int[] map = new int[n+1];
		boolean[][] visit = new boolean[n+1][n+1];
		for(int i=0;i<m;i++) {
			st = new StringTokenizer(br.readLine());
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			visit[a][b] = visit[b][a] = true;
		}
		Queue<Integer> q = new LinkedList<>();
		boolean[] check = new boolean[n+1];
		q.offer(1);
		check[1] = true;
		int result = 0;
		while(!q.isEmpty()) {
			int curr = q.poll();
			for(int i=1;i<=n;i++) {
				if(visit[curr][i] && !check[i]) {
					check[i] = true;
					q.offer(i);
					result++;
				}
			}
		}
		System.out.println(result);
	}
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 2664 촌수계산  (0) 2026.01.22
[BOJ] 2667 단지번호붙이기  (0) 2026.01.22
[BOJ] 2178 미로 탐색  (0) 2026.01.09
[BOJ] 1260 DFS와 BFS  (3) 2026.01.08
[BOJ] 19236 청소년 상어  (0) 2025.12.17