-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2644.cpp
More file actions
38 lines (35 loc) · 853 Bytes
/
2644.cpp
File metadata and controls
38 lines (35 loc) · 853 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
#include <bits/stdc++.h>
using namespace std;
vector<int> family[101];
int N, M, node1, node2;
int cache[101];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
cin >> node1 >> node2;
cin >> M;
for (int i = 0; i < M; i++) {
int parent, child;
cin >> parent >> child;
family[parent].push_back(child);
family[child].push_back(parent);
}
queue<int> Q;
Q.push(node1);
while (!Q.empty()) {
int cur = Q.front();
Q.pop();
if (cur == node2) {
cout << cache[node2];
return 0;
}
for (int i = 0; i < family[cur].size(); i++) {
int next = family[cur][i];
if (cache[next]) continue;
Q.push(next);
cache[next] = cache[cur] + 1;
}
}
cout << -1 << '\n';
}