-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetLevel.cpp
More file actions
executable file
·57 lines (50 loc) · 1.1 KB
/
detLevel.cpp
File metadata and controls
executable file
·57 lines (50 loc) · 1.1 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
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[10];
bool visited[10];
int level[10];
void initialize()
{
for(int i=0; i<10; i++)
visited[i]=false;
}
void bfs(int s)
{
queue<int> q;
q.push(s);
visited[s]=true;
level[s]=0;
while(!q.empty())
{
int v=q.front();
q.pop();
for(int i=0; i<adj[s].size(); i++)
{
if(!visited[adj[s][i]])
{
q.push(adj[s][i]);
level[adj[s][i]]=level[s]+1;
visited[adj[s][i]]=true;
}
}
}
}
int main(void)
{
int nodes, edges, x, y, connectedComponents = 0;
cin >> nodes; // Number of nodes
cin >> edges; // Number of edges
for(int i = 0;i < edges;++i) {
cin >> x >> y;
// Undirected Graph
adj[x].push_back(y); // Edge from vertex x to vertex y
adj[y].push_back(x); // Edge from vertex y to vertex x
}
initialize(); // Initialize all nodes as not visited
int startNode;
cin>>startNode;
bfs(startNode);
for(int i=0; i<nodes; i++)
cout<<level[i]<<"\n";
return 0;
}