๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿฆ„ | ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต/BOJ

[Python] ๋ฐฑ์ค€ 1068 ํŠธ๋ฆฌ

by KASSID 2023. 4. 12.

๋ชฉ์ฐจ

    728x90

    ๋ฌธ์ œ

    ํŠธ๋ฆฌ์—์„œ ๋ฆฌํ”„ ๋…ธ๋“œ๋ž€, ์ž์‹์˜ ๊ฐœ์ˆ˜๊ฐ€ 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

     


    ์ฝ”๋“œ

    import sys
    sys.setrecursionlimit(10**6)
    
    def removeTreeDfs(A, removeIndex):
        A[removeIndex] = None
        for i in range(len(A)):
            if removeIndex == A[i]:
                removeTreeDfs(A, i)
    
    
    
    N = int(sys.stdin.readline().rstrip()) # ๋…ธ๋“œ๊ฐœ์ˆ˜
    nodeList = list(map(int,sys.stdin.readline().rstrip().split())) # ๋…ธ๋“œ ๋ฆฌ์ŠคํŠธ
    trueList = [True for i in range(N)]
    removeIndex = int(sys.stdin.readline().rstrip()) # ์‚ญ์ œ ์ธ๋ฑ์Šค
    
    # remove subTree
    removeTreeDfs(nodeList, removeIndex)
    
    # find leaf node
    leafCnt = 0
    for i in range(N):
        if nodeList[i] != None and i not in nodeList:
            leafCnt += 1
    
    print(leafCnt)

    ๋ฆฌ๋ทฐ

    ํ•˜๋‚˜์˜ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•œ ํ›„์˜ ๋ฆฌํ”„ ๋…ธ๋“œ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

    ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ์‚ญ์ œ ์—ฐ์‚ฐ์„ DFS๋ฅผ ์ด์šฉํ•ด ์ง„ํ–‰ํ•˜์˜€๋‹ค.

     

    ์ฒซ ์‹œ๋„์—์„œ ์‹ค์ˆ˜ํ•œ ์ ์€ ํŠธ๋ฆฌ๊ฐ€ ๋ฌด์กฐ๊ฑด ์ด์ง„ํŠธ๋ฆฌ๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ํ–ˆ๋‹ค๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

    ๋”ฐ๋ผ์„œ ์ฒซ childIdx๋ฅผ ์ฐพ๊ณ  ๊ทธ๊ฒƒ์˜ +1๊นŒ์ง€๋งŒ ๊ณ ๋ คํ•˜์˜€๋Š”๋ฐ

    ์˜ˆ์‹œ์—์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์—ˆ์ง€๋งŒ ์‹ค์ œ ์ œ์ถœํ•œ ์ฝ”๋“œ๋Š” ํ‹€๋ฆฌ๊ฒŒ ๋˜์—ˆ๋‹ค.

     

    ๋”ฐ๋ผ์„œ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ๋‹ค์‹œ ๋ฐ”๊พธ์–ด์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ 

    ์ •๋‹ต์„ ๋ฐ›์•˜๋‹ค.

    def removeTreeDfs(A, removeIndex):
        A[removeIndex] = None
        # ์ž์‹ ๋…ธ๋“œ ์ฐพ๊ธฐ
        for i in range(len(A)):
            if removeIndex == A[i]:
                removeTreeDfs(A, i)

     

     

    ๋Œ“๊ธ€