-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageRoute.cpp
More file actions
73 lines (63 loc) · 1.08 KB
/
Copy pathMessageRoute.cpp
File metadata and controls
73 lines (63 loc) · 1.08 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
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
int n, m;
const int Sz = 1e5;
vector <int> a[Sz + 1];
void Read()
{
cin >> n >> m;
int u, v;
for(int i = 1; i <= m; i++)
{
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
}
int pre[Sz + 1];
int d[Sz + 1];
void BFS(int x)
{
queue <int> q;
d[x] = 1;
q.push(x);
int u;
while(q.size())
{
u = q.front();
q.pop();
for(int v : a[u])
{
if(d[v]) continue;
d[v] = 1;
q.push(v);
pre[v] = u;
}
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Read();
BFS(1);
if(!d[n])
{
cout << "IMPOSSIBLE";
return 0;
}
int tmp = n;
vector <int> res;
while(tmp != 1)
{
res.push_back(tmp);
tmp = pre[tmp];
}
res.push_back(tmp);
reverse(res.begin(), res.end());
cout << res.size() << "\n";
for(int i : res) cout << i << ' ';
}