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

Envío 5608

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

  • Autor: bryancalisto
  • Fecha: 2022-01-23 01:49:36 UTC (Hace casi 3 años)
Caso # Resultado Tiempo Memoria
#1
Incorrecto
0.008 s 3 KBi
#2
Incorrecto
0.025 s 3 KBi
#3
Incorrecto
0.011 s 3 KBi
#4
Correcto
0.02 s 3 KBi
#5
Incorrecto
0.022 s 3 KBi
#6
Incorrecto
0.028 s 3 KBi
#7
Incorrecto
0.018 s 3 KBi
#8
Incorrecto
0.011 s 3 KBi
#9
Incorrecto
0.011 s 3 KBi
#10
Incorrecto
0.008 s 3 KBi
#11
Incorrecto
0.019 s 3 KBi
#12
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Traceback (most recent call last):
  File "script.py", line 28, in <module>
    print(memo)
OSError: [Errno 27] File too large
0.111 s 25 KBi
#13
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Traceback (most recent call last):
  File "script.py", line 28, in <module>
    print(memo)
OSError: [Errno 27] File too large
0.117 s 25 KBi
#14
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Traceback (most recent call last):
  File "script.py", line 28, in <module>
    print(memo)
OSError: [Errno 27] File too large
0.109 s 25 KBi
#15
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Traceback (most recent call last):
  File "script.py", line 28, in <module>
    print(memo)
OSError: [Errno 27] File too large
0.269 s 25 KBi
#16
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Traceback (most recent call last):
  File "script.py", line 28, in <module>
    print(memo)
OSError: [Errno 27] File too large
0.531 s 42 KBi
#17
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Traceback (most recent call last):
  File "script.py", line 28, in <module>
    print(memo)
OSError: [Errno 27] File too large
0.538 s 38 KBi
#18
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Traceback (most recent call last):
  File "script.py", line 28, in <module>
    print(memo)
OSError: [Errno 27] File too large
0.409 s 38 KBi
#19
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Traceback (most recent call last):
  File "script.py", line 28, in <module>
    print(memo)
OSError: [Errno 27] File too large
0.152 s 34 KBi
#20
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Traceback (most recent call last):
  File "script.py", line 28, in <module>
    print(memo)
OSError: [Errno 27] File too large
0.183 s 34 KBi
Puntos totales: 5 / 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

    print(memo)

    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')