-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2644.cpp
58 lines (42 loc) · 984 Bytes
/
2644.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
#include <bits/stdc++.h>
using namespace std;
vector<int> relation[101];
int n;
int p;
int c;
int m;
bool visited[101];
int bfs(int parent, int child) {
queue<pair<int, int>> q;
visited[parent] = true;
q.push({parent, 0});
while (!q.empty()) {
int qfir = q.front().first;
int qscnd = q.front().second;
q.pop();
if (qfir == child) {
cout << qscnd;
return 0;
}
for (int i = 0; i < relation[qfir].size(); i++) {
int next = relation[qfir][i];
if (!visited[next]) {
visited[next] = true;
q.push({next, qscnd+1});
}
}
}
cout << -1;
return -1;
}
int main() {
cin >> n >> p >> c >> m;
for (int i = 0; i < m; i++) {
int temp1, temp2;
cin >> temp1 >> temp2;
relation[temp1].push_back(temp2);
relation[temp2].push_back(temp1);
}
bfs(p,c);
return 0;
}