Preview:
#include<bits/stdc++.h>

void mkadj(unordered_map<int, set<int>> &mp, vector<vector<int>> &edges)
{
    for(int i=0;i<edges.size();i++)
    {
        int u = edges[i][0];
        int v = edges[i][1];

        mp[u].insert(v);
        mp[v].insert(u);
    }
}

void dfs(unordered_map<int, set<int>> &mp, unordered_map<int, bool> &visited, vector<int> &comp, int node)
{
    comp.push_back(node);
    visited[node]=true;
    for(auto x:mp[node])
    {
        if(!visited[x]) dfs(mp, visited, comp, x);
    }
}

vector<vector<int>> depthFirstSearch(int V, int E, vector<vector<int>> &edges)
{
    unordered_map<int, set<int>> mp;
    unordered_map<int, bool> visited;
    mkadj(mp, edges);
   
    vector<vector<int>> ans;
    
    for(int i=0;i<V;i++)
    {
        if(!visited[i])
        {
            vector<int> comp;
            dfs(mp, visited, comp, i);
            ans.push_back(comp);
        }

    }
    return ans;
    // Write your code here
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter