-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs to find level of each node.cpp
More file actions
66 lines (61 loc) · 1.29 KB
/
bfs to find level of each node.cpp
File metadata and controls
66 lines (61 loc) · 1.29 KB
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
#include<bits/stdc++.h>
#define ll long long
#define vll vector<ll>
#define pb push_back
using namespace std;
vll adj[20];
bool visited[20];
ll level[20]={0};
void bfs(ll s)
{
queue<ll> q;
visited[s]=true;
q.push(s);
while(!q.empty())
{
s=q.front();
q.pop();
cout<<s<<' '<<level[s]<<endl;
for(ll i=0 ; i<adj[s].size() ; i++)
{
ll node=adj[s][i];
if(!visited[node])
{
level[node]=level[s]+1; // level(child)=level(parent)+1
q.push(node);
visited[node]=true;
}
}
}
}
void addEdge(ll a,ll b)
{
adj[a].pb(b);
adj[b].pb(a);
}
void initialize()
{
for(ll i=0 ; i<20 ; i++)
visited[i]=false;
}
int main()
{
ll edges[][2]={
{1,2},
{1,3},
{2,4},
{2,5},
{3,6},
{3,7},
{4,8},
{4,9},
{4,10},
{5,11},
{7,12},
{7,13}
};
for(ll i=0 ; i<12 ; i++)
addEdge(edges[i][0],edges[i][1]);
bfs(2);
return 0;
}