Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
13-liner: Sim0000's simplified and corrected two fathers case solution in Clear category for What Is Wrong With This Family? by przemyslaw.daniel
from typing import List
def is_family(tree: List[List[str]]) -> bool:
ancestor = {elem: set() for elem in sum(tree, [])}
for parent, child in tree:
if (ancestor[parent] | {parent}) & (ancestor[child] | {child}):
return False
ancestor[child] |= ancestor[parent] | {parent}
result = list(child for _, child in tree)
return len(result) == len(set(result)) and \
list(ancestor.values()).count(set()) == 1
Jan. 4, 2021