-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
58 lines (49 loc) · 1.06 KB
/
bfs.cpp
File metadata and controls
58 lines (49 loc) · 1.06 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
#include<bits/stdc++.h>
using namespace std;
vector < vector <int> > adj;
int V,E;
int dist[110];
bool marked[110];
int parent[110];
void bfs(int s)
{
for(int i = 1; i < V+1; ++i){
parent[i] = -1;
dist[i] = 1e9;
marked[i] = 0;
}
dist[s] = 0,marked[s] = 1,parent[s] = -1;
queue < int > Q; Q.push(s);
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(auto v: adj[u])
if(!marked[v]){
parent[v] = u;
dist[v] = dist[u]+1;
marked[v] = 1;
Q.push(v);
}
}
}
int main()
{
cin>>V>>E;
adj.resize(V+10);
for(int i = 0; i < E; ++i){
int x,y; cin>>x>>y;
adj[x].push_back(y);
}
for(int i = 1; i < V+1; ++i){
cout<<i<<'\t';
for(auto v:adj[i])
cout<<v<<' ';
cout<<'\n';
}
bfs(1);
for(int i = 0; i < V+1; ++i)
cout<<dist[i]<<(i==V?'\n':' ');
for(int i = 0; i < V+1; ++i)
cout<<marked[i]<<(i==V?'\n':' ');
return 0;
}