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

Envío 5610

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

  • Autor: bryancalisto
  • Fecha: 2022-01-23 01:52:57 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.008 s 3 KBi
#2
Correcto
0.022 s 3 KBi
#3
Correcto
0.008 s 3 KBi
#4
Correcto
0.009 s 3 KBi
#5
Correcto
0.009 s 3 KBi
#6
Correcto
0.016 s 3 KBi
#7
Correcto
0.009 s 3 KBi
#8
Correcto
0.008 s 3 KBi
#9
Correcto
0.008 s 3 KBi
#10
Correcto
0.023 s 3 KBi
#11
Correcto
0.008 s 3 KBi
#12
Correcto
0.567 s 45 KBi
#13
Correcto
1.268 s 44 KBi
#14
Correcto
1.306 s 45 KBi
#15
Correcto
0.519 s 42 KBi
#16
Tiempo límite excedido
1.603 s 109 KBi
#17
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/python-3.8.1/bin/python3 script.py
0.873 s 125 KBi
#18
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/python-3.8.1/bin/python3 script.py
1.305 s 125 KBi
#19
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/python-3.8.1/bin/python3 script.py
0.968 s 125 KBi
#20
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/python-3.8.1/bin/python3 script.py
0.95 s 125 KBi
Puntos totales: 75 / 100

Código

N = int(input())

tree = {}
memo = {}

for i in range(N - 1):
    treeData = input().split(' ')
    tree[treeData[1]] = treeData[0]

C = int(input())

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

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

    try:
        parent = tree[child]
    except:
        parent = None

    while parent is not None:
        if parent == ancestor or (parent in memo and ancestor in memo[parent]):
            print('Yes')
            wasYes = True
            break

        memo[child][parent] = True

        try:
            parent = tree[parent]
        except:
            parent = None

    if not wasYes:
        print('No')