-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30jan2023.java
68 lines (47 loc) · 1.46 KB
/
30jan2023.java
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
59
60
61
62
63
64
65
66
67
68
public int countVertex(int N, int[][] edges){
// code here
ArrayList<ArrayList<Integer>> arr= new ArrayList<>();
for(int i=0;i<=N;i++)
{
arr.add(new ArrayList<Integer>());
}
for(int i=0;i<edges.length;i++)
{
arr.get(edges[i][0]).add(edges[i][1]);
arr.get(edges[i][1]).add(edges[i][0]);
}
boolean[] visited = new boolean[N+1];
ans=0;
dfs(1,1,arr,visited);
return ans;
}
static int dfs(int v , int p , ArrayList<ArrayList<Integer>> arr,boolean[] visited)
{
boolean is =false;
visited[v]=true;
int im=0;
int io=0;
int iz=0;
for(int i=0;i<arr.get(v).size();i++)
{
int a= arr.get(v).get(i);
if(a==p || visited[a]==true) continue;
is=true;
if(dfs(a,v,arr,visited)==1)
{
io++;
}else if(dfs(a,v,arr,visited)==-1){
im++;
}else{
iz++;
}
}
if(is==false) return -1;
if(im>0)
{
ans++;
return 1;
}
if(io>0) return 0;
return -1;
}