-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectingElements_graph.java
55 lines (46 loc) · 1.63 KB
/
ConnectingElements_graph.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class ConnectingElements_graph {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int vertex = Integer.parseInt(st.nextToken());
int edge = Integer.parseInt(st.nextToken());
int[] array = new int[vertex+1];
while (edge-->0){
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
if (array[start]==0 && array[end]==0){
array[start] = start;
array[end] = start;
}
else if (array[start]==0){
array[start] = array[end];
}
else if (array[end]==0){
array[end] = array[start];
}
else if (array[start]!=array[end]){
int tmp = array[end];
for (int i=1; i<=vertex; i++) {
if (array[i] == tmp)
array[i] = array[start];
}
}
}
for (int i=1; i<=vertex; i++){
if (array[i]!=0 && array[i]!=-1){
array[array[i]] = -1;
}
}
int count = 0;
for (int i=1; i<=vertex; i++){
if (array[i]==-1 || array[i]==0) count++;
}
System.out.println(count);
}
}