๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Algorithm/Java][๋ฐฑ์ค€] 1967 ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„

๋ฐ˜์‘ํ˜•

[BOJ] 1967 ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„

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

๋ฌธ์ œ ์ ‘๊ทผ

dfs๋ฅผ ์ด์šฉํ•ด์„œ ์ ‘๊ทผํ•˜์˜€๋‹ค.
1๋ฒˆ ๋…ธ๋“œ์—์„œ dfs๋ฅผ ์‹œ์ž‘ํ•ด์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋…ธ๋“œ์˜ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ–ˆ๋‹ค๊ฐ€, ํ•ด๋‹น ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋‹ค์‹œ dfs๋ฅผ ์‹œ์ž‘ํ•ด์„œ ๊ทธ์ค‘์— ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜์˜€๋‹ค.

Code

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

public class BOJ1967 {
    static class Node {
        int end;
        int value;
        Node(int end, int value){
            this.end = end;
            this.value = value;
        }
    }
    static ArrayList<Node>[] tree;
    static boolean[] visited;
    static int n;
    static int max = 0, maxIndex = 0;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        tree  = new ArrayList[n+1];
        for(int i = 0; i<=n; i++){
            tree[i] = new ArrayList<>();
        }

        for(int i = 0; i<n-1; i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            int from = Integer.parseInt(st.nextToken());
            int to = Integer.parseInt(st.nextToken());
            int value = Integer.parseInt(st.nextToken());
            tree[from].add(new Node(to,value));
            tree[to].add(new Node(from, value));
        }
        // 1๋ฒˆ ๋…ธ๋“œ๋ถ€ํ„ฐ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•˜์—ฌ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰
        visited = new boolean[n+1];
        visited[1] = true;
        dfs(1,0);

        // ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋…ธ๋“œ์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰
        visited = new boolean[n+1];
        visited[maxIndex] = true;
        dfs(maxIndex,0);

        System.out.println(max);
    }

    /**
     * ์žฌ๊ท€ ํ˜ธ์ถœ์„ ์ด์šฉํ•œ dfs ํƒ์ƒ‰
     * @param i ํƒ์ƒ‰ํ•˜๋Š” ๋…ธ๋“œ ๋ฒˆํ˜ธ
     * @param score ์ง€๊ธˆ๊นŒ์ง€ ๊ฒฝ๋กœ์˜ ๊ธธ์ด
     */
    public static void dfs(int i, int score){
        // max์— ๊ฐ€์žฅ ๊ธด ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋ฅผ ์ €์žฅํ•˜๊ณ , maxIndex์— ํ•ด๋‹น ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ ์ €์žฅ
        if(score  > max) {
            max = score;
            maxIndex = i;
        }
        for(Node n : tree[i]){
            if(!visited[n.end]){
                visited[n.end] = true;
                dfs(n.end, score+n.value);
            }
        }

    }
}

์–ด๋ ค์› ๋˜ ์  / ๋ฐฐ์šด ์  / ๋Š๋‚€ ์ 

์ผ๋‹จ ๊ทธ๋ž˜ํ”„๋ฅผ ์ €์žฅํ•  ๋•Œ ๋ฐฐ์—ด๋กœ ์ €์žฅ์„ ํ•˜๊ฒŒ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋œฌ๋‹ค...
๊ทธ๋ž˜ํ”„๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ์‹์ด ์žˆ๋Š”๋ฐ, ์ด ๋ฌธ์ œ๋Š” ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•˜๊ฒŒ ๋˜๋ฉด n*n์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋œจ๋Š”๊ฒƒ์ด๋‹ค.
๊ทธ๋ž˜์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ๊ทธ๋ž˜ํ”„๋ฅผ ์ €์žฅํ–ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ Nodeํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์—ˆ๊ณ , end(ํ–ฅํ•˜๋Š” ๋…ธ๋“œ ์ธ๋ฑ์Šค)์™€ value(๊ฒฝ๋กœ์˜ ๊ธธ์ด)๋ฅผ ์ €์žฅํ–ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ฒ˜์Œ์—๋Š” ๋ชจ๋“  ์ธ๋ฑ์Šค์—์„œ dfs๋ฅผ ์‹คํ–‰ํ•˜์˜€๋Š”๋ฐ, 1๋ฒˆ ์ธ๋ฑ์Šค์—์„œ dfs๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ฑฐ๊ธฐ์—์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ๋งŒ ๋‹ค์‹œ dfs๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ต์ด ๋‚˜์˜จ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.(๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ ๋ธ”๋กœ๊ทธ๋ฅผ ๋ณด๋‹ˆ๊นŒ ๋‹ค ์ด๋ ‡๊ฒŒ ํ’€์—ˆ๋‹ค...)
์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ๋˜๋ฉด dfs๋ฅผ 2๋ฒˆ๋ฐ–์— ์‹คํ–‰ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‹œ๊ฐ„์ด 2696ms์—์„œ 232ms๊นŒ์ง€ ์ค„์–ด๋“ค๊ฒŒ ๋˜์—ˆ๋‹ค. ์—ญ์‹œ ๊ณจ๋“œ๋Š” ์–ด๋ ต๋‹ค.

๋ฐ˜์‘ํ˜•