From 9565df6a8e3d6d727c941e876d6f3cf07333bd51 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Mon, 10 Mar 2025 17:33:42 +0900 Subject: [PATCH] =?UTF-8?q?[20250310]=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=EA=B9=80?= =?UTF-8?q?=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 \354\247\200\353\246\204.md" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 "suyeun84/202503/10 BOJ G4 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" diff --git "a/suyeun84/202503/10 BOJ G4 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" "b/suyeun84/202503/10 BOJ G4 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" new file mode 100644 index 00000000..792d586e --- /dev/null +++ "b/suyeun84/202503/10 BOJ G4 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" @@ -0,0 +1,58 @@ +```java +import java.io.*; +import java.util.*; + +public class boj1967 { + 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> graph; + static boolean[] visited; + static int answer = 0; + public static void main(String[] args) throws Exception { + nextLine(); + int n = nextInt(); + graph = new ArrayList<>(n+1); + visited = new boolean[n+1]; + for (int i = 0; i < n+1; i++) graph.add(new ArrayList<>()); + for (int i = 0; i < n-1; i++) { + nextLine(); + int a = nextInt(); + int b = nextInt(); + int c = nextInt(); + graph.get(a).add(new Node(b, c)); + graph.get(b).add(new Node(a, c)); + } + + for (int i = 1; i <= n; i++) { + if (graph.get(i).size() != 1) continue; + visited[i] = true; + dfs(i, 0); + visited[i] = false; + } + System.out.println(answer); + } + + static void dfs(int node, int total) { + for (Node n : graph.get(node)) { + if (visited[n.n]) continue; + visited[n.n] = true; + dfs(n.n, total + n.c); + visited[n.n] = false; + } + answer = Math.max(answer, total); + } + + static class Node { + int n; + int c; + public Node(int n, int c) { + this.n = n; + this.c = c; + } + } +} + +```