-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblemA.java
50 lines (47 loc) · 1.34 KB
/
ProblemA.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
import java.util.*;
public class ProblemA{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int caseN = 1;
while(true){
int numNodes = sc.nextInt();
int numOps = sc.nextInt();
if(numNodes == 0 && numOps == 0){ break; }
System.out.println("Case " + caseN++ + ":");
int nodes[] = new int[numNodes];
for(int i = 0; i < numNodes; i++){ nodes[i] = -1; }
for(int i = 0; i < numOps; i++){
sc.nextLine();
char command = sc.next().charAt(0);
if(command == '+'){
join(sc.nextInt(),sc.nextInt(),nodes);
}else if(command == '?'){
System.out.println(checkTree(sc.nextInt(),sc.nextInt(),nodes));
}else{
System.out.println("Invalid command: '" + command + "'");
}
}
}
}
public static void join(int a, int b, int[] uptree){
if(a == b){
//do nothing
} else if(uptree[a] < 0 && uptree[b] < 0){
uptree[a] += uptree[b];
uptree[b] = a;
} else if(uptree[a] > -1 && uptree[b] > -1){
join(uptree[a],uptree[b],uptree);
} else if(uptree[a] > -1){
join(uptree[a],b,uptree);
} else if(uptree[b] > -1){
join(uptree[b],a,uptree);
}
}
public static boolean checkTree(int a, int b, int[] uptree){
return root(a,uptree) == root(b,uptree);
}
public static int root(int a, int[] uptree){
if(uptree[a] > -1){ return root(uptree[a], uptree);}
else{ return a; }
}
}