-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDFS.cpp
More file actions
49 lines (39 loc) · 702 Bytes
/
DFS.cpp
File metadata and controls
49 lines (39 loc) · 702 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
48
49
#include <bits/stdc++.h>
using namespace std;
#define MAXN 500000
int n , m ;
int visited [MAXN] ;
vector <int> adj_list [MAXN] ;
void dfs (int x)
{
for (int i = 0 ; i < adj_list[x].size() ; i++)
{
int v = adj_list[x][i] ;
if(visited[v] == -1)
{
visited[v] = visited[x] ;
dfs(v) ;
}
}
}
void initialize ()
{
for (int i = 1 ; i <= n ; i++)
{
visited[i] = -1 ;
}
}
int main ()
{
int a , b ;
cin >> n >> m ;
initialize();
for (int i = 1 ; i <= m ; i++)
{
cin >> a >> b ;
adj_list[a].push_back(b) ;
adj_list[b].push_back(a) ;
}
dfs(1) ;
return 0;
}