๊ธฐ๋ก๋ฐฉ

BOJ_1068 : ํŠธ๋ฆฌ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1068 : ํŠธ๋ฆฌ

Soom_1n 2023. 4. 7. 13:51

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

 

1068๋ฒˆ: ํŠธ๋ฆฌ

์ฒซ์งธ ์ค„์— ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” 0๋ฒˆ ๋…ธ๋“œ๋ถ€ํ„ฐ N-1๋ฒˆ ๋…ธ๋“œ๊นŒ์ง€, ๊ฐ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋งŒ์•ฝ ๋ถ€๋ชจ๊ฐ€ ์—†๋‹ค๋ฉด (๋ฃจํŠธ) -1์ด ์ฃผ์–ด์ง„๋‹ค

www.acmicpc.net



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

  • ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ ๋…ธ๋“œ์™€ ๊ฐ ์ธ๋ฑ์Šค ๋ณ„ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ ์ •๋ณด๊ฐ€ ์ž…๋ ฅ๋œ๋‹ค.
    • ํ•œ ๋…ธ๋“œ๋ฅผ ์ง€์šฐ๋ฉด ์•„๋ž˜ ์ž์† ๋…ธ๋“œ๋“ค์ด ๋ชจ๋‘ ์ง€์›Œ์ง„๋‹ค.
    • ๋‚จ์€ ๋ฆฌํ”„๋…ธ๋“œ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค.
    • ๋งŒ์•ฝ ์•„์ง ๋ถ€๋ชจ๋…ธ๋“œ๊ฐ€ ๋“ฑ์žฅํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด, ํ์— ๋„ฃ๊ณ  ์ˆœ์„œ๋ฅผ ๊ธฐ๋‹ค๋ฆฐ๋‹ค.
  • ์‚ญ์ œํ•  ๋•Œ  DFS๋ฅผ ์‚ฌ์šฉํ•ด ์ž์‹ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ์‚ญ์ œํ•œ๋‹ค.

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

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

public class Main {
    private static int N, cnt;
    private static ArrayList<Integer>[] adjList;

    private static void dfs_delete(int target) {
        while (adjList[target].size() > 0) {
            dfs_delete(adjList[target].get(0));
            adjList[target].remove(0);
        }
        adjList[target] = null;
    }

    private static class Pair {
        int i, num;

        public Pair(int i, int num) {
            this.i = i;
            this.num = num;
        }
    }

    private static void print() {
        System.out.println("=========================");
        for (ArrayList<Integer> arr : adjList) {
            System.out.println(arr);
        }
    }

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

        N = Integer.parseInt(st.nextToken());
        adjList = new ArrayList[N];

        Queue<Pair> queue = new ArrayDeque<>();
        int start = 0;

        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            int num = Integer.parseInt(st.nextToken());

            if (num == -1) {
                start = i;
                adjList[i] = new ArrayList<>();
            } else if (adjList[num] != null) {
                adjList[num].add(i);
                adjList[i] = new ArrayList<>();
                adjList[i].add(num);
            } else {
                queue.add(new Pair(i, num));
            }
        }

        while (!queue.isEmpty()) {
            Pair pair = queue.poll();
            int num = pair.num;
            int i = pair.i;
            if (adjList[num] != null) {
                adjList[num].add(i);
                adjList[i] = new ArrayList<>();
                adjList[i].add(num);
            } else {
                queue.add(new Pair(i, num));
            }
        }

//        print();

        // Delete node
        cnt = 0;
        int target = Integer.parseInt(br.readLine());
        if (target != start) { // ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์žˆ์œผ๋ฉด, ๋ถ€๋ชจ ๋…ธ๋“œ์—์„œ ํƒ€๊ฒŸ ์‚ญ์ œ
            adjList[adjList[target].get(0)].remove(Integer.valueOf(target));

            for (int i = 0; i < N; i++) { // ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ˆ๋ฉด, ๋ถ€๋ชจ๋…ธ๋“œ ์ •๋ณด ์‚ญ์ œ
                if (i != start)
                    adjList[i].remove(0);
            }

            dfs_delete(target);

            for (ArrayList<Integer> arr: adjList) {
                if (arr != null && arr.size() == 0) cnt++;
            }
        }
//        print();

        // Output
        System.out.println(cnt);
    }
}

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

  • ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ adjList์— ํŠธ๋ฆฌ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค.
    • ์•„์ง ๋ถ€๋ชจ๊ฐ€ ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š์•˜๋‹ค๋ฉด ํ์— ๋„ฃ๊ณ  ๊ธฐ๋‹ค๋ฆฐ๋‹ค.
  • ๋…ธ๋“œ ์‚ญ์ œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
    • ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๋ฉด ๊ณ„์‚ฐํ•  ๊ฒƒ๋„ ์—†์ด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.
    • ๋ถ€๋ชจ๋…ธ๋“œ๊ฐ€ ์žˆ์œผ๋ฉด ๋ถ€๋ชจ ๋…ธ๋“œ ์ž์‹ ์ •๋ณด์—์„œ ํ˜„์žฌ ํƒ€๊ฒŸ ๋…ธ๋“œ๋ฅผ ์ง€์šด๋‹ค.
    • ํƒ€๊ฒŸ ๋…ธ๋“œ์˜ ์ž์‹๋“ค์„ dfs๋กœ ๋ชจ๋‘ ์‚ญ์ œํ•ด ์ค€๋‹ค.
    • adjList์—์„œ null์ด ์•„๋‹Œ(๋…ธ๋“œ๊ฐ€ ์กด์žฌ) ์ƒํƒœ์ด๋ฉฐ, ์ž์‹ ๋…ธ๋“œ์˜ ์ˆ˜๊ฐ€ 0์ด๋ฉด ๋ฆฌํ”„๋…ธ๋“œ์ด๋ฏ€๋กœ ์นด์šดํŠธํ•œ๋‹ค.
  • cnt๋ฅผ ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค

๐Ÿ”ธ end ๐Ÿ”ธ

  • union find ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์ธ ์ค„ ์•Œ์•˜์ง€๋งŒ, dfs๋กœ ํ’€์ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

728x90