-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260.cpp
More file actions
72 lines (63 loc) · 913 Bytes
/
1260.cpp
File metadata and controls
72 lines (63 loc) · 913 Bytes
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
// 1260. DFS와 BFS
// 2019.05.14
// DFS, BFS
#include<iostream>
#include<queue>
using namespace std;
int arr[1001][1001];
bool visit[1001];
void DFS(int v, int n)
{
visit[v] = true;
cout << v << " ";
for (int i = 1; i <= n; i++)
{
if (arr[v][i] == 1 && visit[i] == false)
{
DFS(i, n);
}
}
}
void BFS(int v, int n)
{
queue<int> q;
visit[v] = true;
q.push(v);
cout << v << " ";
while (!q.empty())
{
int temp = q.front();
q.pop();
for (int i = 0; i <= n; i++)
{
if (arr[temp][i] == 1 && visit[i] == false)
{
q.push(i);
visit[i] = true;
cout << i << " ";
}
}
}
}
int main(void)
{
int n, m, v;
cin >> n >> m >> v;
while (m > 0)
{
m--;
int a, b;
cin >> a >> b;
arr[a][b] = 1;
arr[b][a] = 1;
}
DFS(v, n);
cout << endl;
for (int i = 0; i < 1001; i++)//방문여부 초기화
{
visit[i] = false;
}
BFS(v, n);
cout << endl;
return 0;
}