-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
50 lines (44 loc) · 848 Bytes
/
bfs.cpp
File metadata and controls
50 lines (44 loc) · 848 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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int N = 1e5;
vector<int> adj[N];
queue<int> q;
bool vis[N];
int dist[N];
void bfs(int u) {
vis[u] = true;
dist[u] = 0;
q.push(u);
while (!q.empty()) {
u = q.front(); q.pop();
for (int v : adj[u]) {
if (!vis[v]) {
vis[v] = true;
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
adj[1].push_back(2);
adj[1].push_back(4);
adj[1].push_back(3);
adj[3].push_back(1);
adj[3].push_back(4);
adj[4].push_back(3);
adj[4].push_back(1);
adj[4].push_back(2);
adj[4].push_back(5);
adj[2].push_back(1);
adj[2].push_back(4);
adj[2].push_back(5);
adj[5].push_back(2);
adj[5].push_back(4);
bfs(1);
for (int i = 1; i <= 5; i++) cout << "dist[" << i << "]: " << dist[i] << "\n";
}