Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

최's 먹공로그

BOJ2606 바이러스 본문

APS

BOJ2606 바이러스

ChoiSH313 2022. 6. 3. 19:45

<Solution>

1. BOJ1260 DFS와 BFS의 DFS 풀이법과 똑같은 문제이다

2. map에 간선을 연결하고 노드의 방문의 체크하며 카운트!

3. map이 연결되어 있지 않거나( != 1) Node에 이미 방문 했을 경우(== 1)에는 넘어가고 아닌 경우에는 연결된 Node 니깐 카운트 해준다!


<Source Code>

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class BOJ2606_바이러스 {

	static int[][] map;
	static int cnt;
	static int[] visit;
	static int map_size;
	
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine(), "");
		map_size = Integer.parseInt(st.nextToken());
		st = new StringTokenizer(br.readLine(), "");
		int line_size = Integer.parseInt(st.nextToken());
		map = new int[map_size][map_size];
		visit = new int[map_size];
		for(int i = 0; i < line_size; i++)
		{
			st = new StringTokenizer(br.readLine(), " ");
			int n1 = Integer.parseInt(st.nextToken());
			int n2 = Integer.parseInt(st.nextToken());
			map[n1-1][n2-1] = 1;
			map[n2-1][n1-1] = 1;
		}
		dfs(0);
		System.out.print(cnt);
	}
	
	public static void dfs(int V) {
		visit[V] = 1;
		for(int i = 0; i < map_size; i++) {
			if(map[V][i] != 1 || visit[i] == 1)
				continue;
			cnt++;
			dfs(i);
		}
	}
}

 

저녁에 꾸역꾸역 나와서 하는중... 화이팅!!

'APS' 카테고리의 다른 글

BOJ2644 촌수계산  (0) 2022.06.05
BOJ2667 단지번호붙이기  (0) 2022.06.05
BOJ2178 미로탐색  (0) 2022.05.22
BOJ1260 DFS와 BFS  (0) 2022.05.22
BOJ1018 체스판 다시 칠하기  (0) 2022.05.22