-
Notifications
You must be signed in to change notification settings - Fork 0
/
breadth first search.cpp
70 lines (62 loc) · 1.39 KB
/
breadth first search.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
66
67
68
69
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
cin>>t;
while(t--)
{
int i,j,n,e,a,b,s,k;
cin>>n>>e;
int arr[n+1][n+1]={0};
int fi[n+1]={0};
list<int> *adj;
adj = new list<int>[n+1];
for(i=0;i<e;i++)
{cin>>a>>b;
if(arr[a][b]==0)
{
adj[a].push_back(b);
adj[b].push_back(a);
arr[a][b]=1;
arr[b][a]=1;
}
}
cin>>s;
k=s;
bool *visited = new bool[n+1];
for(int i = 1; i <= n; i++)
visited[i] = false;
list<int> queue;
visited[s] = true;
queue.push_back(s);
list<int>::iterator l;
while(!queue.empty())
{s = queue.front();
queue.pop_front();
for(l = adj[s].begin(); l != adj[s].end(); ++l)
{
if(!visited[*l])
{
visited[*l] = true;
queue.push_back(*l);
fi[*l]=fi[s]+6;
}
}
}
for(j=1;j<=n;j++)
{if(j!=k)
{if(fi[j]==0)
cout<<"-1"<<" ";
else
cout<<fi[j]<<" ";}
}
cout<<"\n";
}
return 0;
}