█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 6045

Problema 0x1c - Decir si un nodo es ancestro de otro en un árbol

  • Autor: bryancalisto
  • Fecha: 2022-05-20 02:47:39 UTC (Hace casi 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.022 s 3 KBi
#2
Correcto
0.01 s 3 KBi
#3
Correcto
0.023 s 3 KBi
#4
Correcto
0.017 s 3 KBi
#5
Correcto
0.011 s 3 KBi
#6
Correcto
0.01 s 3 KBi
#7
Correcto
0.021 s 3 KBi
#8
Correcto
0.012 s 3 KBi
#9
Correcto
0.012 s 3 KBi
#10
Correcto
0.011 s 3 KBi
#11
Correcto
0.01 s 3 KBi
#12
Correcto
0.375 s 21 KBi
#13
Correcto
0.38 s 21 KBi
#14
Correcto
0.355 s 21 KBi
#15
Correcto
0.877 s 22 KBi
#16
Tiempo límite excedido
1.576 s 21 KBi
#17
Tiempo límite excedido
1.57 s 21 KBi
#18
Tiempo límite excedido
1.554 s 21 KBi
#19
Tiempo límite excedido
1.599 s 21 KBi
#20
Tiempo límite excedido
1.579 s 21 KBi
Puntos totales: 75 / 100

Código

N = int(input())

tree = {}

for i in range(N - 1):
    treeData = input().split(' ')
    tree['0'] = None
    tree[treeData[1]] = treeData[0]  # key=child, value=parent

# print(tree)
# exit()
C = int(input())

for i in range(C):
    wasYes = False
    query = input().split(' ')
    ancestor = query[0]
    child = query[1]

    if ancestor == '0' or ancestor == child:
        print('Yes')
        continue

    parent = tree[child]

    while parent is not None:
        if parent == ancestor:
            print('Yes')
            wasYes = True
            break
        #elif parent < ancestor:
            #break

        parent = tree[parent]

    if not wasYes:
        print('No')