两边独有的加起来是多少
这个数叫"分叉距离"——两条分支各自往前走了多远。运行下面这段程序:
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)
g = {'3f2a91c': [],
'7b4e2d0': ['3f2a91c'],
'a1c5f83': ['7b4e2d0'],
'e90d417': ['7b4e2d0'],
'5c8b206': ['e90d417']}
a = len(only_on(g, 'a1c5f83', '5c8b206'))
b = len(only_on(g, '5c8b206', 'a1c5f83'))
print(str(a) + "+" + str(b) + "=" + str(a + b))
全部评论