-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_ancestor.cpp
More file actions
165 lines (113 loc) · 4.27 KB
/
Copy pathcommon_ancestor.cpp
File metadata and controls
165 lines (113 loc) · 4.27 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
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
// Suppose we have some input data describing a graph of relationships between parents and children over multiple generations.
// The data is formatted as a list of (parent, child) pairs, where each individual is assigned a unique positive integer identifier.
// For example, in this diagram, 6 and 8 have common ancestors of 4 and 14.
// 15
// / \
// 14 13 21
// | |
// 1 2 4 12
// \ / / | \ /
// 3 5 8 9
// \ / \ \
// 6 7 11
// pairs = [
// (1, 3), (2, 3), (3, 6), (5, 6), (5, 7), (4, 5),
// (15, 21), (4, 8), (4, 9), (9, 11), (14, 4), (13, 12),
// (12, 9), (15, 13)
// ]
// 3: [1,2]
// 4: [14]
// 6: [3,5,1,2,4,14]
// 8:[4,14]
// Write a function that takes this data and the identifiers of two individuals as inputs and returns true if and only if they share at least one ancestor.
// Sample input and output:
// hasCommonAncestor(pairs, 3, 8) => false
// hasCommonAncestor(pairs, 5, 8) => true
// hasCommonAncestor(pairs, 6, 8) => true
// hasCommonAncestor(pairs, 6, 9) => true
// hasCommonAncestor(pairs, 1, 3) => false
// hasCommonAncestor(pairs, 3, 1) => false
// hasCommonAncestor(pairs, 7, 11) => true
// hasCommonAncestor(pairs, 6, 5) => true
// hasCommonAncestor(pairs, 5, 6) => true
// hasCommonAncestor(pairs, 3, 6) => true
// hasCommonAncestor(pairs, 21, 11) => true
// n: number of pairs in the input
#include <vector>
#include <iostream>
#include <stack>
using namespace std;
class Solution {
public:
int getMaxId(const vector<vector<int>>& pairs) {
int maxId = 0;
for (const auto& pair : pairs) {
maxId = max(maxId, max(pair[0], pair[1]));
}
return maxId;
}
// make the array
vector<vector<int>> buildParentList(const vector<vector<int>>& pairs, int maxId) {
vector<vector<int>> parents(maxId + 1);
for (const auto& p : pairs) {
int parent = p[0];
int child = p[1];
parents[child].push_back(parent);
}
return parents;
}
vector<bool> getAncestors(int person, const vector<vector<int>>& parents) {
vector<bool> foundAncestor(parents.size(), false);
stack<int> stack;
stack.push(person);
while (!stack.empty()) {
int current = stack.top();
stack.pop();
for (int parent : parents[current]) {
if (!foundAncestor[parent]) {
foundAncestor[parent] = true;
stack.push(parent);
}
}
}
return foundAncestor;
}
// function for main hopefully use for cout
bool hasCommonAncestor(const vector<vector<int>>& pairs, int person1, int person2) {
int maxId = getMaxId(pairs);
vector<vector<int>> parents = buildParentList(pairs, maxId);
vector<bool> ancestors1 = getAncestors(person1, parents);
vector<bool> ancestors2 = getAncestors(person2, parents);
// iterate through the ancestors and check if there is a common ancestor
for (int i = 0; i <= maxId; i++) {
if (ancestors1[i] && ancestors2[i]) {
return true;
}
}
return false;
}
};
int main() {
std::vector<std::vector<int>> pairs = {
{1, 3}, {2, 3}, {3, 6}, {5, 6}, {5, 7}, {4, 5},
{15, 21}, {4, 8}, {4, 9}, {9, 11}, {14, 4}, {13, 12},
{12, 9}, {15, 13}
};
Solution sol;
// print the results of the function calls
cout << boolalpha;
cout << sol.hasCommonAncestor(pairs, 3, 8) << endl; // false
cout << sol.hasCommonAncestor(pairs, 5, 8) << endl; // true
cout << sol.hasCommonAncestor(pairs, 6, 8) << endl; // true
cout << sol.hasCommonAncestor(pairs, 6, 9) << endl; // true
cout << sol.hasCommonAncestor(pairs, 1, 3) << endl; // false
cout << sol.hasCommonAncestor(pairs, 3, 1) << endl; // false
cout << sol.hasCommonAncestor(pairs, 7, 11) << endl; // true
cout << sol.hasCommonAncestor(pairs, 6, 5) << endl; // true
cout << sol.hasCommonAncestor(pairs, 5, 6) << endl; // true
cout << sol.hasCommonAncestor(pairs, 3, 6) << endl; // true
cout << sol.hasCommonAncestor(pairs, 21, 11) << endl; // true
cout << " ---- done! ---- " << endl;
// finish after prints
return 0;
}