Notice
Recent Posts
Recent Comments
Today
Total
01-11 01:45
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ ๊ตฌํ•˜๊ธฐ] ๋ฐฑ์ค€ 1167 (JAVA) - dfs / ๋‹ค์ต์ŠคํŠธ๋ผ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ ๊ตฌํ•˜๊ธฐ] ๋ฐฑ์ค€ 1167 (JAVA) - dfs / ๋‹ค์ต์ŠคํŠธ๋ผ

WonderJay 2023. 3. 20. 18:15
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

1167๋ฒˆ: ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„

ํŠธ๋ฆฌ๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒซ ๋ฒˆ์งธ ์ค„์—์„œ๋Š” ํŠธ๋ฆฌ์˜ ์ •์ ์˜ ๊ฐœ์ˆ˜ V๊ฐ€ ์ฃผ์–ด์ง€๊ณ  (2 ≤ V ≤ 100,000)๋‘˜์งธ ์ค„๋ถ€ํ„ฐ V๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ„์„ ์˜ ์ •๋ณด๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฃผ์–ด์ง„๋‹ค. ์ •์  ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ V๊นŒ์ง€

www.acmicpc.net

๋ฌธ์ œ ์ƒํ™ฉ์€ ๋˜๊ฒŒ ๋‹จ์ˆœํ•˜๋‹ค.

 

๊ฐ€์ค‘์น˜๊ฐ€ ์กด์žฌํ•˜๋Š” ํŠธ๋ฆฌ ๊ตฌ์กฐ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ,

 

์ž„์˜์˜ A ๋…ธ๋“œ๋ถ€ํ„ฐ B ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•  ๊ฒƒ์ด๊ณ 

 

๊ทธ ์ค‘ ์ตœ์žฅ ๊ฒฝ๋กœ๋ฅผ ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์ด๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค.

 

ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค.

 

์Œ.. ์–ด๋–ป๊ฒŒ ํ’€์ง€?

 

์ผ๋‹จ ์‹œ๊ฐ„ ์ œํ•œ์€ 2์ดˆ๋กœ ํ‰๋ฒ”ํ•œ ํŽธ์ด๊ณ ,

 

๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‹ˆ๊นŒ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” 100000 ๊ฐœ์ด๋‹ค.

 

์—์ง€์˜ ๊ฐœ์ˆ˜๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์•˜์œผ๋‚˜, ์ตœ๋Œ€ 100000 - 1 ๊ฐœ๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

 

์–ด์จ‹๋“  ๊ฐ„์— ์ตœ์žฅ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์น˜๋ฉด

 

๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์กฐ๊ธˆ ๋ณ€ํ˜•ํ•ด์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ์„ ํ…๋ฐ

 

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(VlogE) ์ด๊ณ 

 

100000 * Log(100000) ~= 500000 ์ด๋‹ค.

 

๊ทธ๋Ÿผ ์•„๋ฌด ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ์ตœ์žฅ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜์ž.

 

๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ ๊ฐ€์žฅ ๋จผ ๊ฑฐ๋ฆฌ์— ํ•ด๋‹นํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์ฐพ์ž.

 

๊ทธ ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ ์ตœ์žฅ๊ฑฐ๋ฆฌ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•œ๋ฒˆ ๋” ์ˆ˜ํ–‰ํ•˜๋ฉด

 

ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿง ์™œ ๋‘ ๋ฒˆ ์ˆ˜ํ–‰ํ•ด์•ผ ํ• ๊นŒ?

