๊ธฐ๋ก๋ฐฉ

BOJ_20040 : ์‚ฌ์ดํด ๊ฒŒ์ž„ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_20040 : ์‚ฌ์ดํด ๊ฒŒ์ž„

Soom_1n 2024. 3. 29. 13:34

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

 

20040๋ฒˆ: ์‚ฌ์ดํด ๊ฒŒ์ž„

์‚ฌ์ดํด ๊ฒŒ์ž„์€ ๋‘ ๋ช…์˜ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ฐจ๋ก€๋Œ€๋กœ ๋Œ์•„๊ฐ€๋ฉฐ ์ง„ํ–‰ํ•˜๋Š” ๊ฒŒ์ž„์œผ๋กœ, ์„  ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ํ™€์ˆ˜ ๋ฒˆ์งธ ์ฐจ๋ก€๋ฅผ, ํ›„ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ง์ˆ˜ ๋ฒˆ์งธ ์ฐจ๋ก€๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. ๊ฒŒ์ž„ ์‹œ์ž‘ ์‹œ 0 ๋ถ€ํ„ฐ n − 1 ๊นŒ์ง€ ๊ณ ์œ ํ•œ

www.acmicpc.net


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

  • n๊ฐœ์˜ ์ ๋“ค ์‚ฌ์ด์— m๊ฐœ์˜ ์„ ๋ถ„์„ ๊ทธ์„ ๋•Œ, ์‚ฌ์ดํด์ด ์ƒ๊ธฐ๋Š” ์‹œ์ ์„ ํ™•์ธํ•œ๋‹ค.
  • ์‚ฌ์ดํด์ด ์ฒ˜์Œ ์ƒ๊ธด ์ˆœ์„œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์‚ฌ์ดํด์ด ์ƒ๊ธฐ์ง€ ์•Š์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

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

  • ๋ถ„๋ฆฌ ์ง‘ํ•ฉ(Disjoint Set)์„ ์ด์šฉํ•ด ์‚ฌ์ดํด์ด ์ƒ๊ธฐ๋Š” ์‹œ์ ์„ ํŒŒ์•…ํ•œ๋‹ค.

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

import java.io.*;
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());

        parent = new int[n];
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }

        int answer = 0;

        for (int i = 1; i <= m; i++) {
            st = new StringTokenizer(br.readLine());
            if (!union(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()))) {
                answer = i;
                break;
            }
        }

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

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

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

        if (ap == bp) return false;
        else if (a == ap && b == bp) parent[ap] = bp;
        else parent[bp] = ap;

        return true;
    }
}

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

  • ๋ถ€๋ชจ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” int๋ฐฐ์—ด parent๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ฐ ์ธ๋ฑ์Šค ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  • find() ๋ฉ”์„œ๋“œ๋Š” ์ €์žฅ๋œ ์ธ๋ฑ์Šค๋ฅผ ํƒ€๊ณ  ๊ฐ€๋ฉฐ ์ตœ์ข… ๋ฃจํŠธ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๊ณ , ํ•ด๋‹น ์ธ๋ฑ์Šค๋กœ ์ž์‹ ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ ํ›„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • union() ๋ฉ”์„œ๋“œ๋Š” ๋‘ ์ ์˜ ๋ฃจํŠธ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š”๋‹ค.
    • ๋‘ ๋ฃจํŠธ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ™์œผ๋ฉด, ๊ฐ™์€ ์ง‘ํ•ฉ์— ์—ฐ๊ฒฐํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ์‚ฌ์ดํด์ด ์ƒ๊ธด๋‹ค. false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • ๊ธฐ๋ณธ์€ bp ์ธ๋ฑ์Šค์˜ ๋ถ€๋ชจ๋ฅผ ap๋กœ ์ €์žฅํ•œ๋‹ค.
    • ๋งŒ์•ฝ a๊ฐ€ ๋ถ€๋ชจ ์ธ๋ฑ์Šค๊ฐ€ ์—†๊ณ , b๋Š” ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ap ์ธ๋ฑ์Šค์— bp๋ฅผ ์ €์žฅํ•œ๋‹ค.
  • union() ๊ฒฐ๊ณผ๊ฐ€ false๊ฐ€ ๋‚˜์˜จ ์ˆœ์„œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ false๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  •  ๋ถ„๋ฆฌ ์ง‘ํ•ฉ ๋ฌธ์ œ์ธ๊ฑธ ๋ฐ”๋กœ ์•Œ์•„์„œ ์–ด๋ ต์ง€ ์•Š๊ฒŒ ํ’€์ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๊ทผ๋ฐ ์›ƒ๊ธด๊ฑด ์ง€๊ธˆ๊นŒ์ง€ ๋ถ„๋ฆฌ ์ง‘ํ•ฉ(Disjoint Set)์„ Union-Find ์•Œ๊ณ ๋ฆฌ์ฆ˜์ผ๊ณ  ๋ถˆ๋ €๋‹ค. ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ์ข‹์•˜์ง€๋งŒ ์ œ๋Œ€๋กœ ๊ธฐ์–ตํ•˜๋„๋ก ํ•˜์ž.
728x90