From ba5128aaa59235f5872346caeed716376e700339 Mon Sep 17 00:00:00 2001 From: JONGUK HAN <90972240+Ukj0ng@users.noreply.github.com> Date: Fri, 6 Mar 2026 18:18:16 +0900 Subject: [PATCH] =?UTF-8?q?[20260306]=20BOJ=20/=20G3=20/=20LCA=20/=20?= =?UTF-8?q?=ED=95=9C=EC=A2=85=EC=9A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ukj0ng/202603/06 BOJ G3 LCA.md | 95 ++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Ukj0ng/202603/06 BOJ G3 LCA.md diff --git a/Ukj0ng/202603/06 BOJ G3 LCA.md b/Ukj0ng/202603/06 BOJ G3 LCA.md new file mode 100644 index 00000000..e9fc3375 --- /dev/null +++ b/Ukj0ng/202603/06 BOJ G3 LCA.md @@ -0,0 +1,95 @@ +``` +import java.io.*; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.List; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Main { + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static List[] graph; + private static int[][] depth; + private static boolean[] visited; + private static int N, M; + + public static void main(String[] args) throws IOException { + init(); + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + N = Integer.parseInt(br.readLine()); + + graph = new List[N+1]; + depth = new int[N+1][2]; + visited = new boolean[N+1]; + + for (int i = 1; i <= N; i++) { + graph[i] = new ArrayList<>(); + } + + for (int i = 0; i < N-1; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int node1 = Integer.parseInt(st.nextToken()); + int node2 = Integer.parseInt(st.nextToken()); + + graph[node1].add(node2); + graph[node2].add(node1); + } + + BFS(); + + M = Integer.parseInt(br.readLine()); + for (int i = 0; i < M; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int node1 = Integer.parseInt(st.nextToken()); + int node2 = Integer.parseInt(st.nextToken()); + + int depth1 = depth[node1][0]; + int depth2 = depth[node2][0]; + + while (depth1 != depth2) { + if (depth1 > depth2) { + node1 = depth[node1][1]; + depth1 = depth[node1][0]; + } else { + node2 = depth[node2][1]; + depth2 = depth[node2][0]; + } + } + + while (node1 != node2) { + node1 = depth[node1][1]; + node2 = depth[node2][1]; + } + + bw.write(node1 + "\n"); + } + } + + private static void BFS() { + Queue q = new ArrayDeque<>(); + depth[1][0] = 1; + depth[1][1] = -1; + visited[1] = true; + q.add(new int[] {1, 1}); + + while (!q.isEmpty()) { + int[] current = q.poll(); + + for (int next : graph[current[0]]) { + if (visited[next]) continue; + depth[next][0] = current[1]+1; + depth[next][1] = current[0]; + visited[next] = true; + q.add(new int[]{next, current[1]+1}); + } + } + } +} +```