import sys sys.setrecursionlimit(1000000) def dfs(r,b,h,d): s[h] = d if len(e[h])==1 and h!=r: dfs(h,h,h,0) else: for nxt in e[h]: if nxt!=b and d+1