奇安信AC 2
第一道不要忘了考虑 进程不存在 的情况
def test(arr1, arr2, item): if item not in arr1+arr2: return 0 n = len(arr1) dic = {} for i in range(n): dic[arr2[i]] = [] for i in range(n): dic[arr2[i]].append(arr1[i]) stack = [item] res = 0 while stack: cur = stack.pop() res += 1 if cur in dic: stack += dic[cur] return res arr1 = list(map(int, input().split(' '))) arr2 = list(map(int, input().split(' '))) item = int(input()) print(test(arr1, arr2, item))第2道题不要忘了考虑 节点不存在 的情况
def test(arr, x, y): if x not in arr or y not in arr: return -1 i,j = arr.index(x),arr.index(y) while i != j: if i > j: i = (i-1)>>1 else: j = (j-1)>>1 return arr[i] input() arr = list(map(int, input().split(' '))) x,y = map(int, input().split(' ')) print(test(arr, x, y))