-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs_traversal_with_level_tracking.cpp
More file actions
63 lines (51 loc) · 1003 Bytes
/
Copy pathbfs_traversal_with_level_tracking.cpp
File metadata and controls
63 lines (51 loc) · 1003 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
#include <bits/stdc++.h>
using namespace std;
vector<int> v[1005];
bool vis[1005];
int level[1005];
void bfs(int src)
{
queue<int> q;
q.push(src);
vis[src] = true;
level[src] = 0;
while (!q.empty())
{
int front = q.front();
q.pop();
cout << front << endl;
for (int child : v[front])
{
if (!vis[child])
{
q.push(child);
vis[child] = true;
level[child] = level[front] + 1;
}
}
}
}
int main()
{
int n, e;
cin >> n >> e;
while (e--)
{
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
memset(vis, false, sizeof(vis));
memset(level, -1, sizeof(level));
int src;
cin >> src;
bfs(src);
cout << endl
<< "Level of Nodes: " << endl;
for (int i = 0; i < n; i++)
{
cout << i << " -> " << level[i] << endl;
}
return 0;
}