forked from rohitthapliyal2000/Competitive-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.cpp
More file actions
80 lines (75 loc) · 1.49 KB
/
BFS.cpp
File metadata and controls
80 lines (75 loc) · 1.49 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
#include <bits/stdc++.h>
using namespace std;
int visited[1001];
int counter;
void bfs(int node, int dest, vector <int> arr[])
{
for(int i = 1; i <= 1000; i++)
{
visited[i] = 0;
}
vector <int> record(1001);
list <int> q;
visited[node] = 1;
q.push_back(node);
while(!q.empty())
{
int s = q.front();
q.pop_front();
for(int i = 0; i < arr[s].size(); i++)
{
if(visited[arr[s][i]] == 0)
{
visited[arr[s][i]] = 1;
q.push_back(arr[s][i]);
record[arr[s][i]] = s;
}
}
}
if(visited[dest] == 0)
{
cout << "-1 ";
return ;
}
int i = dest;
counter = 6;
while(record[i] != node)
{
i = record[i];
counter += 6;
}
}
int main()
{
int tests;
cin >> tests;
while(tests --)
{
int nodes, edges, u, v;
cin >> nodes >> edges;
vector <int> arr[nodes+1];
for(int i = 0; i < edges; i++)
{
cin >> u >> v;
arr[u].push_back(v);
arr[v].push_back(u);
}
int s;
cin >> s;
for(int i = 1; i <= nodes; i++)
{
if(i == s)
{
continue;
}
counter = 0;
bfs(s, i, arr);
if(counter)
{
cout << counter << " ";
}
}
cout << endl;
}
return 0;
}