-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph1.java
49 lines (43 loc) · 972 Bytes
/
Graph1.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
package dummypkg;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
public class Graph1 {
static boolean visited[];
static HashSet<Integer> al[];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int N=sc.nextInt(), M=sc.nextInt();
al=new HashSet[N+1];
visited=new boolean[N+1];
for(int i=0; i<M; i++) {
int x=sc.nextInt(), y=sc.nextInt();
if(x!=y) {
if(al[x]==null)
al[x]=new HashSet<>();
if(al[y]==null)
al[y]=new HashSet<>();
al[x].add(y);
al[y].add(x);
}
}
dfs(sc.nextInt());
int count=0;
for(int i=1; i<=N; i++) {
if(visited[i]==false)
count++;
}
System.out.println(count);
}
public static void dfs(int st) {
visited[st]=true;
if(al[st]!=null)
for(int i:al[st]) {
if(visited[i]==false) {
dfs(i);
}
}
}
}