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

Envío 6249

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

  • Autor: rpedrazacoello
  • Fecha: 2022-05-28 02:40:28 UTC (Hace casi 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.081 s 16 KBi
#2
Correcto
0.091 s 16 KBi
#3
Correcto
0.088 s 16 KBi
#4
Correcto
0.089 s 16 KBi
#5
Correcto
0.087 s 16 KBi
#6
Correcto
0.117 s 18 KBi
#7
Correcto
0.088 s 16 KBi
#8
Correcto
0.082 s 16 KBi
#9
Correcto
0.09 s 16 KBi
#10
Correcto
0.083 s 16 KBi
#11
Correcto
0.086 s 16 KBi
#12
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.512 s 125 KBi
#13
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.615 s 125 KBi
#14
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.521 s 125 KBi
#15
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.508 s 125 KBi
#16
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.542 s 125 KBi
#17
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.48 s 125 KBi
#18
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.533 s 125 KBi
#19
Tiempo límite excedido
1.024 s 125 KBi
#20
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.515 s 125 KBi
Puntos totales: 55 / 100

Código

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Stack;

public class Main {

    private static HashMap<Integer, ArrayList<Integer>> parentSons = new HashMap<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for(int i=0; i<n-1; i++){
            int p = scanner.nextInt();
            int h = scanner.nextInt();

            if(!parentSons.containsKey(p)){
                ArrayList<Integer> arrayList = new ArrayList<Integer>();
                arrayList.add(h);
                parentSons.put(p, arrayList);
            } else {
                parentSons.get(p).add(h);
            }
        }

        int c = scanner.nextInt();
        for(int i=0; i<c; i++){
            int ancestor = scanner.nextInt();
            int descendant = scanner.nextInt();

            if(isAncestor(ancestor, descendant)){
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }

    private static boolean isAncestor(int ancestor, int descendant){
        if(ancestor == descendant){
            return true;
        }

        if(descendant == 0){
            return false;
        }

        Stack<Integer> toProcess = new Stack<>();
        if(parentSons.containsKey(ancestor)) {
            toProcess.addAll(parentSons.get(ancestor));
        }

        while(!toProcess.isEmpty()){
            int node = toProcess.pop();

            if(descendant == node){
                return true;
            }

            if(parentSons.containsKey(node)){
                toProcess.addAll(parentSons.get(node));
            }
        }

        return false;
    }
}