๊ธฐ๋ก๋ฐฉ

BOJ_1647 : ๋„์‹œ ๋ถ„ํ•  ๊ณ„ํš ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1647 : ๋„์‹œ ๋ถ„ํ•  ๊ณ„ํš

Soom_1n 2024. 3. 14. 15:52

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

1647๋ฒˆ: ๋„์‹œ ๋ถ„ํ•  ๊ณ„ํš

์ฒซ์งธ ์ค„์— ์ง‘์˜ ๊ฐœ์ˆ˜ N, ๊ธธ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 2์ด์ƒ 100,000์ดํ•˜์ธ ์ •์ˆ˜์ด๊ณ , M์€ 1์ด์ƒ 1,000,000์ดํ•˜์ธ ์ •์ˆ˜์ด๋‹ค. ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ M์ค„์— ๊ฑธ์ณ ๊ธธ์˜ ์ •๋ณด๊ฐ€ A B C ์„ธ ๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ฃผ์–ด์ง€๋Š”๋ฐ A๋ฒˆ

www.acmicpc.net


๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • N๊ฐœ์˜ ์ง‘๊ณผ M๊ฐœ์˜ ์ง‘ ์‚ฌ์ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ธธ์ด ์žˆ๋‹ค.
  • ์„œ๋กœ ์ด์–ด์ง€์ง€ ์•Š๋Š” ๋งˆ์„ 2๊ฐœ๋ฅผ ๋งŒ๋“œ๋ ค๊ณ  ํ•˜๋Š”๋ฐ, ๊ฐ ๋งˆ์„ ์•ˆ์—์„œ๋Š” ์ง‘๋ผ๋ฆฌ ๊ธธ์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  • ๊ธธ์˜ ์œ ์ง€๋น„ ํ•ฉ์˜ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ๋งˆ์„ ์•ˆ์—์„œ ์ง‘ ์‚ฌ์ด์— ๊ธธ์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๊ณ , ๊ทธ ๊ฐ€์ค‘์น˜๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ, ๋‘ ๊ฐœ์˜ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ(MST) ๊ฐ€ ๋งŒ๋“ค์–ด์ ธ์•ผ ํ•œ๋‹ค.
  • ์—ฌ๊ธฐ์„œ๋Š” ํฌ๋ฃจ์Šค์นผ(Kruskal) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ–ˆ๋‹ค. 
    • ๊ฐ„์„ ์„ ๋ชจ๋‘ ์ž…๋ ฅ๋ฐ›๊ณ , ์œ ์ง€๋น„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
    • ์ฐจ๋ก€๋กœ ์ง‘ํ•ฉ์„ ํ•ฉ์น  ์ˆ˜ ์žˆ๋Š”์ง€ Union-Find๋กœ ํ™•์ธํ•˜๋ฉฐ, ์ง‘ํ•ฉ์ด 2๊ฐœ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
  • ์ตœ์ข… ๋ˆ„์  ์œ ์ง€๋น„๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class Main {
    private static int[] parent;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        if (N == 2) {
            bw.write("0");
            bw.flush();
            return;
        }

        parent = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            parent[i] = i;
        }

        Edge[] edges = new Edge[M];
        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            edges[i] = new Edge(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
        }

        Arrays.sort(edges, Comparator.comparing(o -> o.cost));

        long answer = 0;
        int set = N;
        for (Edge e : edges) {
            if (union(e.start, e.end)) {
                set--;
                answer += e.cost;
            }

            if (set == 2) break;
        }

        bw.write(Long.toString(answer));
        bw.flush();
    }

    private static boolean union(int a, int b) {
        int ap = find(a);
        int bp = find(b);

        if (ap == bp) {
            return false;
        } else {
            parent[bp] = ap;
            return true;
        }
    }

    private static int find(int a) {
        if (parent[a] == a) {
            return a;
        }
        return parent[a] = find(parent[a]);
    }

    private static class Edge {
        int start, end, cost;

        public Edge(int start, int end, int cost) {
            this.start = start;
            this.end = end;
            this.cost = cost;
        }
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ง‘์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. N์ด 2์ด๋ฉด ๊ฐ„์„ ์˜ ์ˆ˜ ์ƒ๊ด€ ์—†์ด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • Union-Find์—์„œ ์ง‘ํ•ฉ ํ™•์ธ์„ ์œ„ํ•œ ๋ถ€๋ชจ ์ธ๋ฑ์Šค ์ €์žฅ ๋ฐฐ์—ด parent๋ฅผ ๋งŒ๋“ค์–ด ์ธ๋ฑ์Šค๋ฅผ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  • ๋‘ ์ธ๋ฑ์Šค์™€ ์œ ์ง€๋น„๋ฅผ ์ €์žฅ ํ•  ๊ฐ„์„  ํด๋ž˜์Šค Edge๋ฅผ ๋งŒ๋“ค์–ด ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  • Edge๋ฐฐ์—ด edges๋ฅผ ์œ ์ง€๋น„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
  • ๊ฐ ๊ฐ„์„ ์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ํ™•์ธํ•˜๋ฉฐ, ์ง‘ํ•ฉ์ด ํ•ฉ์ณ์ง€๋ฉด ์ง‘ํ•ฉ ๊ฐœ์ˆ˜ set์„ -1ํ•˜๊ณ  ์œ ์ง€๋น„๋ฅผ answer์— ๋ˆ„์ ํ•œ๋‹ค.
  • ์ตœ์ข… answer์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ฒ˜์Œ์— 97%์—์„œ ํ‹€๋ ค์„œ answer๊ฐ€ int๊ฐ€ ์•„๋‹ˆ๋ผ long์ด์–ด์„œ ๊ทธ๋Ÿฐ๊ฐ€ํ•˜๊ณ  ๊ณ ์ณค์ง€๋งŒ ์—ฌ์ „ํžˆ ํ‹€๋ ธ๋‹ค.
    • answer์˜ ์ตœ๋Œ€๊ฐ’์€ 10์–ต์œผ๋กœ int๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๊ณ„์‚ฐํ–ˆ์—ˆ๋Š”๋ฐ, ์•„๋‹ˆ๋‚˜ ๋‹ค๋ฅผ๊นŒ ์ด์ƒ์—†์—ˆ๋‹ค.
    • ํ‹€๋ฆฐ ์›์ธ์€ n์ด 2์ผ ๋•Œ๋Š” union์ด ํ•„์š”์—†๋Š”๋ฐ, ์ฝ”๋“œ๋Š” ๋ฌด์กฐ๊ฑด ํ•œ ๋ฒˆ union์ด ์ด๋ฃจ์–ด ์ง€๋„๋ก ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • lowcase๋ฅผ ํ™•์ธํ•˜๋ ค๊ณ  1 2 / 1 2 3 ์ž…๋ ฅ์„ ํ–ˆ์—ˆ๋Š”๋ฐ, ๋ฐ”๋ณด๊ฐ™์ด 3์ด ๋‚˜์˜จ๊ฑธ ํ™•์ธํ•˜๊ณ  ์ด์ƒ์ด ์žˆ๋Š”์ง€ ๋ชฐ๋ž๋‹ค.
    • 0์ด ๋‚˜์™€์•ผ ์ •์ƒ์ด์—ˆ๊ณ , ํ‹€๋ฆฐ ์ดํ›„์— ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ์„ ๋ณด๊ณ  ๊นจ๋‹ฌ์•˜๋‹ค.
    • ๋ญ”๊ฐ€ ๋‹ค๋ฅธ ์ด์œ ๊ฐ€ ์žˆ์„๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ , ์ง์ ‘ ๊ณ„์‚ฐํ•˜๊ณ  ํ…Œ์ŠคํŠธํ•œ ์ผ€์ด์Šค๋ฅผ ๊ฐ„๊ณผํ•œ๊ฒŒ ๋ถ€๋„๋Ÿฝ๋‹ค.
    • ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ์„ ๋ณด๊ธฐ์ „์— ๋”๋”์šฑ ๊ผผ๊ผผํ•˜๊ฒŒ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฒ„๋ฆ‡์„ ๋“ค์—ฌ์•ผ๊ฒ ๋‹ค. ์กฐ๊ธˆ ์‹œ๊ฑด๋ฐฉ์ง„ ํ…Œ๋„์˜€๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
  • ์›ƒ๊ธด๊ฑด '์•„ ์ด๊ฑฐ 2๊ฐœ ์ง‘ํ•ฉ ๋‚˜๋ˆ ์„œ ํ™•์ธํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ, ์ •๋ ฌํ•˜๊ณ  Union-Find' ์“ฐ๋ฉด ๋˜๊ฒ ๋‹ค' ํ•˜๊ณ  ๋ฌธ์ œ ํ’€์ดํ•œ ๋’ค ์งˆ๋ฌธ๊ฒŒ์‹œํŒ์— MST, ํฌ๋ฃจ์Šค์นผ, ํ”„๋ฆผ ์–˜๊ธฐ๊ฐ€ ๋‚˜์™€์„œ ์™œ ๊ทธ๋ ‡๊ฒŒ ํ’€์ดํ•˜์ง€? ํ•˜๊ณ  ์˜๋ฌธ์„ ๊ฐ–์—ˆ๋‹ค.
    • ์•Œ๊ณ ๋ณด๋‹ˆ ๋‚ด ํ’€์ด๊ฐ€ ํฌ๋ฃจ์Šค์นผ์ด์—ˆ๋‹ค...๋„ˆ๋ฌด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ MST ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ก ์ด ๋งŽ์ด ๋นˆ์•ฝํ•˜๋‹ค๋Š”๊ฑธ ๋‹ค์‹œ ํ•œ๋ฒˆ ๊นจ๋‹ฌ์•˜๋‹ค.
  • ์ด๋ฒˆ์—” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด ๊ณ„ํš์„ ์ ์–ด๊ฐ€๋ฉฐ ํ’€์ดํ–ˆ๋‹ค.

728x90