leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1600.cpp (675B)
0 class ThroneInheritance {
1 unordered_map<string, vector<string>> children;
2 unordered_set<string> dead;
4 const string king;
6 public:
7 ThroneInheritance(const string &king) : king(king) {}
8 void birth(const string &parent, const string &child) { children[parent].push_back(child); }
9 void death(const string &name) { dead.insert(name); }
11 vector<string> getInheritanceOrder() {
12 order.clear();
13 rec(king);
14 return order;
15 }
17 private:
18 vector<string> order;
19 void rec(const string &name) {
20 if (!dead.count(name)) order.push_back(name);
21 for (const string &s : children[name])
22 rec(s);
23 }
24 };