From 0d600f24eb2616f47ef6ef2266608d6e4d1cdecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A7=84?= Date: Fri, 24 Oct 2025 19:20:13 +0900 Subject: [PATCH] =?UTF-8?q?[20251024]=20BOJ=20/=20G2=20/=20=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=EC=9D=98=20=EC=A7=80=EB=A6=84=20/=20=EA=B9=80?= =?UTF-8?q?=EB=AF=BC=EC=A7=84?= 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" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 "zinnnn37/202510/24 BOJ G2 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" diff --git "a/zinnnn37/202510/24 BOJ G2 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" "b/zinnnn37/202510/24 BOJ G2 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" new file mode 100644 index 00000000..6712bc55 --- /dev/null +++ "b/zinnnn37/202510/24 BOJ G2 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" @@ -0,0 +1,98 @@ +```java +package etc; + +import java.io.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.StringTokenizer; + +public class BJ_1167_트리의_지름 { + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static StringTokenizer st; + + private static int V, ans, max, maxNode; + private static int[] dist; + private static List[] graph; + + private static class Node { + int to; + int weight; + + Node(int to, int weight) { + this.to = to; + this.weight = weight; + } + } + + public static void main(String[] args) throws IOException { + init(); + sol(); + } + + private static void init() throws IOException { + V = Integer.parseInt(br.readLine()); + ans = 0; + max = 0; + maxNode = 0; + + dist = new int[V + 1]; + Arrays.fill(dist, -1); + dist[1] = 0; + + graph = new List[V + 1]; + for (int i = 1; i <= V; i++) { + graph[i] = new ArrayList<>(); + } + + for (int i = 0; i < V; i++) { + st = new StringTokenizer(br.readLine()); + + int v = Integer.parseInt(st.nextToken()); + + int u; + while ((u = Integer.parseInt(st.nextToken())) != -1) { + int w = Integer.parseInt(st.nextToken()); + graph[v].add(new Node(u, w)); + } + } + } + + private static void sol() throws IOException { + dfs(1, 0); + findMax(); + + max = 0; + Arrays.fill(dist, -1); + dist[maxNode] = 0; + dfs(maxNode, 0); + findMax(); + + bw.write(max + ""); + bw.flush(); + bw.close(); + br.close(); + } + + private static void dfs(int v, int w) throws IOException { + for (Node u : graph[v]) { + if (dist[u.to] != -1) continue; + + dist[u.to] = u.weight + w; + dfs(u.to, dist[u.to]); + } + } + + private static void findMax() { + for (int i = 1; i <= V; i++) { + if (max < dist[i]) { + max = Math.max(max, dist[i]); + maxNode = i; + } + } + } + +} +``` \ No newline at end of file