-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23f6f92
commit 662b11a
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//{ Driver Code Starts | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
class Solution { | ||
public: | ||
// Function to return a list containing the DFS traversal of the graph. | ||
vector<int>ans; | ||
void dfs(int node, vector<bool>&vis, vector<int>adj[],int V){ | ||
vis[node]=true; | ||
ans.push_back(node); | ||
for(auto nbr:adj[node]){ | ||
if(!vis[nbr]){ | ||
dfs(nbr,vis,adj,V); | ||
} | ||
} | ||
} | ||
vector<int> dfsOfGraph(int V, vector<int> adj[]) { | ||
// Code here | ||
vector<bool>vis(V+1,false); | ||
//vis[0]=true; | ||
for(int i=0;i<V;i++){ | ||
if(!vis[i]){ | ||
dfs(i,vis,adj,V); | ||
} | ||
} | ||
return ans; | ||
} | ||
}; | ||
|
||
//{ Driver Code Starts. | ||
int main() { | ||
int tc; | ||
cin >> tc; | ||
while (tc--) { | ||
int V, E; | ||
cin >> V >> E; | ||
|
||
vector<int> adj[V]; | ||
|
||
for (int i = 0; i < E; i++) { | ||
int u, v; | ||
cin >> u >> v; | ||
adj[u].push_back(v); | ||
adj[v].push_back(u); | ||
} | ||
// string s1; | ||
// cin>>s1; | ||
Solution obj; | ||
vector<int> ans = obj.dfsOfGraph(V, adj); | ||
for (int i = 0; i < ans.size(); i++) { | ||
cout << ans[i] << " "; | ||
} | ||
cout << endl; | ||
} | ||
return 0; | ||
} | ||
// } Driver Code Ends |