Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for What Is Wrong With This Family? by gyahun_dash
def is_family(relations):
if any(father == son for father, son in relations):
return False
fathers, sons = zip(*relations)
roots = {f for f in fathers if f not in sons}
if len(roots) != 1 or any(sons.count(s) > 1 for s in sons):
return False
restsons = set(sons)
appeared = {roots.pop()}
while appeared:
appeared = {r[1] for r in relations if r[0] in appeared}
restsons -= appeared
return len(restsons) == 0
Dec. 4, 2016