forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.cpp
More file actions
65 lines (60 loc) · 1.25 KB
/
BFS.cpp
File metadata and controls
65 lines (60 loc) · 1.25 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
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define dopamine ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
int maxi = INT_MAX, mini = INT_MIN;
bool visited[100005] = {false};
int dist[100005];
vector<int>arr[100005];
void initialise(int a)
{
for (int i = 0; i < a + 1; i++)
{ dist[i] = -1;
visited[i] = false;
}
}
int bfs(int a, int b)
{
queue<int>q;
q.push(a);
dist[a] = 0;
while (!q.empty())
{
int r = q.front();
q.pop();
visited[r] = true;
for (int i = 0; i < arr[r].size(); i++ )
{
if (!visited[arr[r][i]])
{
visited[arr[r][i]] = true;
dist[arr[r][i]] = dist[r] + 1;
q.push(arr[r][i]);
}
}
}
return dist[b];
}
int32_t main()
{
//serotonin();
dopamine
int t = 1;//cin >> t;
while (t--)
{
int a, b; cin >> a >> b;
for (int i = 0; i < b; i++)
{
int q, w;
cin >> q >> w;
arr[q].push_back(w);
arr[w].push_back(q);
}
initialise(a);
int e, r;
cin >> e >> r;
cout << bfs(e , r ) << "\n";
}
return 0;
}