-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.cpp
More file actions
47 lines (42 loc) · 660 Bytes
/
DFS.cpp
File metadata and controls
47 lines (42 loc) · 660 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
36
37
38
39
40
41
42
43
44
45
46
47
#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;
void DFS(int k);
vi adj[MX];
int mark[MX], dis[MX];
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int m,n;
cin>> n>>m;
REP(i, 0, m-1)
{
int x, y;
cin>>x>>y;
adj[x].PB(y);
adj[y].PB(x);
}
DFS(0);
REP(i, 0, n) cout<<dis[i]<<endl;
return 0;
}
void DFS(int k)
{
mark[k] = 1;
for(auto x: adj[k])
{
if(!mark[x])
{
dis[x] = dis[k]+1;
DFS(x);
}
}
}