-
Notifications
You must be signed in to change notification settings - Fork 126
/
BFS.cpp
36 lines (28 loc) · 781 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
>> BFS
>>> Reources:
T-1: https://cp-algorithms.com/graph/breadth-first-search.html
>>> Implementation:
const int N=200000;
bool vis[N];
int level[N];
vector<int> adj[N];
void bfs(int v)
{
queue<int> q;
q.push(v);
vis[v]=1;
while(!q.empty())
{
int val=q.front();
q.pop();
for(auto i:adj[val])
{
if(!vis[i])
{
q.push(i);
level[i]=level[val]+1;
vis[i]=1;
}
}
}
}