-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage Route.cpp
More file actions
50 lines (42 loc) · 1023 Bytes
/
Message Route.cpp
File metadata and controls
50 lines (42 loc) · 1023 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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
using vi = vector<int>;
#define pb push_back
void solve(){
int N, M; cin >> N >> M;
vi dist(N+1,INT_MAX), parent(N+1);
vector<vi> adj(N+1);
while (M--) {
int a,b; cin >> a >> b;
adj[a].pb(b), adj[b].pb(a);
}
queue<int> q;
dist[1] = 0; q.push(1);
while (!q.empty()) {
int x = q.front(); q.pop();
for (int t: adj[x]) if (dist[t] == INT_MAX) {
dist[t] = dist[x]+1; parent[t] = x;
q.push(t);
}
}
if (dist[N] == INT_MAX) cout << "IMPOSSIBLE";
else {
cout << dist[N]+1 << "\n";
vi v{N}; while (v.back() != 1) v.pb(parent[v.back()]);
reverse(begin(v), end(v));
for (int t: v) cout << t << " ";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//int t;
//cin>>t;
//while(t--)
solve();
return 0;
}