-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs to find depth of node.cpp
More file actions
58 lines (51 loc) · 963 Bytes
/
dfs to find depth of node.cpp
File metadata and controls
58 lines (51 loc) · 963 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
#define ll long long
#define vll vector<ll>
#define pll pair<ll,ll>
#define pb push_back
using namespace std;
vll adj[2001];
bool vis[2001];
void dfs(ll curr,ll cnt)
{
vis[curr]=true;
cout<<curr<<" : "<<cnt<<endl;
for(ll i=0 ; i<adj[curr].size() ; i++)
{
ll node=adj[curr][i];
if(!vis[node])
{
dfs(node,cnt+1);
}
}
}
void addEdge(ll a,ll b)
{
adj[a].pb(b);
}
void initialize()
{
for(ll i=0 ; i<=2000 ; i++)
vis[i]=false;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);cout.tie(NULL);
initialize();
addEdge(1,2);
addEdge(1,3);
addEdge(2,4);
addEdge(2,5);
addEdge(2,6);
addEdge(3,9);
addEdge(5,7);
addEdge(5,8);
addEdge(9,10);
addEdge(9,11);
addEdge(10,12);
addEdge(10,13);
addEdge(10,14);
dfs(1,0);
return 0;
}