-
Notifications
You must be signed in to change notification settings - Fork 96
/
[14]_1.cpp
54 lines (49 loc) · 875 Bytes
/
[14]_1.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
int n, m, start;
vector<int> adj[1001];
bool d[1001];
// 깊이 우선 탐색(DFS)
void dfs(int x) {
if (d[x]) return;
d[x] = true;
cout << x << ' ';
for (int i = 0; i < adj[x].size(); i++) {
int y = adj[x][i];
dfs(y);
}
}
// 너비 우선 탐색(BFS)
void bfs(int x) {
queue<int> q;
q.push(x);
d[x] = true;
while (!q.empty()) {
int now = q.front();
q.pop();
cout << now << ' ';
for (int i = 0; i < adj[now].size(); i++) {
int y = adj[now][i];
if (!d[y]) {
q.push(y);
d[y] = true;
}
}
}
}
int main() {
cin >> n >> m >> start;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 1; i <= n; i++) {
sort(adj[i].begin(), adj[i].end());
}
dfs(start);
cout << '\n';
memset(d, false, sizeof(d));
bfs(start);
}