-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
72 lines (65 loc) · 988 Bytes
/
bfs.cpp
File metadata and controls
72 lines (65 loc) · 988 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// input
// 6 5
// 1 2
// 2 3
// 3 4
// 4 5
// 5 6
// 6
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// @author: __Taohid
const int N = 105;
vector<int>g[N];
bool vis[N];
int dis[N];
int par[N];
void bfs(int vertex) {
queue<int>q;
q.push(vertex);
vis[vertex] = 1;
while (!q.empty()) {
int cur_v = q.front();
q.pop();
for (auto v : g[cur_v]) {
if (!vis[v]) {
q.push(v);
par[v] = cur_v;
vis[v] = 1;
dis[v] = dis[cur_v] + 1;
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
while (m--) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
bfs(1);
// shortest path
for (int i = 1; i <= n; i++) {
cout << dis[i] << '\n';
}
// path printing
int x;
cin >> x;
vector<int>path;
path.push_back(x);
while (par[x]) {
path.push_back(par[x]);
x = par[x];
}
for (int i = path.size() - 1; i >= 0; i--) {
cout << path[i] << ' ';
}
cout << '\n';
return 0;
}