โ–ถ ์ตœ์žฅ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๋Š” ํ•จ์ˆ˜๋ฅผ getLongestPath(int v) ๋ผ๊ณ  ํ•˜์ž. ์šด์ด ์ข‹๊ฒŒ๋„ v ๊ฐ€ ๋ฃจํŠธ ํ˜น์€ ๋ฆฌํ”„ ๋…ธ๋“œ๋ผ๋ฉด ํ•œ ๋ฒˆ๋งŒ ์ˆ˜ํ–‰ํ•ด๋„ ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ์–ด๋–ค ๋…ธ๋“œ๊ฐ€ ๋ฃจํŠธ , ๋ฆฌํ”„ ๋…ธ๋“œ์ธ ์ง€๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— v ๊ฐ’์œผ๋กœ๋Š” ์ž„์˜์˜ ๋…ธ๋“œ๋ฅผ ํƒํ•  ์ˆ˜ ๋ฐ–์— ์—†๋‹ค. ์ž„์˜๋กœ ์„ ํƒํ•œ v ๋…ธ๋“œ๊ฐ€ ํŠธ๋ฆฌ์˜ ํ•œ ๊ฐ€์šด๋ฐ ์œ„์น˜ํ•œ ๋…ธ๋“œ๋ผ๊ณ  ์ƒ์ƒํ•ด๋ณด์ž. ๊ทธ๋Ÿผ v ๋…ธ๋“œ์—์„œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆ˜ํ–‰ํ•˜๋”๋ผ๋„, ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์„ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ v ๋…ธ๋“œ์—์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋…ธ๋“œ(A)๋ฅผ ์ฐพ๊ณ , ํ•ด๋‹น ๋…ธ๋“œ์—์„œ getLongestPath ๋ฅผ ๋‹ค์‹œ ์ˆ˜ํ–‰ํ•ด์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ํ•ด๋‹น ํŠธ๋ฆฌ์—์„œ ์ตœ์žฅ ๊ฒฝ๋กœ๊ฐ€ ๋  ๊ฒƒ์ด๊ณ  ์ด๊ฒƒ์ด ๋ฐ”๋กœ ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์ด๋‹ค. (์กฐ๊ธˆ ๋” ์—„๋ฐ€ํ•˜๊ฒŒ ์ฆ๋ช…์„ ํ•˜๊ณ  ์‹ถ์€๋ฐ... ์ง€๊ธˆ์€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค ๐Ÿ˜ข) 

 

์ด๋ ‡๊ฒŒ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ 2 ๋ฒˆ ์ˆ˜ํ–‰ํ•ด์„œ ๊ตฌํ•  ์ˆ˜๋„ ์žˆ๊ณ 

 

๊ทธ๋ƒฅ DFS ๋กœ dist ๋ฐฐ์—ด์„ ๊ฐฑ์‹ ํ•ด๊ฐ€๋ฉด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

DFS ๋ฅผ ์ˆ˜ํ–‰ํ•  ๊ฒฝ์šฐ O(n+m) ~= O(200000) ์ด๋ฏ€๋กœ

 

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณด๋‹ค๋Š” ์กฐ๊ธˆ ๋” ๋น ๋ฅธ ์‹œ๊ฐ„ ๋ณต์žก๋„๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

 

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ณ€ํ˜•ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์กฐ๊ธˆ ๊ตฌํ˜„์ด ๋ฒˆ๊ฑฐ๋กœ์šธ ์ˆ˜ ์žˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋จผ์ € DFS ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

 

import java.io.*;
import java.util.*;

public class Main {
    static int v;
    static ArrayList<Node>[] graph;
    static int[] dist;
    static boolean[] visited;

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk;

        v = Integer.parseInt(br.readLine()); // ์ •์ ์˜ ๊ฐœ์ˆ˜
        dist = new int[v + 1];
        visited = new boolean[v + 1];

        graph = new ArrayList[v + 1];
        for (int i = 0; i <= v; i++) {
            graph[i] = new ArrayList<>();
        }

        for (int i = 1; i <= v; i++) {
            stk = new StringTokenizer(br.readLine());

            int a = Integer.parseInt(stk.nextToken());
            while (true) {
                int b = Integer.parseInt(stk.nextToken());
                if (b == -1) break;
                int w = Integer.parseInt(stk.nextToken());
                graph[a].add(new Node(b, w));
            }
        }

        dfs(1);
        int max = -1;
        int max_node = -1;
        for (int i = 1; i <= v; i++) {
            if (dist[i] >= max) {
                max = dist[i];
                max_node = i;
            }
        }

        dist = new int[v + 1];
        visited = new boolean[v + 1];
        dfs(max_node);
        for (int i = 1; i <= v; i++) {
            max = Math.max(max, dist[i]);
        }

        bw.write(max + "\n");
        bw.flush();
        br.close();
        bw.close();
    }

    static class Node {
        int to;
        int w;

        public Node(int to, int w) {
            this.to = to;
            this.w = w;
        }
    }

    static void dfs(int v) {
        visited[v] = true;
        for (Node ele : graph[v]) {
            // ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰
            if (!visited[ele.to]) {
                // ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
                visited[ele.to] = true; // ๋ฐฉ๋ฌธ
                dist[ele.to] = dist[v] + ele.w;
                dfs(ele.to);
            }
        }
    }
}

