-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageRoute.cpp
More file actions
67 lines (52 loc) · 1.21 KB
/
MessageRoute.cpp
File metadata and controls
67 lines (52 loc) · 1.21 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
67
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
vector<bool>visited(N,false);
vector<int> adj [N];
vector<long long> dist(N);
vector<int>parent(N);
void BFS(int n){
dist[n] = 0;
queue<int>q;
q.push(n);
while(!q.empty()){
int current = q.front();
q.pop();
for(int next : adj[current]){
if(dist[next] == LONG_LONG_MAX){
dist[next] = dist[current] + 1;
q.push(next);
parent[next] = current;
}
}
}
}
int main(){
int n,m;
cin >> n >> m;
for(int i = 0 ; i < m ; i ++){
int x,y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 1 ; i <= n ; i ++){
dist[i] = LONG_LONG_MAX;
}
BFS(1);
if(dist[n] == LONG_LONG_MAX)cout << "IMPOSSIBLE";
else{
cout << dist[n] + 1 << endl ;
int ans[dist[n]+1];
ans[dist[n]] = n;
int i = parent[n];
int z = dist[n] - 1;
while(i != 1){
ans[z--] = i;
i = parent[i];
}
ans[0] = 1;
for(int i = 0 ; i <= dist[n] ; i ++)cout << ans[i] << " ";
}
return 0;
}