forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
42 lines (36 loc) · 930 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
class ThroneInheritance {
public:
unordered_map<string, vector<string>> g;
unordered_set<string> dead;
string king;
vector<string> ans;
ThroneInheritance(string kingName) {
king = kingName;
}
void birth(string parentName, string childName) {
g[parentName].push_back(childName);
}
void death(string name) {
dead.insert(name);
}
vector<string> getInheritanceOrder() {
ans.resize(0);
dfs(king);
return ans;
}
void dfs(string& x) {
if (!dead.count(x)) {
ans.push_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();
*/