forked from jpcsousa/codejam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.cpp
More file actions
executable file
·39 lines (30 loc) · 696 Bytes
/
dfs.cpp
File metadata and controls
executable file
·39 lines (30 loc) · 696 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
#include<iostream>
#include<queue>
#define MAX 100
using namespace std;
queue<int> myQueue;
int G[MAX][MAX];
int visit[MAX];
int V, E;
void dfs(int s) {
int i, j, node;
memset(visit, 0, sizeof(visit));
myQueue.push(s);
while(!myQueue.empty())
{
node = myQueue.front();
myQueue.pop();
if(visit[node]) continue;
visit[node] = 1;
cout << node << " ";
for(i=0; i<V; i++)
if(G[i][node]) myQueue.push(i);
for(j=0; j<V; j++)
if(G[node][j]) myQueue.push(j);
}
}
int main() {
memset(visit, 0, sizeof(visit));
dfs(0);
return 0;
}