-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path167.select_nodes.cpp
More file actions
47 lines (38 loc) · 1.07 KB
/
Copy path167.select_nodes.cpp
File metadata and controls
47 lines (38 loc) · 1.07 KB
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
class Solution{
public:
int count=0;
bool dfs(vector<int> adj[],vector<int> &visited,int node){
//mkae node visted
visited[node]=1;
bool select=false;
for(auto i:adj[node]){
if(visited[i]==0){
//call the dfs for the child
bool isChildSelected=dfs(adj,visited,i);
//means if child is not selected than select the parent
if(!isChildSelected){
select=true;
}
}
}
//increment the count if the current node is selected
if(select){
count++;
}
return select;
}
int countVertex(int N, vector<vector<int>>edges){
//making adjacency matrix
vector<int> adj[N+1];
for(auto i:edges){
int v=i[0];
int u=i[1];
adj[v].push_back(u);
adj[u].push_back(v);
}
//visted array
vector<int> visited(N+1,0);
dfs(adj,visited,1);
return count;
}
};