-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP2644.cpp
51 lines (38 loc) · 894 Bytes
/
P2644.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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> graph[101] = {};
int check[101] = {};
void bfs(int start) {
queue<int> q;
q.push(start);
check[start] = 1;
while(!q.empty()) {
int front = q.front();
q.pop();
for(int i = 0; i < graph[front].size(); i++) {
int node = graph[front][i];
if(check[node] != 0) continue;
check[node] += check[front] + 1;
q.push(node);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, x, y, m;
cin >> n >> x >> y >> m;
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
bfs(x);
if(check[y] == 0) cout << "-1";
else cout << check[y] - 1;
return 0;
}