-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs with adsacent matrix.cpp
53 lines (50 loc) · 1.05 KB
/
dfs with adsacent matrix.cpp
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
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int edges[10000][10000];
bool visited[10000];
void dfs(int n, int sv)
{
cout<<sv<<endl;
visited[sv]=true;
for(int i=1;i<=n;i++){
if(i==sv){
continue;
}
if(edges[sv][i]==1){
if(visited[i]){
continue;
}
dfs(n,i);
}
}
}
int main()
{
int n;
int e;
cin>>n>>e;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
edges[i][j]=0;
}
}
for(int i=1;i<=e;i++){
int f,s;
cin>>f>>s;
edges[f][s]=1;
edges[s][f]=1;
}
for(int i=1;i<=n;i++){
visited[i]=false;
}
cout<<"printing dfs"<<endl;
dfs(n,1);
/*cout<<"Enter a range to find it has a path or not"<<endl;
int l,r;
cin>>l>>r;
dfs(n,l);
if(visited[r]==1)cout<<"Yes"<<endl;
else cout<<"No"<<endl;*/
return 0;
}