-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProblemC.cpp
50 lines (49 loc) · 1.07 KB
/
ProblemC.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
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
void BFS(int);
vector <int> v[10];
bool vis[10];
int level[10];
int main(){
int nodes,edges;
cin>>nodes>>edges;
int a,b;
for(int i=0;i<edges;i++){
cin>>a>>b;
v[a].push_back(b);
}
for(int i=1;i<=nodes;i++){
cout<<"Children of node: "<<i<<" : ";
for(int j=0;j<v[i].size();j++){
if(j == v[i].size() - 1){
cout<<v[i][j]<<endl;
}else{
cout<<v[i][j]<<" ---> ";
}
}
}
cout<<"Now time for BFS"<<endl;
BFS(1);
return 0;
}
void BFS(int s){
queue <int> q;
q.push(s);
level[s] = 0;
vis[s] = true;
cout<<s<<endl;
while(!q.empty()){
int p = q.front();
q.pop();
for(int i=0;i<v[p].size();i++){
if(vis[v[p][i]] == false){
cout<<v[p][i]<<endl;
level [v[p][i]] = level[p]+1;
q.push(v[p][i]);
vis[v[p][i]] = true;
}
}
}
}