-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.cpp
More file actions
60 lines (54 loc) · 1.11 KB
/
BFS.cpp
File metadata and controls
60 lines (54 loc) · 1.11 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
59
60
#include<bits/stdc++.h>
using namespace std;
#define WHITE 1
#define GRAY 2
#define BLACK 3
#define size 100
int adj[size][size], color[size],d[size], parent[size];
int vertex,edge;
void BFS(int s){
for(int u=0; u<=vertex; u++)
{
color[u] = WHITE;
d[u] = INT_MAX;
parent[u] = -1;
}
d[s]= 0;
parent[s] = -1;
queue<int> q;
q.push(s);
while(!q.empty())
{
int u;
u = q.front();
color[u] = GRAY;
q.pop();
cout<<u<<" ";
for(int v=0;v<vertex;v++)
{
if(adj[u][v] == 1)
{
if(color[v] == WHITE)
{
q.push(v);
d[v] = d[u] + 1;
parent[v] = u;
}
}
}
}
}
int main(){
freopen("bfsin.txt","r",stdin);
cin>>vertex>>edge;
int v1,v2;
for(int i=0; i<edge; i++){
cin>>v1>>v2;
adj[v1][v2] = 1;
}
BFS(0);
cout<<endl;
cout<<parent[2]<<endl;
cout<<d[5];
return 0;
}