-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage Route.cpp
More file actions
executable file
·57 lines (53 loc) · 895 Bytes
/
Copy pathMessage Route.cpp
File metadata and controls
executable file
·57 lines (53 loc) · 895 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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n, m;
vector<int> v[100009];
int odw[100009];
int kol[100009];
queue<int> q;
vector<int> res;
bool bfs() {
q.push(1);
odw[1] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
if (x == n) {
while (x != 0) {
res.push_back(x);
x = kol[x];
}
reverse(res.begin(), res.end());
return true;
}
for (unsigned i = 0; i < v[x].size(); i++) {
if (!odw[v[x][i]]) {
odw[v[x][i]] = odw[x] + 1;
kol[v[x][i]] = x;
q.push(v[x][i]);
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
if (bfs()) {
cout << odw[n] << "\n";
for (int x : res) {
cout << x << " ";
}
cout << "\n";
} else {
cout << "IMPOSSIBLE\n";
}
return 0;
}