-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.cpp
More file actions
45 lines (39 loc) · 873 Bytes
/
DFS.cpp
File metadata and controls
45 lines (39 loc) · 873 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
*/author>> Hridoy Saha<<*/
* GRAPH REPRESENTATIONS using STL (DFS)
* @author Amirul Islam (shiningflash)
* *
* creates adjacency list first
* DFS of Unweighted and Undirected
*********************************************/
#include <bits/stdc++.h>
using namespace std;
int n, e, u, v;
vector <vector<int> > g;
vector <bool> vis;
void dfs_visit(int u) {
vis[u] = 1;
printf("%d ", u);
for (int v = 0; v < g[u].size(); v++)
if (!vis[g[u][v]]) dfs_visit(g[u][v]);
}
int main() {
scanf("%d %d", &n, &e);
vis.assign(n, 0);
g.assign(n, vector<int>());
while (e--) scanf("%d %d", &u, &v), g[u].push_back(v), g[v].push_back(u);
for (int i = 0; i < n; i++) if (!vis[i]) dfs_visit(i);
puts("");
}
/******************
INPUT
---
- 1st line, 6 Vertex, 5 Edge
6 5
0 1
1 2
2 4
3 4
OUTPUT:
-----
0 1 5 2 4 3
*****************/