백준 2606번) 바이러스

하우르·2021년 3월 23일
0
post-custom-banner
package 신규아이디추천;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
	static char[][] map;

	public static int BFS(int start_vertex) {
		HashSet<Integer> visited = new HashSet<>();
		Queue<Integer> will_visit = new LinkedList<>();
		visited.add(start_vertex);
		will_visit.add(start_vertex);
		while (will_visit.isEmpty() == false) {
			Integer current_vertex = will_visit.remove();
			for (int i = 0; i < map.length; i++) {
				if (map[current_vertex - 1][i] == 1 && !visited.contains(i + 1)) {
					visited.add(i + 1);
					will_visit.add(i + 1);
				}
			}
		}
		return visited.size();
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		int N = Integer.parseInt(reader.readLine());
		int K = Integer.parseInt(reader.readLine());
		StringTokenizer tokenizer;
		map = new char[101][101];
		for (int i = 0; i < K; ++i)
		{
			tokenizer = new StringTokenizer(reader.readLine());
			int x = Integer.parseInt(tokenizer.nextToken());
			int y = Integer.parseInt(tokenizer.nextToken());
			map[x-1][y-1]=map[y-1][x-1]=1;
		}
		int count = BFS(1)-1;
		System.out.println(count);

	}




}
profile
주니어 개발자
post-custom-banner

0개의 댓글