-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS.cpp
55 lines (50 loc) · 786 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for(int i =a;i<=b; i++)
const int MX = 2e5 + 3;
int mark[MX], dis[MX];
vi adj[MX];
void BFS(int k);
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
REP(i, 0,m-1)
{
int x, y;
cin>>x>>y;
adj[x].PB(y);
adj[y].PB(x);
}
BFS(0);
REP(i, 0, n) cout<<dis[i]<<endl;
return 0;
}
void BFS(int k)
{
deque<int> dq;
dq.PB(k);
while(dq.size())
{
int temp = dq.front();
dq.pop_front();
mark[temp] = 1;
for(auto x: adj[temp])
{
if(!mark[x])
{
mark[x] = 1;
dq.PB(x);
dis[x] = dis[temp]+1;
}
}
}
}