forked from vineetyadav2403/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shortest Reach.cpp
58 lines (51 loc) · 1.39 KB
/
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
//https://www.hackerrank.com/challenges/bfsshortreach/problem?h_r%5B%5D%5B%5D%5B%5D%5B%5D=next-challenge&h_r%5B%5D%5B%5D%5B%5D%5B%5D=next-challenge&h_v%5B%5D%5B%5D%5B%5D%5B%5D=zen&h_v%5B%5D%5B%5D%5B%5D%5B%5D=zen&isFullScreen=false
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
vector<int> adj[n+1];
int sp[n+1];
for(int k=0;k<=n;k++) sp[k] = -1;
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<bool> vis(n+1);
for(int k=0;k<=n;k++) vis[k] = false;
int p;
cin>>p;
queue<int> q;
q.push(p);
int count = 1;
vis[p] = true;
while(!q.empty()){
int k = q.size();
while(k--){
int v = q.front();
for(auto x:adj[v]){
if(!vis[x]){
vis[x] = true;
sp[x] = count * 6;
q.push(x);
}
}
q.pop();
}
count++;
}
for(int k=1;k<=n;k++){
if(k!=p){
cout<<sp[k]<<" ";
}
}
cout<<endl;
}
return 0;
}