๊ธฐ๋ก๋ฐฉ

BOJ_1976 : ์—ฌํ–‰ ๊ฐ€์ž ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1976 : ์—ฌํ–‰ ๊ฐ€์ž

Soom_1n 2024. 4. 22. 00:16

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

 

1976๋ฒˆ: ์—ฌํ–‰ ๊ฐ€์ž

๋™ํ˜์ด๋Š” ์นœ๊ตฌ๋“ค๊ณผ ํ•จ๊ป˜ ์—ฌํ–‰์„ ๊ฐ€๋ ค๊ณ  ํ•œ๋‹ค. ํ•œ๊ตญ์—๋Š” ๋„์‹œ๊ฐ€ N๊ฐœ ์žˆ๊ณ  ์ž„์˜์˜ ๋‘ ๋„์‹œ ์‚ฌ์ด์— ๊ธธ์ด ์žˆ์„ ์ˆ˜๋„, ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๋™ํ˜์ด์˜ ์—ฌํ–‰ ์ผ์ •์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ์—ฌํ–‰ ๊ฒฝ๋กœ๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ธ

www.acmicpc.net


 


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

  • N๊ฐœ์˜ ๋„์‹œ ์ค‘ M๊ฐœ์˜ ๋„์‹œ๋ฅผ ์—ฌํ–‰ํ•˜๊ณ ์ž ํ•œ๋‹ค.
  • ๊ฒฝ๋กœ๊ฐ€ ์žˆ์–ด์„œ ์—ฌํ–‰์ด ๊ฐ€๋Šฅํ•˜๋ฉด YES, ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด NO๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

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

  • ์—ฌํ–‰ ํ•˜๊ณ ์ž ํ•˜๋Š” M๊ฐœ์˜ ๋„์‹œ ๊ฐ„์— ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š”์ง€ ํŒŒ์•…ํ•˜๋Š” ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ๊ฐ™์€ ์ง‘ํ•ฉ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
    • ๋ถ„๋ฆฌ ์ง‘ํ•ฉ(Disjoint Set)์œผ๋กœ N๊ฐœ์˜ ๋„์‹œ๋“ค์˜ ์ง‘ํ•ฉ์„ ๋‚˜๋ˆˆ๋‹ค.
    • ์—ฌํ–‰ ๊ณ„ํš์ƒ์˜ ๋„์‹œ๋“ค์ด ๊ฐ™์€ ์ง‘ํ•ฉ์— ์žˆ์œผ๋ฉด ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.
    • ๋ถ„๋ฆฌ ์ง‘ํ•ฉ์ด ์•„๋‹ˆ๋ผ BFS๋‚˜ DFS๋กœ Brute Force ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ–ˆ๋‹ค๋ฉด, N์ด 200์ด๊ณ  M์ด 1000 ์ด๋ฉด์„œ 1์žํ˜• ๊ทธ๋ž˜ํ”„์ด๊ณ  ๋์—์„œ ๋์„ ์˜ค๊ณ ๊ฐ„๋‹ค๋ฉด 200^1000์ด๋ผ๋Š” ๋ง๋„ ์•ˆ๋˜๋Š” ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๋‚˜์˜จ๋‹ค.
    • ๋ถ„๋ฆฌ ์ง‘ํ•ฉ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(logN) ์ด๋ฏ€๋กœ ๊ฐ„๋‹จํžˆ ํ•ด๊ฒฐ๋œ๋‹ค.
  • ๊ฒฝ๋กœ ์œ ๋ฌด๊ฐ€ N * N ์ธ์ ‘ ํ–‰๋ ฌ๋กœ ์ฃผ์–ด์ง€๋Š”๋ฐ, ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋‹ค๋Š” 1 ์ž…๋ ฅ ์‹œ union ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • ๋งˆ์ง€๋ง‰ M๊ฐœ์˜ ์—ฌํ–‰ ๊ฒฝ๋กœ๊ฐ€ ๊ฐ™์€ ์ง‘ํ•ฉ์— ์žˆ๋Š”์ง€ find ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

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

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 = null;

        int N = Integer.parseInt(br.readLine());
        int M = Integer.parseInt(br.readLine());

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

        for (int i = 1; i <= N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 1; j <= N; j++) {
                int a = Integer.parseInt(st.nextToken());
                if (a == 1) {
                    union(i, j);
                }
            }
        }

        st = new StringTokenizer(br.readLine());
        int set_parent = find(Integer.parseInt(st.nextToken()));
        String answer = "YES";

        for (int i = 1; i < M; i++) {
            if (set_parent != find(Integer.parseInt(st.nextToken()))) {
                answer = "NO";
                break;
            }
        }

        bw.write(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) {
            parent[ap] = bp;
            return true;
        }
        return false;
    }
}

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

  • ๋ถ„๋ฆฌ ์ง‘ํ•ฉ์„ ์œ„ํ•ด union(), find() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, union() ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜๊ฐ’์€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ boolean์ด ์•„๋‹ˆ๋ผ void ๊ฐ€ ๋˜์–ด๋„ ์ƒ๊ด€ ์—†๋‹ค.
  • ๋ถ„๋ฆฌ ์ง‘ํ•ฉ์„ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•œ ํ›„, ์—ฌํ–‰ ๊ฒฝ๋กœ ์ฒซ ๋ฒˆ์งธ ๋„์‹œ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ ๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•œ ๋’ค, ๋‹ค์Œ ์—ฌํ–‰ ๊ฒฝ๋กœ ๋„์‹œ๋“ค์˜ ๋ถ€๋ชจ ๋…ธ๋“œ ๋ฒˆํ˜ธ์™€ ๊ฐ™์€์ง€ ํ™•์ธํ•œ๋‹ค.
    • ๋ชจ๋‘ ๊ฐ™์œผ๋ฉด YES, ํ•˜๋‚˜๋ผ๋„ ๋‹ค๋ฅด๋ฉด NO๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  •  ๋ถ„๋ฆฌ ์ง‘ํ•ฉ์„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์„ ํƒํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค.
728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_2573 : ๋น™์‚ฐ  (0) 2024.04.27
BOJ_14499 : ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ  (0) 2024.04.26
BOJ_2580 : ์Šค๋„์ฟ   (0) 2024.04.19
BOJ_1707 : ์ด๋ถ„ ๊ทธ๋ž˜ํ”„  (0) 2024.04.13
BOJ_3190 : ๋ฑ€  (0) 2024.04.11