-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1667.cpp
83 lines (76 loc) · 1.44 KB
/
1667.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
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<cstdio>
#include<set>
#include<unordered_set>
#include<cmath>
#include<algorithm>
#include<functional>
#include<utility>
#include<cstdlib>
using namespace std;
typedef long long int lli;
typedef size_t idx;
#define vi vector<int>
#define pb(n) push_back(n)
#define ln "\n"
#define sp ends
#define newline cout << ln
#define fastios ios_base::sync_with_stdio(false); cin.tie(0)
// cses : Message Route - graph
vector<vi> gp;
void bfs_route(int n){
queue<int> q;
vector<bool> vis(n, false);
q.push(1);
vis[1] = true;
vector<int> level(n);
level[1] = 0;
vector<vi> parent(n);
while(!q.empty()){
int u = q.front();
for(int i = 0; i < gp[u].size(); ++i){
int v = gp[u][i];
// parent[v].pb(u);
if(!vis[v]){
parent[v].pb(u);
if(v == n - 1){
cout << level[u] + 2 << ln;
vi net;
net.pb(n - 1);
for(int i = n - 1; i != 1; i = parent[i][0]){
net.pb(parent[i][0]);
}
for(auto it = net.rbegin(); it != net.rend(); ++it){
cout << *it << " ";
}
newline;
return;
}
vis[v] = true;
q.push(v);
level[v] = level[u] + 1;
}
}
q.pop();
}
cout << "IMPOSSIBLE\n";
return ;
}
int main(){
fastios;
int n, m, a, b;
cin >> n >> m;
gp.resize(n + 1);
for(int i = 0; i < m; ++i){
cin >> a >> b;
gp[a].pb(b);
gp[b].pb(a);
}
bfs_route(n + 1);
return 0;
}