-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDivisibilityTree.java
More file actions
40 lines (28 loc) · 840 Bytes
/
Copy pathDivisibilityTree.java
File metadata and controls
40 lines (28 loc) · 840 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
class Solution {
int ans;
public int minimumEdgeRemove(int n, int[][] edges) {
List<List<Integer>> list = new ArrayList<>();
for(int i=0;i<=n;i++) list.add(new ArrayList<>());
for(int e[]:edges){
list.get(e[0]).add(e[1]);
list.get(e[1]).add(e[0]);
}
ans=0;
no_of_nodes(1,list,new boolean[n+1]);
return ans;
}
int no_of_nodes(int i,List<List<Integer>> list,boolean vis[]){
vis[i] = true;
int non=1;
for(Integer nbr:list.get(i)){
if(!vis[nbr]){
non+=no_of_nodes(nbr,list,vis);
}
}
if(non%2==0){
if(i>1) ans++;
non=0;
}
return non;
}
}