|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +class Solution { |
| 5 | + |
| 6 | + private static int maxNode; |
| 7 | + private static int[] answer; |
| 8 | + private static boolean[] isParent, isChild, visited; |
| 9 | + private static Queue<Integer> q; |
| 10 | + |
| 11 | + private static Map<Integer, List<Integer>> graph; |
| 12 | + |
| 13 | + private static void init(int[][] edges) { |
| 14 | + answer = new int[4]; |
| 15 | + isParent = new boolean[1000001]; |
| 16 | + isChild = new boolean[1000001]; |
| 17 | + |
| 18 | + graph = new HashMap<>(); |
| 19 | + |
| 20 | + maxNode = 0; |
| 21 | + for (int[] edge : edges) { |
| 22 | + List<Integer> tmp; |
| 23 | + |
| 24 | + maxNode = Math.max(maxNode, Math.max(edge[0], edge[1])); |
| 25 | + |
| 26 | + isParent[edge[0]] = true; |
| 27 | + isChild[edge[1]] = true; |
| 28 | + |
| 29 | + if (graph.get(edge[0]) == null) { |
| 30 | + tmp = new ArrayList<>(); |
| 31 | + } else { |
| 32 | + tmp = graph.get(edge[0]); |
| 33 | + } |
| 34 | + |
| 35 | + tmp.add(edge[1]); |
| 36 | + graph.put(edge[0], tmp); |
| 37 | + } |
| 38 | + q = new ArrayDeque<>(); |
| 39 | + visited = new boolean[maxNode + 1]; |
| 40 | + } |
| 41 | + |
| 42 | + private static void findRoot() { |
| 43 | + for (int i = 0; i <= maxNode; i++) { |
| 44 | + if (isParent[i] && !isChild[i]) { |
| 45 | + List<Integer> children = graph.get(i); |
| 46 | + |
| 47 | + // 나가는 간선이 2개 이상 |
| 48 | + if (children != null && children.size() >= 2) { |
| 49 | + answer[0] = i; |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static void bfs(int start) { |
| 57 | + q.offer(start); |
| 58 | + visited[start] = true; |
| 59 | + |
| 60 | + int nodeCnt = 0; |
| 61 | + int edgeCnt = 0; |
| 62 | + |
| 63 | + while (!q.isEmpty()) { |
| 64 | + int cur = q.poll(); |
| 65 | + nodeCnt++; |
| 66 | + |
| 67 | + List<Integer> children = graph.get(cur); |
| 68 | + |
| 69 | + // | |
| 70 | + if (children == null) { |
| 71 | + answer[2]++; |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + edgeCnt += children.size(); |
| 76 | + |
| 77 | + // 자식 정점이 2개 이상 - 8 |
| 78 | + if (children.size() > 1) { |
| 79 | + answer[3]++; |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + for (int next : children) { |
| 84 | + if (visited[next]) { |
| 85 | + // 간선 수 == 노드 수 - O |
| 86 | + // 간선 수 > 노드 수 - 8자 |
| 87 | + if (edgeCnt == nodeCnt) { |
| 88 | + answer[1]++; |
| 89 | + } else { |
| 90 | + answer[3]++; |
| 91 | + } |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + visited[next] = true; |
| 96 | + q.offer(next); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public int[] solution(int[][] edges) { |
| 102 | + init(edges); |
| 103 | + findRoot(); |
| 104 | + |
| 105 | + for (int n : graph.get(answer[0])) { |
| 106 | + Arrays.fill(visited, false); |
| 107 | + bfs(n); |
| 108 | + q.clear(); |
| 109 | + } |
| 110 | + |
| 111 | + return answer; |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
0 commit comments