-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageRoute.cpp
More file actions
66 lines (54 loc) · 1.23 KB
/
Copy pathmessageRoute.cpp
File metadata and controls
66 lines (54 loc) · 1.23 KB
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
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100001];
vector<int> visited(100001);
vector<int> dist(100001,0);
vector<int> parent(100001, 1);
int main() {
int n, m;
cin >> n >> m;
for(int i = 0; i <= n; i++) {
g[i].clear();
visited[i] = 0;
dist[i] = -1;
}
for(int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
// bfs
queue<int> qu;
dist[1] = 0;
qu.push(1);
visited[1] = 1;
while(!qu.empty()) {
int x = qu.front();
qu.pop();
for(int i = 0; i < g[x].size(); i++) {
int y = g[x][i];
if(visited[y] == 1) continue;
dist[y] = dist[x]+1;
qu.push(y);
visited[y] = 1;
parent[y] = x;
}
}
if(dist[n] == -1) {
cout << "IMPOSSIBLE\n";
return 0;
}
int node = dist[n]+1;
vector<int> route(node);
route[node-1] = n;
for(int i = node-2; i >= 0; i--) {
route[i] = parent[route[i+1]];
}
cout << node << endl;
for(int i = 0; i < node; i++) {
cout << route[i] << " ";
}
cout << endl;
return 0;
}