-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260.cpp
More file actions
90 lines (74 loc) · 1.37 KB
/
1260.cpp
File metadata and controls
90 lines (74 loc) · 1.37 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @file 1260.cpp
* @brief DFS와 BFS
* @author Sam Kim (samkim2626@gmail.com)
*/
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
int num = 1001;
vector<int> g[1001];
bool visited[1001];
void dfs(int start)
{
if (visited[start] == true)
{
return;
}
visited[start] = true;
cout << start << ' ';
for (int i = 0; i < g[start].size(); i++)
{
dfs(g[start][i]);
}
}
void bfs(int start)
{
deque<int> dq;
dq.push_back(start);
visited[start] = true;
while (!dq.empty())
{
int x = dq.front();
cout << x << ' ';
dq.pop_front();
for (int i = 0; i < g[x].size(); i++)
{
int y = g[x][i];
if (!visited[y])
{
dq.push_back(y);
visited[y] = true;
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, v;
cin >> n >> m >> v;
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
// 오름차순
for (int i = 0; i < num; i++)
{
sort(g[i].begin(), g[i].end());
}
dfs(v);
cout << '\n';
for (int i = 0; i < num; i++)
{
visited[i] = false;
}
bfs(v);
return 0;
}