-
Notifications
You must be signed in to change notification settings - Fork 169
/
Message Route.cpp
49 lines (42 loc) · 942 Bytes
/
Message Route.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
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5+1;
int N, M, K, a, b, p[maxN], dist[maxN];
bool vis[maxN];
vector<int> G[maxN];
queue<int> Q;
int main(){
scanf("%d %d", &N, &M);
for(int i = 0; i < M; i++){
scanf("%d %d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
Q.push(1);
vis[1] = true;
while(!Q.empty()){
int u = Q.front(); Q.pop();
for(int v : G[u]){
if(!vis[v]){
dist[v] = dist[u]+1;
vis[v] = true;
p[v] = u;
Q.push(v);
}
}
}
if(!vis[N]){
printf("IMPOSSIBLE\n");
return 0;
}
int u = N;
K = dist[N];
vector<int> ans(K+1);
for(int i = K; i >= 0; i--){
ans[i] = u;
u = p[u];
}
printf("%d\n", K+1);
for(int i = 0; i <= K; i++)
printf("%d%c", ans[i], (" \n")[i==K]);
}