-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS_.cpp
37 lines (32 loc) · 935 Bytes
/
BFS_.cpp
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
#include<bits/stdc++.h>
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
vector<int>BfsOfGrapgh(int v, vector<int> adj[])
{
vector<int>bfs;
vector<int>vis(v+1,0);
for (int i = 1; i <=v; i++ )
{
if(!vis[i]){
queue<int>q;
q.push(i);
vis[i]=1;
while(!q.empty()){
int node=q.front();
q.pop();
bfs.push_back(node);
for(auto it: adj[node]){
if(!vis[it]){
q.push(it);
vis[it]=1;
}
}
}
}
}
return bfs;
}
};