forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
43 lines (37 loc) · 948 Bytes
/
Solution.cpp
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
class ThroneInheritance {
public:
ThroneInheritance(string kingName) {
king = kingName;
}
void birth(string parentName, string childName) {
g[parentName].emplace_back(childName);
}
void death(string name) {
dead.insert(name);
}
vector<string> getInheritanceOrder() {
ans.resize(0);
dfs(king);
return ans;
}
private:
string king;
unordered_set<string> dead;
unordered_map<string, vector<string>> g;
vector<string> ans;
void dfs(string& x) {
if (!dead.contains(x)) {
ans.emplace_back(x);
}
for (auto& y : g[x]) {
dfs(y);
}
}
};
/**
* Your ThroneInheritance object will be instantiated and called as such:
* ThroneInheritance* obj = new ThroneInheritance(kingName);
* obj->birth(parentName,childName);
* obj->death(name);
* vector<string> param_3 = obj->getInheritanceOrder();
*/