-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.cpp
49 lines (38 loc) · 815 Bytes
/
dfs.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
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#define MAX 100000
#define UNVISITED -1
using namespace std;
int component[MAX];
vector<int> grafo[MAX];
/**
*
* Graph is unweighted,
* or all distances are one between two vertices adjacent.
*
*/
// Deep First Search
void dfs(int v)
{
// For every neighboor of v
for(int i = 0; i < grafo[v].size(); ++i)
{
if(component[grafo[v][i]] == -1)
{
// mark as visited
component[grafo[v][i]] = component[v];
// explore neighboors
dfs(grafo[v][i]);
}
}
}
int main() {
int v = 0;
// mark all nodes with unvisited
memset(component, UNVISITED, sizeof component);
component[v] = v;
dfs(v);
return 0;
}