From 2bbeb64ed4080693d5df8b293f7e70fb2d94341f Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Sun, 21 Sep 2025 11:40:57 +0900 Subject: [PATCH] =?UTF-8?q?[20250921]=20BOJ=20/=20G4=20/=20=EA=B0=80?= =?UTF-8?q?=EC=9E=A5=20=EA=B0=80=EA=B9=8C=EC=9A=B4=20=EA=B3=B5=ED=86=B5=20?= =?UTF-8?q?=EC=A1=B0=EC=83=81=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 --- ...5\355\206\265 \354\241\260\354\203\201.md" | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 "0224LJH/202509/21 BOJ \352\260\200\354\236\245 \352\260\200\352\271\214\354\232\264 \352\263\265\355\206\265 \354\241\260\354\203\201.md" diff --git "a/0224LJH/202509/21 BOJ \352\260\200\354\236\245 \352\260\200\352\271\214\354\232\264 \352\263\265\355\206\265 \354\241\260\354\203\201.md" "b/0224LJH/202509/21 BOJ \352\260\200\354\236\245 \352\260\200\352\271\214\354\232\264 \352\263\265\355\206\265 \354\241\260\354\203\201.md" new file mode 100644 index 00000000..deee7e7f --- /dev/null +++ "b/0224LJH/202509/21 BOJ \352\260\200\354\236\245 \352\260\200\352\271\214\354\232\264 \352\263\265\355\206\265 \354\241\260\354\203\201.md" @@ -0,0 +1,91 @@ +```java + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +class Main { + static BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); + static int nodeCnt, num1, num2,ans; + static Node[] nodes; + static HashSet set = new HashSet<>(); + + static class Node{ + int num; + Node parent; + HashSet children = new HashSet<>(); + + } + + + public static void main(String[] args) throws NumberFormatException, IOException { + int TC = Integer.parseInt(br.readLine()); + for (int tc = 1; tc <= TC; tc++) { + init(); + process(); + print(); + + } + + } + + + public static void init() throws NumberFormatException, IOException { + + nodeCnt = Integer.parseInt(br.readLine()); + nodes = new Node[nodeCnt+1]; + + for (int i = 1; i <= nodeCnt; i++) { + nodes[i] = new Node(); + nodes[i].num = i; + } + + for (int i = 0; i < nodeCnt-1; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int parentNum = Integer.parseInt(st.nextToken()); + int childNum = Integer.parseInt(st.nextToken()); + + nodes[parentNum].children.add(nodes[childNum]); + nodes[childNum].parent = nodes[parentNum]; + } + + StringTokenizer st = new StringTokenizer(br.readLine()); + num1 = Integer.parseInt(st.nextToken()); + num2 = Integer.parseInt(st.nextToken()); + + + + } + + public static void process() { + set.clear(); + + Node cur = nodes[num1]; + set.add(cur); + while(cur.parent != null) { + set.add(cur); + cur = cur.parent; + } + + cur = nodes[num2]; + while(cur.parent != null) { + if (set.contains(cur)) { + ans = cur.num; + return; + } + cur = cur.parent; + } + + ans = cur.num; + + } + + + + + public static void print() { + System.out.println(ans); + } +} +```