classThroneInheritance:def__init__(self,kingName:str):self.king=kingNameself.dead=set()self.g=defaultdict(list)defbirth(self,parentName:str,childName:str)->None:self.g[parentName].append(childName)defdeath(self,name:str)->None:self.dead.add(name)defgetInheritanceOrder(self)->List[str]:defdfs(x:str):xnotinself.deadandans.append(x)foryinself.g[x]:dfs(y)ans=[]dfs(self.king)returnans# Your ThroneInheritance object will be instantiated and called as such:# obj = ThroneInheritance(kingName)# obj.birth(parentName,childName)# obj.death(name)# param_3 = obj.getInheritanceOrder()
classThroneInheritance{privateStringking;privateSet<String>dead=newHashSet<>();privateMap<String,List<String>>g=newHashMap<>();privateList<String>ans=newArrayList<>();publicThroneInheritance(StringkingName){king=kingName;}publicvoidbirth(StringparentName,StringchildName){g.computeIfAbsent(parentName,k->newArrayList<>()).add(childName);}publicvoiddeath(Stringname){dead.add(name);}publicList<String>getInheritanceOrder(){ans.clear();dfs(king);returnans;}privatevoiddfs(Stringx){if(!dead.contains(x)){ans.add(x);}for(Stringy:g.getOrDefault(x,List.of())){dfs(y);}}}/** * Your ThroneInheritance object will be instantiated and called as such: * ThroneInheritance obj = new ThroneInheritance(kingName); * obj.birth(parentName,childName); * obj.death(name); * List<String> param_3 = obj.getInheritanceOrder(); */
classThroneInheritance{public:ThroneInheritance(stringkingName){king=kingName;}voidbirth(stringparentName,stringchildName){g[parentName].emplace_back(childName);}voiddeath(stringname){dead.insert(name);}vector<string>getInheritanceOrder(){ans.resize(0);dfs(king);returnans;}private:stringking;unordered_set<string>dead;unordered_map<string,vector<string>>g;vector<string>ans;voiddfs(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(); */
typeThroneInheritancestruct{kingstringdeadmap[string]boolgmap[string][]string}funcConstructor(kingNamestring)ThroneInheritance{returnThroneInheritance{kingName,map[string]bool{},map[string][]string{}}}func(this*ThroneInheritance)Birth(parentNamestring,childNamestring){this.g[parentName]=append(this.g[parentName],childName)}func(this*ThroneInheritance)Death(namestring){this.dead[name]=true}func(this*ThroneInheritance)GetInheritanceOrder()(ans[]string){vardfsfunc(string)dfs=func(xstring){if!this.dead[x]{ans=append(ans,x)}for_,y:=rangethis.g[x]{dfs(y)}}dfs(this.king)return}/** * Your ThroneInheritance object will be instantiated and called as such: * obj := Constructor(kingName); * obj.Birth(parentName,childName); * obj.Death(name); * param_3 := obj.GetInheritanceOrder(); */
classThroneInheritance{privateking:string;privatedead:Set<string>=newSet();privateg:Map<string,string[]>=newMap();constructor(kingName:string){this.king=kingName;}birth(parentName:string,childName:string):void{this.g.set(parentName,this.g.get(parentName)||[]);this.g.get(parentName)!.push(childName);}death(name:string):void{this.dead.add(name);}getInheritanceOrder():string[]{constans:string[]=[];constdfs=(x:string)=>{if(!this.dead.has(x)){ans.push(x);}for(constyofthis.g.get(x)||[]){dfs(y);}};dfs(this.king);returnans;}}/** * Your ThroneInheritance object will be instantiated and called as such: * var obj = new ThroneInheritance(kingName) * obj.birth(parentName,childName) * obj.death(name) * var param_3 = obj.getInheritanceOrder() */
publicclassThroneInheritance{privatestringking;privateHashSet<string>dead=newHashSet<string>();privateDictionary<string,List<string>>g=newDictionary<string,List<string>>();privateList<string>ans=newList<string>();publicThroneInheritance(stringkingName){king=kingName;}publicvoidBirth(stringparentName,stringchildName){if(!g.ContainsKey(parentName)){g[parentName]=newList<string>();}g[parentName].Add(childName);}publicvoidDeath(stringname){dead.Add(name);}publicIList<string>GetInheritanceOrder(){ans.Clear();DFS(king);returnans;}privatevoidDFS(stringx){if(!dead.Contains(x)){ans.Add(x);}if(g.ContainsKey(x)){foreach(stringying[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); * IList<string> param_3 = obj.GetInheritanceOrder(); */