-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbfsgraph.cpp
More file actions
56 lines (56 loc) · 760 Bytes
/
bfsgraph.cpp
File metadata and controls
56 lines (56 loc) · 760 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
#include<bits/stdc++.h>
using namespace std;
int main(){
int edge;
cin>>edge;
int x,y;
vector<int> v1[10];
queue<int> q;
for(int i=0;i<edge;i++){
cin>>x>>y;
v1[x].push_back(y);
v1[y].push_back(x);
}
for(int i=1;i<edge;i++){
cout<<i<<"->";
for(int j=0;j<v1[i].size();j++){
cout<<v1[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
for(int i=0;i<v1[1].size();i++){
q.push(v1[1][i]);
}
int arr[100];
memset(arr,0,100*sizeof(arr[0]));
cout<<"1 ";
arr[1]=1;
int temp=0;
while(!q.empty()){
temp=q.front();
q.pop();
for(int i=0;i<v1[temp].size();i++){
if(arr[v1[temp][i]]==0)
q.push(v1[temp][i]);
}
if(arr[temp]!=1){
cout<<temp<<" ";
arr[temp]=1;
}
}
return 0;
}
/*
10
1 2
1 3
3 4
3 5
4 6
5 6
5 7
4 9
9 8
5 8
*/