|
| 1 | +// Authored by: beberiche |
| 2 | +// Co-authored by: - |
| 3 | +// Link: http://boj.kr/4dbb97429a544705b8b5a4fd422048eb |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | +import java.io.*; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + public static void main(String[] args) { |
| 10 | + FastReader rd = new FastReader(); |
| 11 | + int N = rd.nextInt(); |
| 12 | + // 아이템 -> idx |
| 13 | + TreeMap<String, Integer> m = new TreeMap<>(); |
| 14 | + // 아이템 -> 아이템 |
| 15 | + TreeMap<String, List<String>> mm = new TreeMap<>(); |
| 16 | + int[] inDegree = new int[N * 2 + 4]; |
| 17 | + int idx = 0; |
| 18 | + for (int i = 0; i < N; i++) { |
| 19 | + String i1 = rd.next(); |
| 20 | + String i2 = rd.next(); |
| 21 | + |
| 22 | + if (!m.containsKey(i1)) m.put(i1, idx++); |
| 23 | + if (!m.containsKey(i2)) m.put(i2, idx++); |
| 24 | + |
| 25 | + if (!mm.containsKey(i1)) mm.put(i1, new ArrayList<>()); |
| 26 | + if (!mm.containsKey(i2)) mm.put(i2, new ArrayList<>()); |
| 27 | + |
| 28 | + inDegree[m.get(i2)]++; |
| 29 | + mm.get(i1).add(i2); |
| 30 | + } |
| 31 | + |
| 32 | + Queue<Node> q = new LinkedList<>(); |
| 33 | + |
| 34 | + for (String item : m.keySet()) { |
| 35 | + if (inDegree[m.get(item)] == 0) { |
| 36 | + q.add(new Node(item, 0)); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + PriorityQueue<Node> pq = new PriorityQueue<>((n1, n2) -> { |
| 41 | + if (n1.degree == n2.degree) return n1.item.compareTo(n2.item); |
| 42 | + return n1.degree - n2.degree; |
| 43 | + }); |
| 44 | + |
| 45 | + while (!q.isEmpty()) { |
| 46 | + Node curr = q.poll(); |
| 47 | + pq.add(curr); |
| 48 | + |
| 49 | + for (String next : mm.get(curr.item)) { |
| 50 | + int nextIdx = m.get(next); |
| 51 | + inDegree[nextIdx]--; |
| 52 | + if (inDegree[nextIdx] == 0) { |
| 53 | + q.add(new Node(next, curr.degree + 1)); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + if (idx != pq.size()) { |
| 60 | + System.out.println(-1); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + StringBuilder sb = new StringBuilder(); |
| 65 | + while (!pq.isEmpty()) { |
| 66 | + sb.append(pq.poll().item).append("\n"); |
| 67 | + } |
| 68 | + |
| 69 | + System.out.print(sb.toString()); |
| 70 | + } |
| 71 | + |
| 72 | + static class Node { |
| 73 | + String item; |
| 74 | + int degree; |
| 75 | + |
| 76 | + Node(String item, int degree) { |
| 77 | + this.item = item; |
| 78 | + this.degree = degree; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + static class FastReader { |
| 85 | + BufferedReader br; |
| 86 | + StringTokenizer st; |
| 87 | + |
| 88 | + public FastReader() { |
| 89 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 90 | + } |
| 91 | + |
| 92 | + String next() { |
| 93 | + while (st == null || !st.hasMoreElements()) { |
| 94 | + try { |
| 95 | + st = new StringTokenizer(br.readLine()); |
| 96 | + } catch (IOException e) { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + } |
| 100 | + return st.nextToken(); |
| 101 | + } |
| 102 | + |
| 103 | + int nextInt() { |
| 104 | + return Integer.parseInt(next()); |
| 105 | + } |
| 106 | + |
| 107 | + long nextLong() { |
| 108 | + return Long.parseLong(next()); |
| 109 | + } |
| 110 | + |
| 111 | + double nextDouble() { |
| 112 | + return Double.parseDouble(next()); |
| 113 | + } |
| 114 | + |
| 115 | + String nextLine() { |
| 116 | + String str = ""; |
| 117 | + try { |
| 118 | + str = br.readLine(); |
| 119 | + } catch (IOException e) { |
| 120 | + e.printStackTrace(); |
| 121 | + } |
| 122 | + return str; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/* Solution Description |
| 128 | +
|
| 129 | +1. 위상정렬 응용 문제. 위상정렬을 통해 모든 탐색이 가능한지를 확인하여, |
| 130 | + 위상정렬 단계 및 아이템 사전 순으로 정렬된 상태로 출력을 해야한다. |
| 131 | +
|
| 132 | +2. 초기 `q` 에 삽입되는 위상정렬 단계는 `0` 으로 설정하며, |
| 133 | + 현재 노드에서 다음으로 이동할 노드가 존재하는 경우 (진입 차수가 `0` 인 노드) |
| 134 | + `현재 노드의 위상정렬 단계 + 1` 의 값을 다음 이동 노드에 반영한다. |
| 135 | +
|
| 136 | +3. 우선순위 큐를 생성하여, 반환되는 큐의 노드 순으로 우선순위 큐에 담아준다. |
| 137 | + 위상정렬 탐색 완료 이후, 만약 아이템의 갯수와 우선순위 큐의 사이즈가 동일하다면, |
| 138 | + 모든 아이템을 구할 수 있는 경우이며 우선순위 큐의 노드들을 순서대로 출력하자. |
| 139 | +
|
| 140 | +4. 아이템의 갯수와 우선순위 큐의 사이즈가 동일하지 못하다면, |
| 141 | + 현재 주어진 선후 관계로는 모든 아이템을 구매하지 못하는 경우이다. |
| 142 | + (-1을 출력하자.) |
| 143 | +
|
| 144 | + */ |
0 commit comments