Repository navigation
Expand file tree
/
Copy pathReligions.java
More file actions
60 lines (52 loc) · 1.7 KB
/
Copy pathReligions.java
File metadata and controls
60 lines (52 loc) · 1.7 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
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.text.*;
import java.util.*;
import java.math.*;
import java.io.*;
import java.lang.*;
import java.lang.reflect.Array;
public class Religions {
public static void main (String[] args)
{
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
try
{
String s = bufferRead.readLine();
int round = 1;
String[] line;
int m;
int n;
int[] parent;
int pa;
int pb;
int a;
int b;
while(!s.equals("0 0")) {
line = s.split(" ");
m = Integer.parseInt(line[0]);
n = Integer.parseInt(line[1]);
parent = new int[m + 1];
for (int i = 0; i <= m; i++)
parent[i] = i;
for(int i = 0; i < n; i++) {
s = bufferRead.readLine();
line = s.split(" ");
a = Integer.parseInt(line[0]);
b = Integer.parseInt(line[1]);
pa = a;
pb = b;
while(parent[pa] != pa)
pa = parent[pa];
while(parent[pb] != pb)
pb = parent[pb];
if(pb != pa) {
m--;
parent[pa] = pb;
}
}
System.out.println("Case " + round + ": "+ m);
round++;
s = bufferRead.readLine();
}
} catch (IOException e) { e.printStackTrace();}
}
}