-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdfs.cpp
More file actions
62 lines (48 loc) · 941 Bytes
/
dfs.cpp
File metadata and controls
62 lines (48 loc) · 941 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
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
const int N=1e5;
vector<int> g[N];
bool vis[N]={0};
void dfs(int a){
for (auto elm : g[a])
{
if(vis[elm]==0){
vis[elm]=1;
dfs(elm);
}
}
}
int main() {
int n,m;
cout<<"enter the value of n: ";
cin>>n;
cout<<"enter the value of m: ";
cin>>m;
for (int i = 0; i <m; i++)
{
int a,b;
cin>>a>>b;
g[a].push_back(b);
}
for (int i = 1; i <=n; i++)
{
cout<<g[i][1]<<"\n";
cout<<i<<"-->";
for(auto var :g[i])
{
cout<<var<<",";
}
cout<<"\n";
}
for (int i = 1; i <=n; i++)
{
if(!vis[i]){
dfs(i);
}
}
for (int i = 1; i <=10; i++)
{
cout<<vis[i]<<" ";
}
return 0;
}