Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Incorrecto
|
0.078 s | 16 KBi |
#2 |
Incorrecto
|
0.078 s | 16 KBi |
#3 |
Incorrecto
|
0.091 s | 17 KBi |
#4 |
Incorrecto
|
0.082 s | 16 KBi |
#5 |
Incorrecto
|
0.074 s | 16 KBi |
#6 |
Incorrecto
|
0.095 s | 17 KBi |
#7 |
Incorrecto
|
0.088 s | 17 KBi |
#8 |
Incorrecto
|
0.094 s | 17 KBi |
#9 |
Incorrecto
|
0.09 s | 17 KBi |
#10 |
Incorrecto
|
0.083 s | 16 KBi |
#11 |
Incorrecto
|
0.084 s | 16 KBi |
#12 |
Incorrecto
|
0.138 s | 14 KBi |
#13 |
Incorrecto
|
0.105 s | 17 KBi |
#14 |
Incorrecto
|
0.141 s | 14 KBi |
#15 |
Incorrecto
|
0.093 s | 17 KBi |
#16 |
Incorrecto
|
0.093 s | 17 KBi |
#17 |
Incorrecto
|
0.105 s | 17 KBi |
#18 |
Incorrecto
|
0.088 s | 17 KBi |
#19 |
Incorrecto
|
0.087 s | 17 KBi |
#20 |
Incorrecto
|
0.094 s | 17 KBi |
import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; public class Main { private static HashMap<Integer, ArrayList<Integer>> directedRelations = new HashMap<>(); private static HashMap<Integer, Boolean> unprocessedNodes = new HashMap<>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); for(int i=0; i<m; i++){ int from = scanner.nextInt(); int to = scanner.nextInt(); unprocessedNodes.put(from, true); if(!unprocessedNodes.containsKey(to)){ unprocessedNodes.put(to, true); } if(directedRelations.containsKey(from)){ directedRelations.get(from).add(to); } else { ArrayList<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(to); directedRelations.put(from,arrayList); } } while (!unprocessedNodes.isEmpty()){ int node = (int) unprocessedNodes.keySet().toArray()[0]; if(findCycle(node, new HashMap<Integer, Boolean>())){ System.out.println("Yes"); break; } } if(unprocessedNodes.isEmpty()){ System.out.println("No"); } } /** * True if find cycle, false otherwise * @param node * @param recursionNode * @return */ private static boolean findCycle (int node, HashMap<Integer, Boolean> recursionNode){ if(directedRelations.containsKey(node)) { if(!recursionNode.containsKey(node)) { recursionNode.put(node, true); } else { return true; } if(!unprocessedNodes.containsKey(node)){ return false; } for (int relation : directedRelations.get(node)) { if (findCycle(relation, recursionNode)){ return true; } } recursionNode.remove(node); } if(unprocessedNodes.containsKey(node)){ unprocessedNodes.remove(node); } return false; } }