forked from b-chae/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2644.cpp
More file actions
51 lines (44 loc) · 753 Bytes
/
Copy path2644.cpp
File metadata and controls
51 lines (44 loc) · 753 Bytes
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
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int N;
int A, B;
vector<int> relatives[100];
int b[100];
bool visited[100];
list<int> bfsQueue;
int ans = -1;
void bfs() {
if (bfsQueue.empty()) return;
int x = bfsQueue.front(); bfsQueue.pop_front();
for (int r : relatives[x]) {
if (!visited[r]) {
visited[r] = true;
if (r == B - 1) {
ans = b[x] + 1;
return;
}
else {
b[r] = b[x] + 1;
bfsQueue.push_back(r);
}
}
}
bfs();
}
int main()
{
cin >> N >> A >> B;
int i, n, x, y;
cin >> n;
for (i = 0; i < n; i++) {
cin >> x >> y;
relatives[x - 1].push_back(y - 1);
relatives[y - 1].push_back(x - 1);
}
bfsQueue.push_back(A-1);
visited[A - 1] = true;
bfs();
cout << ans;
}