Depth First Search (DFS) for a Graph
Basics:
Problems:
(1) Unreachable
Nodes (Hackerearth).- see bottom of
the page
(2) 280
- Vertex (UVa).
(4) 10452
– Marcus (UVa).
// 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