From e35dc0b4bbfc1d7d41e659ceae8221419ef317a7 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Thu, 11 Dec 2025 13:43:07 +0900 Subject: [PATCH] =?UTF-8?q?[20251211]=20BOJ=20/=20G4=20/=20=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=EC=9D=98=20=EC=A7=80=EB=A6=84=20/=20=EC=9D=B4?= =?UTF-8?q?=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\235\230 \354\247\200\353\246\204.md" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 "0224LJH/202512/11 BOJ \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" diff --git "a/0224LJH/202512/11 BOJ \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" "b/0224LJH/202512/11 BOJ \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" new file mode 100644 index 00000000..d611845b --- /dev/null +++ "b/0224LJH/202512/11 BOJ \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" @@ -0,0 +1,86 @@ +```java + +import java.util.StringTokenizer; +import java.util.*; +import java.io.*; + +public class Main { + + static int nodeCnt,ans = 0; + static Node[] nodes; + + static class Node{ + int idx = 0; + Node parent = null; + HashSet set = new HashSet<>(); + int dis = 0; + + public Node (int idx) { + this.idx = idx; + } + } + + + + public static void main(String[] args) throws IOException { + init(); + process(); + print(); + + } + + private static void init() throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + nodeCnt = Integer.parseInt(br.readLine()); + nodes = new Node[nodeCnt+1]; + + for (int i = 1; i<= nodeCnt; i++) { + nodes[i] = new Node(i); + } + + for (int i = 1; i < nodeCnt; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int pNum = Integer.parseInt(st.nextToken()); + int cNum = Integer.parseInt(st.nextToken()); + int dis = Integer.parseInt(st.nextToken()); + Node p = nodes[pNum]; + Node c = nodes[cNum]; + c.parent = p; + p.set.add(c); + c.dis = dis; + } + } + + private static void process() throws IOException { + int rs = recursive(1); + ans = Math.max(ans, rs); + + } + + public static int recursive(int num) { + Node target = nodes[num]; + if(target.set.isEmpty()) return target.dis; + + PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder()); + + for (Node n: target.set) { + pq.add(recursive(n.idx)); + } + int longest = pq.poll(); + + if (!pq.isEmpty()) { + int second = pq.poll(); + + ans = Math.max(ans, longest+second); + } + + + return longest + target.dis; + } + + private static void print() { + System.out.println(ans); + } + +} +```