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; + } + } +} + +```