/
FamilyTree.java
166 lines (159 loc) · 4.98 KB
/
FamilyTree.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.io.*;
import java.util.*;
public class FamilyTree {
static HashMap<String, ArrayList<String>> mothers = new HashMap<>();
public static void main(String[] args) throws Exception {
// Great-Great-GrandMother: great-great aunt, great-great grandmother
// Great-Great-GrandMother
//
//
//
Scanner scan = new Scanner(new BufferedReader(new FileReader("family.in")));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("family.out")));
int n = scan.nextInt();
String cow1 = scan.next();
String cow2 = scan.next();
for (int i = 0; i < n; i++) {
String first = scan.next();
String second = scan.next();
if (mothers.containsKey(first)) {
mothers.get(first).add(second);
}
else {
ArrayList<String> tempList = new ArrayList<>();
tempList.add(second);
mothers.put(first, tempList);
}
}
// cow1 gm of cow2
// cow2 that's a grandmother of cow1
// check if cow1 and cow2 are siblings
if (mother(cow1).equals(mother(cow2))) {
pw.println("SIBLINGS");
pw.close();
scan.close();
return;
}
// check if cow1 is direct descendent of cow2
int num1 = isDirectDescendant(cow1, cow2);
int num2 = isDirectDescendant(cow2, cow1);
if (num1 != -1) {
String str = "grand-mother";
if (num1 == 1) {
str = "mother";
}
else if (num1 == 2) {
str = "grand-mother";
}
else {
for (int i = 0; i < num1 - 2; i++) {
str = "great-" + str;
}
}
pw.println(cow1 + " is the " + str + " of " + cow2);
scan.close();
pw.close();
return;
}
// check if cow2 is direct desc. of cow1
if (num2 != -1) {
String str = "grand-mother";
if (num2 == 1) {
str = "mother";
}
else if (num2 == 2) {
str = "grand-mother";
}
else {
for (int i = 0; i < num2 - 2; i++) {
str = "great-" + str;
}
}
pw.println(cow2 + " is the " + str + " of " + cow1);
scan.close();
pw.close();
return;
}
// check if cow1 is child of anscestor of cow2
if (doAunt(cow1, cow2, pw)) {
scan.close();
pw.close();
return;
}
// check if cow2 is child of anscestor of cow1
if (doAunt(cow2, cow1, pw)) {
scan.close();
pw.close();
return;
}
// check if cow1 and cow2 share a common anscestor
if (commonAnscestor(cow1, cow2, pw)) {
pw.println("COUSINS");
scan.close();
pw.close();
return;
}
// output NOT RELATED
pw.println("NOT RELATED");
scan.close();
pw.close();
}
public static int isDirectDescendant(String ansc, String desc) {
// ansc : cow1, cow2, cow3
// cow1 : desc
int count = 1;
String mot = mother(desc);
while (!(mot.equals("") || mot.equals(ansc))) {
mot = mother(mot);
count++;
}
if (mot.equals("")) {
return -1;
}
return count;
}
public static String mother(String desc) {
for (String str : mothers.keySet()) {
ArrayList<String> children = mothers.get(str);
if (children.contains(desc)) {
return str;
}
}
return "";
}
/*
* Checks if . is aunt of .
* prints output if so and returns true
* otherwise returns false
*/
public static boolean doAunt(String aunt, String niece, PrintWriter pw) {
String mot = mother(aunt);
int num = isDirectDescendant(mot, niece);
if (num != -1) {
String str = "aunt";
for (int i = 0; i < num - 2; i++) {
str = "great-" + str;
}
pw.println(aunt + " is the " + str + " of " + niece);
return true;
}
return false;
}
public static boolean commonAnscestor(String cow1, String cow2, PrintWriter pw) {
String mot1 = mother(cow1);
while (isDirectDescendant(mot1, cow2) == -1 && !(mot1.equals(""))) {
mot1 = mother(mot1);
}
if (!mot1.equals("")) {
return true;
}
mot1 = mother(cow2);
while (isDirectDescendant(mot1, cow1) == -1 && !(mot1.equals(""))) {
mot1 = mother(mot1);
}
if (!mot1.equals("")) {
return true;
}
return false;
}
}