Código:
Gracias. resolve = lambda a, b: [(a[0], b[1])] if a[1] == b[0] else [] def isconsistent(inequations): pending = inequations[:] done = [] hashes = set() while pending: c = pending.pop() if c[0] == c[1]: return False for i in done: n = resolve(c, i) + resolve(i, c) for e in n: h = "%s<%s"%e if h in hashes: continue hashes.add(h) pending.append(e) done.append(c) return True print(isconsistent([("A", "B"), ("B", "C"), ("C", "D")])) print(isconsistent([("A", "B"), ("B", "C"), ("C", "A")]))