forked from strang3-r/Leetcode75
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.cpp
More file actions
32 lines (30 loc) · 670 Bytes
/
DFS.cpp
File metadata and controls
32 lines (30 loc) · 670 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
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int>> &graph, int source, vector<bool> &vis)
{
cout << source << " ";
vis[source] = true;
for (int neigh : graph[source])
if (!vis[neigh])
dfs(graph, neigh, vis);
}
int main()
{
int n, e;
cin >> n >> e;
vector<vector<int>> graph(n);
vector<bool> vis(n, false);
for (int i = 0; i < e; i++)
{
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for (int i = 0; i < n; i++)
{
if (!vis[i])
dfs(graph, i, vis);
}
return 0;
}