본문 바로가기

Algorithm

[Algorithm/Java][백준] 1167 트리의 지름

반응형

[BOJ] 1167 트리의 지름

https://www.acmicpc.net/problem/1167

문제 접근

이 문제는 아래 문제와 거의 똑같은 문제이다. 트리를 입력해주는 방식만 다르고 알고리즘은 똑같은 문제여서 쉽게 풀 수 있었다.
1967 트리의 지름

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class BOJ1167 {
    static class Node{
        int to;
        int dist;
        public Node(int to, int dist) {
            this.to = to;
            this.dist = dist;
        }
    }
    static int V;
    static ArrayList<Node>[] graph;
    static boolean[] visited;
    static int max = 0, maxIndex = 0;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        V = Integer.parseInt(br.readLine());
        graph = new ArrayList[V+1];
        for(int i = 0; i<=V; i++){
            graph[i] = new ArrayList<>();
        }
        for(int i = 1 ; i<=V; i++){
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            int idx = Integer.parseInt(st.nextToken());
            while(true){
                int to = Integer.parseInt(st.nextToken());
                if(to == -1){
                    break;
                }
                int dist = Integer.parseInt(st.nextToken());
                graph[idx].add(new Node(to,dist));
            }
        }
        visited = new boolean[V+1];
        dfs(1,0);

        visited = new boolean[V+1];
        dfs(maxIndex,0);

        System.out.println(max);
    }

    /**
     * dfs 탐색을 통해서 가장 멀리 있는 노드와의 경로 길이와 그 노드의 인덱스를 구하는 함수.
     * @param i 탐색하려는 노드 인덱스
     * @param dist 지금까지의 경로의 길이
     */
    static void dfs(int i, int dist){
        if(dist > max){
            max = dist;
            maxIndex = i;
        }
        visited[i] = true;
        for(Node n : graph[i]){
            if(!visited[n.to]){
                dfs(n.to,dist+n.dist);
            }
        }
    }
}

어려웠던 점 / 배운 점 / 느낀 점

바로 어제 풀었던 문제와 거의 똑같은 문제여서 쉽게 풀 수 있었다.
그런데 다시 코드를 짜다보니까 변수명이나 약간의 알고리즘을 더 보기좋게 바꾸었다.(나름대로 리팩토링)

반응형