forked from fatemehkarimi/uvaSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva-1148.cpp
66 lines (52 loc) · 1.27 KB
/
uva-1148.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
//uva 1148
//The mysterious X network
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
void BFS(vector < vector <int> > & Graph, vector <int> & levels, int s);
int main(void)
{
int T = 0;
cin >> T;
while(T--) {
int n = 0;
cin >> n;
vector <vector <int> > Graph(n);
for(int i = 0; i < n; ++i){
int s, c;
cin >> s >> c;
for(int j = 0; j < c; ++j){
int tmp;
cin >> tmp;
Graph[s].push_back(tmp);
}
}
int s, d;
cin >> s >> d;
vector <int> levels(n);
BFS(Graph, levels, s);
cout << s << ' ' << d << ' ' << levels[d] - 1 << endl;
if(T) cout << endl;
}
return 0;
}
void BFS(vector < vector <int> > & Graph, vector <int> & levels, int s)
{
vector <bool> visited(Graph.size(), false);
visited[s] = 1;
queue <int> Queue;
Queue.push(s);
levels[s] = 0;
while(!Queue.empty()){
int front = Queue.front();
Queue.pop();
for (auto a : Graph[front])
if(!visited[a]){
visited[a] = 1;
Queue.push(a);
levels[a] = levels[front] + 1;
}
}
return;
}