-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDFS(Vectors).cpp
More file actions
42 lines (39 loc) · 789 Bytes
/
DFS(Vectors).cpp
File metadata and controls
42 lines (39 loc) · 789 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
#include<bits/stdc++.h>
using namespace std;
vector <int> graph[100];
int n,m,v,u;
bool visited[100];
void DFS(int p)
{
for(int i = 0; i < n; i ++)
visited[i] = false;
stack <int> s;
s.push(p);
while(!s.empty())
{
int curr = s.top();
cout<<curr<<" ";
visited[curr] = true;
s.pop();
for(int i = 0; i < graph[curr].size(); i ++)
{
if(!visited[graph[curr][i]])
{
s.push(graph[curr][i]);
visited[graph[curr][i]] = true;
}
}
}
}
int main()
{
scanf(" %d %d", &n, &m);
for(int i = 0; i < m; i ++)
{
scanf(" %d %d", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
}
DFS(2);
return 0;
}