From 0b39e0e7693513c7a729db9a0a9f5b8079693d1d Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Sat, 25 Oct 2025 17:17:53 +0900 Subject: [PATCH] =?UTF-8?q?[20251025]=20PGM=20/=20Lv3=20/=20=EB=93=B1?= =?UTF-8?q?=EB=8C=80=20/=20=EC=9D=B4=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../25 PGM \353\223\261\353\214\200.md" | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 "0224LJH/202510/25 PGM \353\223\261\353\214\200.md" diff --git "a/0224LJH/202510/25 PGM \353\223\261\353\214\200.md" "b/0224LJH/202510/25 PGM \353\223\261\353\214\200.md" new file mode 100644 index 00000000..3a0f1baa --- /dev/null +++ "b/0224LJH/202510/25 PGM \353\223\261\353\214\200.md" @@ -0,0 +1,79 @@ +```java +import java.io.*; +import java.util.*; + +class Solution { + static int nodeCnt, answer; + static Node[] nodes; + + static class Node{ + int num; + boolean isOn = false; + Node parent = null; + HashSet to = new HashSet<>(); + + public Node(int num){ + this.num = num; + } + } + + public int solution(int n, int[][] lighthouse) { + nodeCnt = n; + answer = 0; + init(lighthouse); + + makeTree(1); + turnLight(1); + for (int i = 1; i<= nodeCnt; i++){ + if (nodes[i].isOn)answer++; + } + answer = Math.min(answer, nodeCnt - answer); + + return answer; + } + + private void turnLight(int nodeNum){ + Node node = nodes[nodeNum]; + if (node.to.size() == 0){ + node.isOn = false; + return; + } + + boolean result = true; + for (Node child: node.to){ + turnLight(child.num); + result &= child.isOn; + } + node.isOn = !result; + } + + private void makeTree(int nodeNum){ + Node node = nodes[nodeNum]; + for (Node child: node.to){ + child.parent = node; + child.to.remove(node); + makeTree(child.num); + } + + } + + private void init(int[][] lighthouse){ + + nodes = new Node[nodeCnt+1]; + for (int i = 0; i <= nodeCnt; i++){ + nodes[i] = new Node(i); + } + + for (int i = 0; i< nodeCnt-1; i++){ + int n1 = lighthouse[i][0]; + int n2 = lighthouse[i][1]; + + Node node1 = nodes[n1]; + Node node2 = nodes[n2]; + node1.to.add(node2); + node2.to.add(node1); + + } + } +} +```