Search
Duplicate
๐Ÿฅˆ

DFS์™€ BFS

์ฃผ์ฐจ
15
๋ฌธ์ œ๋ฒˆํ˜ธ
1260
์–ธ์–ด
C++
ํ‹ฐ์–ด
์‹ค๋ฒ„
์œ ํ˜•
BFS
DFS
nj_Blog
O
nj_์ƒํƒœ
ํ‘ธ๋Š” ์ค‘
์ดํ•ด๋„
ํ’€์ด
์‚ฌ๋žŒ
์ดํ•ด๋„ 2
13 more properties

๋ฌธ์ œ๋งํฌ

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

์ฝ”๋“œ ์ œ์ถœ ๊ธฐ๋ก (๋ฉ”๋ชจ๋ฆฌ ๋ฐ ์‹œ๊ฐ„)

๋ฉ”๋ชจ๋ฆฌ : 3656 KB
์‹œ๊ฐ„ : 4 ms

Code

#include <iostream> #include <queue> #include <algorithm> int N; int M; int V; std::vector <int> arr[1001]; std::queue <int> bfs_arr[1001]; std::queue <int> dfs_arr[1001]; std::queue <int> bfs_result; int visited[1001] = {0 , }; void input_faster() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); } void my_queue_sort(int i) { sort(arr[i].begin(), arr[i].end()); for(int j = 0 ; j<arr[i].size() ; j++){ dfs_arr[i].push(arr[i][j]); bfs_arr[i].push(arr[i][j]); } } void input() { int x; int y; std::cin >> N >> M >> V; while (M--){ std::cin >> x >> y; arr[x].push_back(y); arr[y].push_back(x); } // queue ์ •๋ ฌ for(int i = 1 ; i<=N ; i++) my_queue_sort(i); } void dfs(int V) { std::cout << V << " "; visited[V] = 1; int num = dfs_arr[V].size(); for(int i = 0; i < num ; i++){ int next = dfs_arr[V].front(); if(visited[next] == 0){ dfs(next); } dfs_arr[V].pop(); } } void bfs(int V) { // visited ์ดˆ๊ธฐํ™” for (int i = 0; i < N + 1; i++) visited[i] = 0; bfs_result.push(V); visited[V] = 1; while(!bfs_result.empty()){ int tmp = bfs_result.front(); bfs_result.pop(); std::cout << tmp << " "; while(!bfs_arr[tmp].empty()){ if(visited[bfs_arr[tmp].front()] == 0){ bfs_result.push(bfs_arr[tmp].front()); visited[bfs_arr[tmp].front()] = 1; } bfs_arr[tmp].pop(); } } } void print_val() { dfs(V); std::cout << std::endl; bfs(V); } int main() { input_faster(); input(); print_val(); return (0);
C++
๋ณต์‚ฌ

๋ฉ”๋ชจ

๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ํฌ๊ฒŒย ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)๊ณผย ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)์ด ์žˆ์Šต๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ ๊ทธ๋ž˜ํ”„๋ž€,ย ์ •์ (node)๊ณผ ๊ทธ ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ (edge)์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ์ผ์ข…์„ ๋งํ•˜๋ฉฐ,
๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค๋Š” ๊ฒƒ์€ย ํ•˜๋‚˜์˜ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์ฐจ๋ก€๋Œ€๋กœ ๋ชจ๋“  ์ •์ ๋“ค์„ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

1. ์ž…๋ ฅ๊ฐ’ ์ €์žฅ

์šฐ์„ , DFS, BFS ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”ย ์ž…๋ ฅ๊ฐ’์„ queue ์— ์ €์žฅํ•˜๋Š” ๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ ์•„๋ž˜์˜ ๊ฒฝ์šฐ๋กœ ์ž…๋ ฅ๊ฐ’์ด ๋“ค์–ด์˜จ๋‹ค๋ฉด
1 2 1 3 1 4 2 4 3 4
C++
๋ณต์‚ฌ
์ˆœ์„œ๋Œ€๋กœ queue[a].push(b) & queue[b].push(a) ๊ณผ์ •์„ ๊ฑฐ์ณ ์•„๋ž˜์™€ ๊ฐ™์€ queue๋ฅผ ๋งŒ๋“ค์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฐ˜๋ฉด, ์•„๋ž˜์˜ ๊ฒฝ์šฐ๋กœ ์ž…๋ ฅ๊ฐ’์ด ๋“ค์–ด์˜จ๋‹ค๋ฉด
5 4 5 2 1 2 3 4 3 1
C++
๋ณต์‚ฌ
์ˆœ์„œ๋Œ€๋กœ queue[a].push(b) & queue[b].push(a) ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•œ ํ›„ย ์ •๋ ฌ์„ ํ•ด์•ผ ์•„๋ž˜์˜ dfs, bfs ์›๋ฆฌ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2. DFS & BFS ์›๋ฆฌ

์•„๋ž˜์˜ ๊ทธ๋ž˜ํ”„์—์„œย ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ๋ฅผ 1ย ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ ย DFS, BFS ๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
1. ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ (DFS, Depth-First Search):ย ์ตœ๋Œ€ํ•œ ๊นŠ์ด ๋‚ด๋ ค๊ฐ„ ๋’ค, ๋”์ด์ƒ ๊นŠ์ด ๊ฐˆ ๊ณณ์ด ์—†์„ ๊ฒฝ์šฐ ์˜†์œผ๋กœ ์ด๋™ย  ย ์ผ๋ฐ˜์ ์œผ๋กœ DFS ๋Š” ์Šคํƒ ๋˜๋Š” ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
2. ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ (BFS, Breadth-First Search): ์ตœ๋Œ€ํ•œ ๋„“๊ฒŒ ์ด๋™ํ•œ ๋‹ค์Œ, ๋” ์ด์ƒ ๊ฐˆ ์ˆ˜ ์—†์„ ๋•Œ ์•„๋ž˜๋กœ ์ด๋™ย  ย ์ผ๋ฐ˜์ ์œผ๋กœ BFS ๋Š” ํ๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค

์ฐธ์กฐ

void input_faster() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); }
C++
๋ณต์‚ฌ
๋ฅผ ์“ธ ์‹œ์—๋Š” printf, scanf ์ ˆ๋Œ€ ์‚ฌ์šฉ ๊ธˆ์ง€!