-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
35 lines (34 loc) · 761 Bytes
/
bfs.cpp
File metadata and controls
35 lines (34 loc) · 761 Bytes
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
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
vector<int>adj[N];
bool visited[N];
int level[N];
void bfs(int node){
queue<int>q;
q.push(node);
visited[node]=true;
while(!q.empty()){
int current_vertex=q.front();
q.pop();
for(auto child : adj[current_vertex]){
if(!visited[child]){
q.push(child);
visited[child]=true;
level[child]=level[current_vertex]+1;
}
}
}
}
int main(){
int n;
cin>>n;
memset(visited,false,sizeof(visited));
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
bfs(1);
}