-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBfs.cpp
More file actions
63 lines (55 loc) · 1.14 KB
/
Bfs.cpp
File metadata and controls
63 lines (55 loc) · 1.14 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
61
62
63
#include<bits/stdc++.h>
#define mx 10000
using namespace std;
int node,edge;
vector<int>v[mx];
bool b[mx];
memset(b,false,sizeof b);
queue<int>q;
void BFS ()
{
for(int i=1; i<=node; i++)
{
if(!b[i])
{
q.push(i);
b[i]=true;
while( ! q.empty() )
{
int f=q.front();
q.pop();
for(int j=0; j<=v[f].size(); j++)
{
if(!b[j])
{
q.push(j);
b[j]=true;
}
}
}
}
}
}
int main()
{
scanf("%d%d",&node,&edge);
memset(b,false,sizeof b);
for(int i=1; i<=edge; i++)
{
int x,y;
scanf("%d%d",&x,&y);
v[x].push_back(y);
}
printf("''-------The Graph is--------''\n\n");
for(int i=1; i<=node; i++)
{
printf("%d-----> ",i);
for(int j=0; j<v[i].size(); j++)
{
printf("%d ",v[i][j]);
}
printf("\n");
}
BFS();
return 0;
}