-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS_tree.cpp
54 lines (48 loc) · 840 Bytes
/
BFS_tree.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
/*
간선의 개수 : 7
1 2
1 3
1 5
2 4
2 6
3 7
3 8
*/
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector <int> v[12];
int n;
void bfs(int k)
{
queue <int> tree;
vector <bool> visited(n+2,0);
visited[k]=true;
tree.push(k);
while(!tree.empty()){
int current=tree.front();
tree.pop();
cout << current << "=>";
for(int i=0;i<v[current].size();i++){
int s=v[current][i];
if(!visited[s]){
visited[s]=true;
tree.push(s);
}
}
}
}
int main()
{
int a,b;
cout << "간선의 개수 : ";
cin >> n;
for(int i=0;i<n;i++){
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
cout << "==========너비우선탐색(BFS)=========="<<endl;
bfs(1);
}