五项一起对得上吗
把这条路线算过的东西一次验完。运行下面这段程序:
def ancestors(g, c):
seen = set()
st = [c]
while st:
x = st.pop()
if x in seen:
continue
seen.add(x)
st.extend(g[x])
return seen
def only_on(g, a, b):
return ancestors(g, a) - ancestors(g, b)
def merge_base(g, a, b):
both = ancestors(g, a) & ancestors(g, b)
for x in both:
others = both - {x}
if not any(x in ancestors(g, y) for y in others):
return x
return None
def is_ff(g, cur, target):
return cur in ancestors(g, target)
g = {'3f2a91c': [],
'7b4e2d0': ['3f2a91c'],
'a1c5f83': ['7b4e2d0'],
'e90d417': ['7b4e2d0'],
'5c8b206': ['e90d417']}
ok = (len(ancestors(g, 'a1c5f83')) == 3
and len(ancestors(g, '5c8b206')) == 4
and merge_base(g, 'a1c5f83', '5c8b206') == '7b4e2d0'
and len(only_on(g, '5c8b206', 'a1c5f83')) == 2
and is_ff(g, 'a1c5f83', '5c8b206') is False)
print(ok)
全部评论