-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdfs.cpp
More file actions
58 lines (47 loc) · 939 Bytes
/
dfs.cpp
File metadata and controls
58 lines (47 loc) · 939 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<iostream>
#include<vector>
using namespace std;
/*each first line of input has n node and m edges
corresponding m lines contain node edge relation and llast line contains the head node
*/
// this program prints a single integer denoting the number of nodes that are unreachable from the given head node
vector <int> g[100005];
bool visited[100005];
int c=0;
void initialize()
{
for(int i = 0; i<100005; i++){
visited[i] = false;
}
}
void dfs(int a)
{
visited[a] = true;
c++;
int x = g[a].size();
for(int i = 0; i < x; i++)
{
int x1 = g[a][i];
if(visited[x1] == false){
dfs(x1);
}
}
}
int main()
{
int n, m;
cin>>n>>m;
for(int i = 0; i < m; i++)
{
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
int x;
cin>>x;
initialize();
dfs(x);
cout<<n-c;
return 0;
}