-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.cpp
More file actions
40 lines (37 loc) · 699 Bytes
/
Copy pathdfs.cpp
File metadata and controls
40 lines (37 loc) · 699 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
#include <bits/stdc++.h>
using namespace std ;
#define inf 10000000
vector<vector<int> > adj ;
vector<bool> visit ;
void dfs(int node)
{
visit[node]=true;
for(auto to:adj[node])
{
if(visit[to]==false)
dfs(to);
}
}
int main()
{
int n, m, u, v ;
cin >> n >> m ;
adj.resize(n+1) ;
visit.assign(n+1, false) ;
for (int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
adj[x - 1].push_back(y - 1);
adj[y - 1].push_back(x - 1);
}
cin>>u;
dfs(u-1);
for(int i=0;i<=n;i++)
{
if(visit[i]==1)
{
cout << i+1 << " is reachable from u"<<endl;
}
}
}