-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.cpp
63 lines (49 loc) · 1.21 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
const int maxn = 1e5 + 8;
ll n, m;
int main() {
cin >> n >> m;
vector<set<int>> adj(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
adj[a].insert(b);
adj[b].insert(a);
}
// bfs
queue<int> q;
int parent[maxn];
int dist[maxn];
memset(dist, 0x3f3f3f3f, sizeof(dist));
q.push(0);
while (!q.empty()) {
int top = q.front();
q.pop();
if (top == n - 1) {
vector<int> path;
for (int curr = top; curr != 0; curr = parent[curr]) {
path.push_back(curr);
}
path.push_back(0);
reverse(path.begin(), path.end());
cout << path.size() << endl;
for (int i : path) cout << i + 1 << " ";
cout << endl;
return 0;
}
for (int i : adj[top]) {
if (dist[i] == 0x3f3f3f3f) {
parent[i] = top;
dist[i] = dist[top] + 1;
q.push(i);
}
}
}
cout << "IMPOSSIBLE" << endl;
return 0;
}