-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
94 lines (78 loc) · 1.65 KB
/
bfs.cpp
File metadata and controls
94 lines (78 loc) · 1.65 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<bits/stdc++.h>
using namespace std;
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define pb push_back
#define rep(i,a,b) for(int i=a;i<b;i++)
#define vl vector<ll>
#define vi vector<int>
#define lb lower_bound
#define ub upper_bound
// vector<vector<int>> vec( n , vector<int> (m, 0));
// priority_queue<pi, vector<pi>, greater<pi>>q;
typedef int64_t ll;
#define pi pair<ll, int>
// bfs algorithm to travel all nodes and find shortest distnce from
// starting node to all other node
// https://www.hackerrank.com/challenges/bfsshortreach/problem
void solve()
{
int n, p;
cin >> n >> p;
std::vector<int>adj[n + 2];
for (int i = 0; i < p; i++)
{
int a1, a2;
cin >> a1 >> a2;
adj[a1].pb(a2);
adj[a2].pb(a1);
}
int start;
cin >> start;
// ********->>>>> BFS <<<<<-*******
vector<int>visited(n + 1, 0);
std::vector<ll>dist(n + 1, -1);
queue<int>q;
q.push(start);
visited[start] = 1;
dist[start] = 0;
while (!q.empty())
{
int node = q.front();
q.pop();
for (auto it : adj[node])
{
if (!visited[it])
{
q.push(it);
visited[it] = 1;
dist[it] = dist[node] + 1;
}
}
}
// ********->>>>> BFS End Here <<<<<-*******
// printing distance array
for (int i = 1; i < n + 1; i++)
{
if (i != start)
{ cout << dist[i] << " ";
}
}
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}