Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for What Is Wrong With This Family? by zongareight
from collections import defaultdict
def is_family(tree: list[list[str]]) -> bool:
fathers,sons = defaultdict(set), {}
for father,son in tree:
fathers[father].add(son)
if son == father or son in sons and sons[son] != father or son in fathers and father in fathers[son]:
return False
sons[son] = father
return len(fathers.keys() - sons.keys()) == 1
Nov. 6, 2022