https://www.acmicpc.net/problem/2606
DFS (Depth-First Search)
DFS๋ ๊น์ด ์ฐ์ ํ์์ด๋ค.
์๋ ๊ทธ๋ฆผ์ฒ๋ผ ํ ๊ณณ๋ง ๊น๊ฒ~ ๊ณ์ ํ๋ค๊ฐ ๋์ด ๋์ค๋ฉด, ๋ค๋ฅธ ๊ณณ์ผ๋ก ๊ฐ์ ๋ค์ ๊น๊ฒ~ ๊ณ์ ํ๋ ๊ฒ์ด๋ค.
- ๋จ์ ๊ฒ์์ BFS๋ณด๋ค ๋๋ฆฌ๋ค
- ๋ชจ๋ ๊ณณ์ ๋ฐฉ๋ฌธํ ๋ ์์ฃผ ์ฌ์ฉ๋๋ค.
- ์ฌ๊ทํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ ํ์ ๋ ธ๋(ํ์ฌ ๋ ธ๋์ ์ธ์ ํ๊ณ , ๋ฐฉ๋ฌธํ ์ ์ด ์๋ ๋ ธ๋)๋ก ์ด๋ํ๋ค.
BFS (Breadth-First Search)
BFS๋ ๋๋น ์ฐ์ ํ์์ด๋ค.
์๋ ๊ทธ๋ฆผ์ฒ๋ผ ์์ผ๋ก ๋๊ฒ~ ํ๋์ฉ ์ดํด๋ณด๊ณ ์ฐพ๋ ๊ฐ์ด ์์ผ๋ฉด, ํ ๋จ๊ณ ๊น๊ฒ ๋ค์ด๊ฐ์ ๋ค์ ๋๊ฒ~ ์ดํด๋ณด๋ ๊ฒ์ด๋ค.
- ๋จ์ ๊ฒ์์ DFS๋ณด๋ค ๋น ๋ฅด๋ค.
- ์ต๋จ๊ฒฝ๋ก๋ฅผ ์ฐพ์ ๋ ์์ฃผ ์ฌ์ฉ๋๋ค
- Queue๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ ํ์ ๋ ธ๋(ํ์ฌ ๋ ธ๋์ ์ธ์ ํ๊ณ , ๋ฐฉ๋ฌธํ ์ ์ด ์๋ ๋ ธ๋)๋ฅผ ์ ์ฅํ๋ค.
DFS ๋ฌธ์ ํ์ด
node[a][b] = node[b][a] = 1
: ๊ฐ ๋ ธ๋๋ค์ ๋ฐฉํฅ์ฑ์ด ์๋ค. ๋ฐฉํฅ์ฑ์ด ์์ผ๋ 2๋ฒ์์ 5๋ฒ์ผ๋ก ๊ฐ์๋, 5๋ฒ์์ 2๋ฒ์ผ๋ก ๊ฐ์๋ ์๋ค.
: ๋์นญ์ขํ๋ฅผ ์ ๋ ฅํด์ฃผ๋ฉด ์ข ๋ ์ฝ๊ฒ ํ์์ด ๋๋ ์ ์๋ค.
dfs ๋ถ๋ถ์ ์ฃผํฉ์๋ถ๋ถ์ ์์ธํ๊ฒ ์ ์ด๋จ์ผ๋ ์ฝ๋๋ฅผ ๋ฐ๋ผ ์ฝ์ด๋ณด๋ฉด ์์ฐ์ค๋ฝ๊ฒ ์ดํด๋ ๊ฒ์ด๋ค!
dfs ํจ์๋ฅผ ์ฌ๊ท์ ์ผ๋ก ํธ์ถํ๋ฉด์ ๋ ๊น๊ฒ ํ์ํ๋ค
DFS ์ ์ถ์ฝ๋
package baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main2606_dfs {
/*
7 //์ปดํจํฐ์ ์
6 //๋คํธ์ํฌ์ ์ฐ๊ฒฐ๋์ด์๋ ์ปดํจํฐ ์์ ์
1 2 // 1-2 ์ฐ๊ฒฐ๋จ
2 3 // 2-3 ์ฐ๊ฒฐ๋จ
1 5 // 1-5 ์ฐ๊ฒฐ๋จ
5 2 // 5-2 ์ฐ๊ฒฐ๋จ
5 6 // 5-6 ์ฐ๊ฒฐ๋จ
4 7 // 4-7 ์ฐ๊ฒฐ๋จ
* */
static int[][] node;
static boolean[] check; //๋ฐ์ด๋ฌ์ค์ ๊ฐ์ผ๋์๋์ง
static int count = 0; //๊ฐ์ผ ์ปดํจํฐ ์
static int computers;
static int pairs;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
computers = Integer.parseInt(br.readLine());
pairs = Integer.parseInt(br.readLine());
node = new int[computers+1][computers+1];
check = new boolean[computers+1];
for (int i = 0; i < pairs; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
node[a][b] = 1;
node[b][a] = 1;
//๋ฐฉํฅ์ฑ์ด ์๊ธฐ๋๋ฌธ์
}
dfs(1);
System.out.println(count-1);
}
public static void dfs(int start) {
check[start] = true; // 1๋ฒ (start) ๊ฐ์ผ
count++; //๊ฐ์ผ๋ ์ปดํจํฐ ์ ์ฆ๊ฐ
for (int i = 0; i <= computers; i++) { //์ปดํจํฐ ๊ฐ์๋งํผ ๋ฐ๋ณต
if (node[start][i] == 1 && !check[i]) { //node[start][i] == 1 : 1๋ฒ(start)๊ณผ ์ฐ๊ฒฐ๋ ๋
ธ๋๋ผ๋ฉด //!check[i] : i๋ฒ ์ปดํจํฐ๊ฐ ๊ฐ์ผ๋์ง ์์๋ค๋ฉด
dfs(i); // i๋ฒ ์ปดํจํฐ ๊ฐ์ผ์ฒ๋ฆฌ๋ฆฌ
}
}
}
}
BFS ๋ฌธ์ ํ์ด
node[a][b] = node[b][a] = 1
: ๊ฐ ๋ ธ๋๋ค์ ๋ฐฉํฅ์ฑ์ด ์๋ค. ๋ฐฉํฅ์ฑ์ด ์์ผ๋ 2๋ฒ์์ 5๋ฒ์ผ๋ก ๊ฐ์๋, 5๋ฒ์์ 2๋ฒ์ผ๋ก ๊ฐ์๋ ์๋ค.
: ๋์นญ์ขํ๋ฅผ ์ ๋ ฅํด์ฃผ๋ฉด ์ข ๋ ์ฝ๊ฒ ํ์์ด ๋๋ ์ ์๋ค.
bfs ๋ถ๋ถ์ ์ฃผํฉ์๋ถ๋ถ์ ํ๋ฆ์ ๋ฐ๋ผ๊ฐ๋ณด์!
bfs ํจ์๋ 1๋ฒ๋ง ํธ์ถ๋๊ณ , ๋ ธ๋๋ฅผ ํ์ํ๋ฉด์ ๋ค์์ ์ด๋ค ๋ ธ๋๋ฅผ ํ์ํ ๊ฒ์ธ์ง๋ฅผ queue์ ์ ์ฅํ๋ค.
ํ์ฌ ๋ ธ๋ ํ์์ด ๋๋๋ฉด, queue์ ์ ์ฅํด๋ ๋ ธ๋๋ฅผ ๊บผ๋ด์ด ํ์ํ๋ค.
BFS ์ ์ถ์ฝ๋
package baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main2606_bfs {
/*
7 //์ปดํจํฐ์ ์
6 //๋คํธ์ํฌ์ ์ฐ๊ฒฐ๋์ด์๋ ์ปดํจํฐ ์์ ์
1 2 // 1-2 ์ฐ๊ฒฐ๋จ
2 3 // 2-3 ์ฐ๊ฒฐ๋จ
1 5 // 1-5 ์ฐ๊ฒฐ๋จ
5 2 // 5-2 ์ฐ๊ฒฐ๋จ
5 6 // 5-6 ์ฐ๊ฒฐ๋จ
4 7 // 4-7 ์ฐ๊ฒฐ๋จ
* */
static int[][] node;
static boolean[] check; //๋ฐ์ด๋ฌ์ค์ ๊ฐ์ผ๋์๋์ง
static int count = 0; //๊ฐ์ผ ์ปดํจํฐ ์
static Queue<Integer> queue = new LinkedList<>();
static int computers;
static int pairs;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
computers = Integer.parseInt(br.readLine());
pairs = Integer.parseInt(br.readLine());
node = new int[computers+1][computers+1];
check = new boolean[computers+1];
for (int i = 0; i < pairs; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
node[a][b] = 1;
node[b][a] = 1;
//๋ฐฉํฅ์ฑ์ด ์๊ธฐ๋๋ฌธ์
}
bfs(1);
System.out.println(count);
}
public static void bfs(int start) {
queue.offer(start);
check[start] = true; // 1๋ฒ (start) ๊ฐ์ผ
while(!queue.isEmpty()){
int temp = queue.poll();
for (int i = 0; i <= computers; i++) {
if(node[temp][i] == 1 && !check[i]) {
queue.offer(i);
check[i] = true;
count++;
}
}
}
}
}
'๐ซAlgorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋์ ๊ณํ๋ฒ DP ๊ตฌํ๋ฐฉ๋ฒ ( + [๋ฐฑ์ค] 11050 ์ดํญ๊ณ์ ๋ฌธ์ ํ์ด) (0) | 2022.02.13 |
---|