Tuesday, July 30, 2019

DFS Tutorial | Competitive Programming


Depth First Search (DFS) for a Graph
Basics:
Problems:
(1)   Unreachable Nodes (Hackerearth).- see bottom of the page
(5)   Vjudge: 1.
(6)   DFS variation: Lightoj. A2OJ.
// DFS.cpp
/// (Data Structure: graph, visit)
vector<int> v[30001];
int vis[30001], c=0;
void dfs(int s)
{
    if(vis[s])
        return ;
    vis[s]=1;
    c++;
    for(int i=0;i<v[s].size();i++)
    {
        if(!vis[v[s][i]])
        {
            dfs(v[s][i]);
        }
    }
}

No comments:

Post a Comment