-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_route.cpp
71 lines (54 loc) · 1.24 KB
/
message_route.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int>> g;
vector<int> dist, parent;
void get_dists() {
parent.assign(n, -1);
dist.assign(n, -1);
dist[0] = 0;
queue<int> q;
q.push(0);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u: g[v])
if (dist[u] == -1) {
q.push(u);
parent[u] = v;
dist[u] = dist[v] + 1;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int m;
cin >> n >> m;
g.assign(n, vector<int>());
while (m--) {
int a, b;
cin >> a >> b;
g[a - 1].push_back(b - 1);
g[b - 1].push_back(a - 1);
}
get_dists();
int ans = dist[n - 1];
if (ans == -1) cout << "IMPOSSIBLE\n";
else {
cout << (ans + 1) << "\n";
int v = n - 1;
vector<int> path;
while (parent[v] != -1) {
path.push_back(v + 1);
v = parent[v];
}
path.push_back(1);
reverse(path.begin(), path.end());
for (int i = 0; i <= ans; i++) {
if (i) cout << " ";
cout << path[i];
}
cout << "\n";
}
}