-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreadth First Search: Shortest Reach.cpp
65 lines (52 loc) · 1.22 KB
/
Breadth First Search: Shortest Reach.cpp
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
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
void bfs(ll start, vector<ll> adjList[], vector<ll> &dis, vector<ll> &parent)
{
queue<ll> q;
q.push(start);
dis[start] = 0;
while (!q.empty())
{
ll u = q.front();
q.pop();
for(int i=0; i < adjList[u].size(); i++)
{
int v = adjList[u][i];
if(dis[v]==-1)
{
dis[v] = dis[u] + 6;
parent[v] = u;
q.push(v);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(nullptr);
// memset(dp, -1, sizeof(dp));
ll tc; cin>>tc;
while(tc--)
{
vector<ll> adjList[1010];
vector<ll> dis(1010, -1);
vector<ll> parent(1010, -1);
ll nodes, edges; cin>>nodes>>edges;
for(int i=1; i<=edges; i++)
{
ll u, v; cin>>u>>v;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
ll start; cin>>start;
bfs(start, adjList, dis, parent);
for(int i = 1; i <= nodes; i++)
{
if(dis[i] == 0) continue;
cout<<dis[i]<<" ";
}
cout<<endl;
}
}