|
| 1 | +```java |
| 2 | +//1시간 30분 |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + |
| 10 | + static final int PAINT = 1; |
| 11 | + static final int MOVE = 2; |
| 12 | + static final int COUNT = 3; |
| 13 | + |
| 14 | + static StringBuilder sb = new StringBuilder(); |
| 15 | + static Node[] nodes; |
| 16 | + static int nodeCnt, operCnt; |
| 17 | + static int[][] opers; |
| 18 | + |
| 19 | + static class Node{ |
| 20 | + Node parent = null; |
| 21 | + int num; |
| 22 | + int color = 0; // 자신의 부모로 가는 길의 색상 |
| 23 | + |
| 24 | + public Node(int num){ |
| 25 | + this.num = num; |
| 26 | + } |
| 27 | + |
| 28 | + public void moveTo(Node nParent){ |
| 29 | + parent = nParent; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + public static void main(String[] args) throws IOException { |
| 35 | + init(); |
| 36 | + process(); |
| 37 | + print(); |
| 38 | + } |
| 39 | + |
| 40 | + private static void init() throws IOException { |
| 41 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 42 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 43 | + nodeCnt = Integer.parseInt(st.nextToken()); |
| 44 | + operCnt = Integer.parseInt(st.nextToken()); |
| 45 | + |
| 46 | + nodes = new Node[nodeCnt]; |
| 47 | + opers = new int[operCnt][4]; |
| 48 | + |
| 49 | + nodes[0] = new Node(0); |
| 50 | + for (int i = 1; i < nodeCnt; i++) { |
| 51 | + nodes[i] = new Node(i); |
| 52 | + nodes[i].moveTo(nodes[0]); |
| 53 | + } |
| 54 | + |
| 55 | + for (int i = 0; i < operCnt; i++) { |
| 56 | + st = new StringTokenizer(br.readLine()); |
| 57 | + int idx = 0; |
| 58 | + while(st.hasMoreTokens()){ |
| 59 | + opers[i][idx++] = Integer.parseInt(st.nextToken()); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static void process() { |
| 65 | + for (int i = 0; i < operCnt; i++) { |
| 66 | + int[] oper = opers[i]; |
| 67 | + |
| 68 | + switch (oper[0]) { |
| 69 | + case PAINT: paintWholePath(oper[1], oper[2], oper[3]); break; |
| 70 | + case MOVE: move(oper[1], oper[2]); break; |
| 71 | + case COUNT: getColorCntOfPath(oper[1], oper[2]); break; |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + private static Node findFirstCommonParent(Node node1, Node node2){ |
| 79 | + Node curNode1 = node1; |
| 80 | + Node curNode2 = node2; |
| 81 | + |
| 82 | + if (node1 == node2) {return node1;} |
| 83 | + |
| 84 | + |
| 85 | + HashSet<Node> set = new HashSet<>(); |
| 86 | + set.add(curNode1); |
| 87 | + set.add(curNode2); |
| 88 | + while(true){ |
| 89 | + if (curNode1.parent != null) { |
| 90 | + curNode1 = curNode1.parent; |
| 91 | + if (set.contains(curNode1)) { |
| 92 | + return curNode1; |
| 93 | + } |
| 94 | + set.add(curNode1); |
| 95 | + } |
| 96 | + |
| 97 | + if (curNode2.parent != null) { |
| 98 | + curNode2 = curNode2.parent; |
| 99 | + if (set.contains(curNode2)) { |
| 100 | + return curNode2; |
| 101 | + } |
| 102 | + set.add(curNode2); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static void paintWholePath(int num1, int num2, int color){ |
| 108 | + Node node1 = nodes[num1]; |
| 109 | + Node node2 = nodes[num2]; |
| 110 | + Node parent = findFirstCommonParent(node1, node2); |
| 111 | + |
| 112 | + while (node1 != parent){ |
| 113 | + node1.color = color; |
| 114 | + node1 = node1.parent; |
| 115 | + } |
| 116 | + while (node2 != parent){ |
| 117 | + node2.color = color; |
| 118 | + node2 = node2.parent; |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + private static void getColorCntOfPath(int num1, int num2){ |
| 123 | + Node node1 = nodes[num1]; |
| 124 | + Node node2 = nodes[num2]; |
| 125 | + Node parent = findFirstCommonParent(node1, node2); |
| 126 | + |
| 127 | + HashSet<Integer> set = new HashSet<>(); |
| 128 | + while (node1 != null && node1 != parent ){ |
| 129 | + //노드1부터 상위 노드로 가는 엣지의 색상 추가. |
| 130 | + set.add(node1.color); |
| 131 | + node1 = node1.parent; |
| 132 | + } |
| 133 | + while (node2 != null && node2 != parent){ |
| 134 | + set.add(node2.color); |
| 135 | + node2 = node2.parent; |
| 136 | + } |
| 137 | + |
| 138 | + sb.append(set.size()).append("\n"); |
| 139 | + } |
| 140 | + |
| 141 | + private static void move(int targetN, int nParentNum){ |
| 142 | + Node target = nodes[targetN]; |
| 143 | + Node nParent = nodes[nParentNum]; |
| 144 | + target.moveTo(nParent); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + private static void print() { |
| 150 | + System.out.print(sb.toString()); |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
0 commit comments