Skip to content

Commit f32936b

Browse files
committed
[Silver II] Title: DFS와 BFS, Time: 164 ms, Memory: 15884 KB -BaekjoonHub
1 parent acf4c19 commit f32936b

2 files changed

Lines changed: 46 additions & 47 deletions

File tree

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,65 @@
1-
/* /dev/stdin */
2-
let fs = require('fs');
3-
let input = fs.readFileSync('/dev/stdin').toString().split('\n');
1+
// /dev/stdin
2+
const fs = require("fs");
3+
let input = fs
4+
.readFileSync("/dev/stdin")
5+
.toString()
6+
.split("\n")
7+
.map((x) => x.replace("\r", ""));
48

5-
// 노드, 간선, 시작
6-
const [N, M, V] = input[0].split(' ').map(Number);
7-
8-
let graph = [];
9+
const [N, M, V] = input[0].split(" ").map(Number);
910

10-
for(let i = 0; i <= N; i++) {
11-
graph.push([]);
12-
}
13-
for(let i = 1; i <= M; i++) {
14-
const [x, y] = input[i].split(' ').map(Number)
15-
graph[x].push(y)
16-
graph[x].sort((a, b) => a - b);
17-
graph[y].push(x)
18-
graph[y].sort((a, b) => a - b);
19-
}
20-
21-
// console.log(graph)
11+
const graph = Array.from({ length: N + 1 }, () => []);
2212

23-
let visited = Array(N+1).fill(false);
24-
let dfsLine = '';
25-
let bfsLine = '';
13+
const dfsResult = [];
14+
const bfsResult = [];
2615

16+
for (let i = 1; i <= M; i++) {
17+
const [x, y] = input[i].split(" ").map(Number);
18+
graph[x].push(y);
19+
graph[y].push(x);
20+
}
2721

28-
dfs(graph, V, visited);
29-
30-
visited = Array(N+1).fill(false);
31-
bfs(graph, V, visited);
32-
console.log(dfsLine)
33-
console.log(bfsLine)
22+
// 🔑 방문 순서 조건
23+
for (let i = 1; i <= N; i++) {
24+
graph[i].sort((a, b) => a - b);
25+
}
3426

27+
let visited = Array.from({ length: N + 1 }, () => false);
3528

29+
dfs(graph, V, visited);
3630

3731
function dfs(graph, v, visited) {
32+
// 현재 노드를 방문 처리
3833
visited[v] = true;
39-
dfsLine += v + ' '
40-
41-
for(i of graph[v]) {
42-
if(!visited[i]) {
43-
dfs(graph, i, visited);
34+
dfsResult.push(v);
35+
// 현재 노드와 연결된 다른 노드를 재귀적으로 방문
36+
for (const next of graph[v]) {
37+
if (!visited[next]) {
38+
dfs(graph, next, visited);
4439
}
4540
}
4641
}
42+
//////////////
43+
visited = Array.from({ length: N + 1 }, () => false);
4744

48-
function bfs(graph, start, visited) {
49-
let queue = [];
50-
queue.push(start);
45+
bfs(graph, V, visited);
5146

52-
visited[start] = true;
47+
function bfs(graph, v, visited) {
48+
const queue = [];
49+
queue.push(v);
50+
visited[v] = true;
5351

54-
while(queue.length != 0) {
55-
let v = queue.shift();
56-
bfsLine += (v + ' ');
57-
for(i of graph[v]) {
58-
if(!visited[i]) {
59-
queue.push(i);
60-
visited[i] = true;
52+
while (queue.length > 0) {
53+
const q = queue.shift(); // 큐의 맨 앞
54+
bfsResult.push(q);
55+
for (const next of graph[q]) {
56+
if (!visited[next]) {
57+
visited[next] = true;
58+
queue.push(next);
6159
}
6260
}
6361
}
64-
6562
}
6663

64+
console.log(dfsResult.join(" "));
65+
console.log(bfsResult.join(" "));

백준/Silver/1260. DFS와 BFS/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 34072 KB, 시간: 80 ms
7+
메모리: 15884 KB, 시간: 164 ms
88

99
### 분류
1010

1111
그래프 이론, 그래프 탐색, 너비 우선 탐색, 깊이 우선 탐색
1212

1313
### 제출 일자
1414

15-
2024년 4월 15일 14:30:56
15+
2026년 2월 9일 18:14:08
1616

1717
### 문제 설명
1818

0 commit comments

Comments
 (0)