-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageRoute.cpp
48 lines (44 loc) · 980 Bytes
/
MessageRoute.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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN =1e5;
int n, m, p[mxN];
vector<int> adj[mxN], ans;
int main(){
cin >> n >> m;
for(int i=0, a,b; i<m; ++i){
cin >> a >> b, --a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
memset(p, -1, 4*n);
queue<int> qu;
qu.push(0);
p[0]=-2;
while(qu.size()){
int u = qu.front();
qu.pop();
for(int v: adj[u]){
if(p[v] < 0){
p[v] = u;
qu.push(v);
}
}
}
if(p[n-1] < 0){
cout << "IMPOSSIBLE";
} else{
int u = n-1;
while(u) {
ans.push_back(u);
u = p[u];
}
ans.push_back(0);
reverse(ans.begin(), ans.end());
cout << ans.size() << "\n";
for(int a: ans){
cout << a+1 << " ";
}
}
}