์œ„ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด AC ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

 

๊ทธ๋Ÿฌ๋ฉด ์ด์ œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด๋ณด์ž.

 

dfs ํ•จ์ˆ˜ ๋Œ€์‹ ์— Dijkstra ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ๋Œ€์ฒดํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

์ฃผ์˜ํ•  ๊ฒƒ์€ ์ตœ์žฅ๊ฑฐ๋ฆฌ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ฏ€๋กœ

 

์ดˆ๊ธฐํ™” ํ•˜๋Š” ๋ถ€๋ถ„์ด๋ผ๋˜๊ฐ€,

 

๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ถ€๋ถ„์˜ ์กฐ๊ฑด๋ฌธ,

 

Node ์˜ Comparable ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ์—์„œ

 

์ผ๋ฐ˜์ ์ธ ์ตœ๋‹จ๊ฑฐ๋ฆฌ Dijkstra ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ • ๋ฐ˜๋Œ€๋กœ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค!

 

import java.io.*;
import java.util.*;

public class Main {
    static int v;
    static ArrayList<Node>[] graph;
    static int[] dist;
    static boolean[] visited;

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk;

        v = Integer.parseInt(br.readLine()); // ์ •์ ์˜ ๊ฐœ์ˆ˜
        dist = new int[v + 1];
        visited = new boolean[v + 1];

        graph = new ArrayList[v + 1];
        for (int i = 0; i <= v; i++) {
            graph[i] = new ArrayList<>();
        }

        for (int i = 1; i <= v; i++) {
            stk = new StringTokenizer(br.readLine());

            int a = Integer.parseInt(stk.nextToken());
            while (true) {
                int b = Integer.parseInt(stk.nextToken());
                if (b == -1) break;
                int w = Integer.parseInt(stk.nextToken());
                graph[a].add(new Node(b, w));
            }
        }

        dijkstra(new Node(1, 0));
        int max = -1;
        int max_node = -1;
        for (int i = 1; i <= v; i++) {
            if (dist[i] >= max) {
                max = dist[i];
                max_node = i;
            }
        }

        dist = new int[v + 1];
        visited = new boolean[v + 1];

        dijkstra(new Node(max_node, 0));
        for (int i = 1; i <= v; i++) {
            max = Math.max(max, dist[i]);
        }

        bw.write(max + "\n");
        bw.flush();
        br.close();
        bw.close();
    }

    static class Node implements Comparable<Node> {
        int to;
        int w;

        public Node(int to, int w) {
            this.to = to;
            this.w = w;
        }

        @Override
        public int compareTo(Node o) {
            if(this.w <= o.w){
                return 1;
            } return -1;
        }
    }

    static void dijkstra(Node start){
        PriorityQueue<Node> pq = new PriorityQueue<>();
        Arrays.fill(dist, -Integer.MAX_VALUE);
        dist[start.to] = 0;
        visited[start.to] = true;
        pq.add(start);

        while(!pq.isEmpty()){
            Node now = pq.poll();
            for(Node ele : graph[now.to]){
                // ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ ํƒ์ƒ‰
                if(!visited[ele.to]){
                    if(dist[ele.to] < dist[now.to] + ele.w){
                        // ๋” ์ตœ์žฅ ๊ฒฝ๋กœ๋ฅผ ๋ฐœ๊ฒฌํ•œ๋‹ค๋ฉด
                        dist[ele.to] = dist[now.to] + ele.w;
                        visited[ele.to] = true;
                        pq.add(new Node(ele.to, dist[ele.to]));
                    }
                }
            }
        }
    }
}

 

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ณ€ํ˜•ํ•˜๋Š” ๊ณผ์ •์—์„œ ์‹ค์ˆ˜๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ์–ด์„œ

 

๊ทธ๋ƒฅ DFS ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์กฐ๊ธˆ ๋” ๊ฐ„๋‹จํ•˜๊ธฐ๋„ ํ•˜๊ณ 

 

์‹œ๊ฐ„ ์ œํ•œ๋„ ์กฐ๊ธˆ ๋” ๋„๋„ํ•˜๊ฒŒ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

2023. 03. 20. ์›”.

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments