-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2644.cpp
More file actions
58 lines (41 loc) · 854 Bytes
/
2644.cpp
File metadata and controls
58 lines (41 loc) · 854 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
52
53
54
55
56
57
58
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<cstring>
#include<math.h>
#include<queue>
#include<vector>
using namespace std;
vector<int>adj[101];
int visited[101];
int main(void)
{
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n; cin >> n;
int find1, find2;
cin >> find1 >> find2;
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
queue<int> q;
q.push(find1);
visited[find1] = 1;
while (!q.empty()) {
int curr = q.front(); q.pop();
if (curr == find2) {
cout << visited[find2] - 1;
return 0;
}
for (auto next : adj[curr]) {
if (visited[next])continue;
visited[next] = visited[curr] + 1;
q.push(next);
}
}
cout << "-1";
}