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

Partially Committed

[๋ฐฑ์ค€ 1068] ํŠธ๋ฆฌ (JAVA) ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 1068] ํŠธ๋ฆฌ (JAVA)

WonderJay 2023. 1. 22. 12:19
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

1068๋ฒˆ: ํŠธ๋ฆฌ

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

www.acmicpc.net

๋”๋ณด๊ธฐ

๋ฌธ์ œ

ํŠธ๋ฆฌ์—์„œ ๋ฆฌํ”„ ๋…ธ๋“œ๋ž€, ์ž์‹์˜ ๊ฐœ์ˆ˜๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ๋งํ•œ๋‹ค.

ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋…ธ๋“œ ํ•˜๋‚˜๋ฅผ ์ง€์šธ ๊ฒƒ์ด๋‹ค. ๊ทธ ๋•Œ, ๋‚จ์€ ํŠธ๋ฆฌ์—์„œ ๋ฆฌํ”„ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋…ธ๋“œ๋ฅผ ์ง€์šฐ๋ฉด ๊ทธ ๋…ธ๋“œ์™€ ๋…ธ๋“œ์˜ ๋ชจ๋“  ์ž์†์ด ํŠธ๋ฆฌ์—์„œ ์ œ๊ฑฐ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠธ๋ฆฌ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž.

ํ˜„์žฌ ๋ฆฌํ”„ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ์ด๋‹ค. (์ดˆ๋ก์ƒ‰ ์ƒ‰์น ๋œ ๋…ธ๋“œ) ์ด๋•Œ, 1๋ฒˆ์„ ์ง€์šฐ๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ•œ๋‹ค. ๊ฒ€์ •์ƒ‰์œผ๋กœ ์ƒ‰์น ๋œ ๋…ธ๋“œ๊ฐ€ ํŠธ๋ฆฌ์—์„œ ์ œ๊ฑฐ๋œ ๋…ธ๋“œ์ด๋‹ค.

์ด์ œ ๋ฆฌํ”„ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” 1๊ฐœ์ด๋‹ค.

์ž…๋ ฅ

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

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ํŠธ๋ฆฌ์—์„œ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ๋…ธ๋“œ๋ฅผ ์ง€์› ์„ ๋•Œ, ๋ฆฌํ”„ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1 

5
-1 0 0 1 1
2

์˜ˆ์ œ ์ถœ๋ ฅ 1 

2

์˜ˆ์ œ ์ž…๋ ฅ 2 

5
-1 0 0 1 1
1

์˜ˆ์ œ ์ถœ๋ ฅ 2 

1

์˜ˆ์ œ ์ž…๋ ฅ 3 

5
-1 0 0 1 1
0

์˜ˆ์ œ ์ถœ๋ ฅ 3 

0

์˜ˆ์ œ ์ž…๋ ฅ 4 

9
-1 0 0 2 2 4 4 6 6
4

์˜ˆ์ œ ์ถœ๋ ฅ 4 

2

ํŠธ๋ฆฌ์—์„œ ํŠน์ • ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜์˜€์„ ๋•Œ, ๋ฆฌํ”„ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

๋งŒ์•ฝ์— ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค. ( ์˜ˆ์™ธ ์ฒ˜๋ฆฌ )

 

๋ฃจํŠธ ๋…ธ๋“œ ์ด์™ธ์˜ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š”,

 

dfs(๋ฃจํŠธ ๋…ธ๋“œ) ๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ

 

์‚ญ์ œํ•œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฆฌํ”„ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŒ…ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

๋ฆฌํ”„๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŒ…ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ„๋‹จํ•˜๋‹ค.

 

int nodes = 0 ; ์ด๋ผ๊ณ  ์ž์‹ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŒ…ํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์ž.

 

dfs ( v ) ๋ผ๊ณ  ํ–ˆ์„ ๋•Œ,

 

v ์— ์—ฐ๊ฒฐ๋œ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ฅผ ์ฐพ๊ณ  nodes ๋ฅผ ํ•˜๋‚˜ ์ฆ๊ฐ€ํ•ด์ค€ ๋‹ค์Œ์—, ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๋ฐฉ๋ฌธํ•˜๋„๋ก ํ•œ๋‹ค.

 

dfs ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ, nodes ์— ์ €์žฅ๋œ ๊ฐ’์„ ํ™•์ธํ•œ๋‹ค.

 

์ด๋•Œ nodes ์— ์ €์žฅ๋œ ๊ฐ’์ด 0 ์ด๋ฉด ํ•ด๋‹น dfs ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ์‹คํ–‰์‹œํ‚จ(?) ๋…ธ๋“œ๋Š” ์ž์‹ ๋…ธ๋“œ๊ฐ€ ๋‹จ ํ•˜๋‚˜๋„ ์—†๋Š” ๋ฆฌํ”„ ๋…ธ๋“œ๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ nodes ๊ฐ€ 0 ์ธ ๊ฒฝ์šฐ์—๋งŒ ans ๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค.

 

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {
    static ArrayList<Integer>[] graph;
    static boolean visited[];
    static int delete;
    static int parent[];
    static int ans;

    public static void main(String[] args) throws IOException {
        //System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer stk;
        int n = Integer.parseInt(br.readLine()); // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜
        graph = new ArrayList[n + 1];
        visited = new boolean[n + 1];
        parent = new int[n + 1];
        for (int i = 0; i < n; i++)
            graph[i] = new ArrayList<>();
        int root = -1;
        stk = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            int p = Integer.parseInt(stk.nextToken());
            if (p == -1) {
                // i ๋…ธ๋“œ๊ฐ€ ๋ฐ”๋กœ ๋ฃจํŠธ ๋…ธ๋“œ
                root = i; // ๋ฃจํŠธ ๋…ธ๋“œ ์ •๋ณด๋ฅผ ์ €์žฅํ•จ
            } else {
                // p ๊ฐ€ i ๋…ธ๋“œ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ์ž„
                graph[i].add(p);
                graph[p].add(i); // ์„œ๋กœ ์—ฐ๊ฒฐ
            }
        }
        delete = Integer.parseInt(br.readLine());
        if (delete == root) {
            System.out.println(0);
            return;
        } else dfs(root);
        System.out.println(ans);
    }

    static void dfs(int v) {
        visited[v] = true;
        int nodes = 0;
        for (int cur : graph[v]) {
            // ์—ฐ๊ฒฐ ๋…ธ๋“œ ํƒ์ƒ‰
            if (cur != delete && !visited[cur]) {
                nodes++;
                dfs(cur);
            }
        }
        if (nodes == 0) {
            ans++; // ์ž์‹ ๋…ธ๋“œ๊ฐ€ ํ•˜๋‚˜๋„ ์—†์œผ๋ฉด ๋ฆฌํ”„ ๋…ธ๋“œ์ž„
        }
    }
}

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