From c04310c56ef91698c67bbc2e7204015c408ba1ea Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Wed, 23 Jul 2025 15:38:09 +0900 Subject: [PATCH] =?UTF-8?q?[20250723]=20BOJ=20/=20G5=20/=20=EB=85=B8?= =?UTF-8?q?=EB=93=9C=EC=82=AC=EC=9D=B4=EC=9D=98=20=EA=B1=B0=EB=A6=AC=20/?= =?UTF-8?q?=20=EA=B9=80=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\235\230 \352\261\260\353\246\254.md" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 "suyeun84/202507/23 BOJ G5 \353\205\270\353\223\234\354\202\254\354\235\264\354\235\230 \352\261\260\353\246\254.md" diff --git "a/suyeun84/202507/23 BOJ G5 \353\205\270\353\223\234\354\202\254\354\235\264\354\235\230 \352\261\260\353\246\254.md" "b/suyeun84/202507/23 BOJ G5 \353\205\270\353\223\234\354\202\254\354\235\264\354\235\230 \352\261\260\353\246\254.md" new file mode 100644 index 00000000..53c6ddfc --- /dev/null +++ "b/suyeun84/202507/23 BOJ G5 \353\205\270\353\223\234\354\202\254\354\235\264\354\235\230 \352\261\260\353\246\254.md" @@ -0,0 +1,70 @@ +```java +import java.util.*; +import java.io.*; + +public class boj1240 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + static ArrayList[] map; + static boolean[] visit; + static int n; + static int max; + + public static void main(String[] args) throws Exception { + nextLine(); + n = nextInt(); + int m = nextInt(); + map = new ArrayList[n]; + visit = new boolean[n]; + + for (int i = 0; i < n; i++) { + map[i] = new ArrayList<>(); + } + + for (int i = 0; i < n - 1; i++) { + nextLine(); + int a = nextInt() - 1; + int b = nextInt() - 1; + int d = nextInt(); + map[a].add(new Node(b, d)); + map[b].add(new Node(a, d)); + } + + for (int i = 0; i < m; i++) { + nextLine(); + int a = nextInt() - 1; + int b = nextInt() - 1; + dfs(a, b, 0); + System.out.println(max); + max = 0; + visit = new boolean[n]; + } + } + + static void dfs(int start, int end, int dist) { + if (start == end) { + max = dist; + return; + } + + visit[start] = true; + for (int i = 0; i < map[start].size(); i++) { + if (!visit[map[start].get(i).next]) { + dfs(map[start].get(i).next, end, dist + map[start].get(i).dist); + } + } + } +} + +class Node { + int next; + int dist; + + public Node(int next, int dist) { + this.next = next; + this.dist = dist; + } +} +```