forked from sahilbansalweb/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.cpp
More file actions
25 lines (21 loc) · 650 Bytes
/
DFS.cpp
File metadata and controls
25 lines (21 loc) · 650 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
>> DFS
>>> Resources:
Tutorial : https://cp-algorithms.com/graph/depth-first-search.html
>>> Implementation:
const int N=200005;
bool vis[N];
int subtree[N];
vector<int> adj[N];
void dfs(int v)
{
vis[v]=true;
subtree[v]=1;
for(int u:adj[v])
{
if(!vis[u])
{
dfs(u);
subtree[v]+=subtree[u];
}
}
}