Skip to content

Commit 20a49c0

Browse files
committed
[Silver II] Title: DFS와 BFS, Time: 8 ms, Memory: 5940 KB -BaekjoonHub
1 parent 3791cbf commit 20a49c0

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
#include <queue>
3+
#include <string.h>
4+
5+
using namespace std;
6+
#define MAX 1001
7+
8+
int graph[MAX][MAX];
9+
int visited[MAX];
10+
11+
int N, M, V;
12+
13+
void dfs(int V) {
14+
visited[V] = 1;
15+
cout << V << " ";
16+
17+
for (int i = 1; i <= N; i++) {
18+
if (graph[V][i] == 1 && visited[i] == 0) {
19+
dfs(i);
20+
}
21+
if (i == N) return;
22+
}
23+
}
24+
25+
void bfs(int V) {
26+
queue<int> q;
27+
q.push(V);
28+
29+
while (!q.empty()) {
30+
int next = q.front();
31+
q.pop();
32+
33+
visited[next] = 1;
34+
cout << next << " ";
35+
36+
for (int i = 1; i <= N; i++) {
37+
if (graph[next][i] == 1 && visited[i] == 0) {
38+
q.push(i);
39+
visited[i] = 1;
40+
}
41+
}
42+
}
43+
}
44+
45+
int main() {
46+
cin >> N >> M >> V;
47+
48+
for (int i = 0; i < M; i++) {
49+
int u, v;
50+
cin >> u >> v;
51+
graph[u][v] = 1;
52+
graph[v][u] = 1;
53+
}
54+
55+
dfs(V);
56+
57+
cout << '\n';
58+
memset(visited, 0, sizeof(visited));
59+
60+
bfs(V);
61+
62+
return 0;
63+
}

백준/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-
메모리: 122216 KB, 시간: 192 ms
7+
메모리: 5940 KB, 시간: 8 ms
88

99
### 분류
1010

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

1313
### 제출 일자
1414

15-
2023년 11월 18일 15:23:20
15+
2025년 1월 14일 09:39:25
1616

1717
### 문제 설명
1818

0 commit comments

Comments
 (0)