๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 18352_ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ

๋ฟŒ์•ผ._. 2022. 2. 4. 22:02

Silver II

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/18352)

<ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ>

๋ฌธ์ œ 

 

์–ด๋–ค ๋‚˜๋ผ์—๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€์˜ ๋„์‹œ์™€ M๊ฐœ์˜ ๋‹จ๋ฐฉํ–ฅ ๋„๋กœ๊ฐ€ ์กด์žฌํ•œ๋‹ค. ๋ชจ๋“  ๋„๋กœ์˜ ๊ฑฐ๋ฆฌ๋Š” 1์ด๋‹ค.
์ด๋•Œ ํŠน์ •ํ•œ ๋„์‹œ X๋กœ๋ถ€ํ„ฐ ์ถœ๋ฐœํ•˜์—ฌ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋„์‹œ ์ค‘์—์„œ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ์ •ํ™•ํžˆ K์ธ ๋ชจ๋“  ๋„์‹œ๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋˜ํ•œ ์ถœ๋ฐœ ๋„์‹œ X์—์„œ ์ถœ๋ฐœ ๋„์‹œ X๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋Š” ํ•ญ์ƒ 0์ด๋ผ๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด N=4, K=2, X=1์ผ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ทธ๋ž˜ํ”„๊ฐ€ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.

์ด๋•Œ 1๋ฒˆ ๋„์‹œ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋„์‹œ ์ค‘์—์„œ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ 2์ธ ๋„์‹œ๋Š” 4๋ฒˆ ๋„์‹œ๋ฟ์ด๋‹ค.  2๋ฒˆ๊ณผ 3๋ฒˆ ๋„์‹œ์˜ ๊ฒฝ์šฐ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ 1์ด๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

 

์ž…๋ ฅ

 

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ N, ๋„๋กœ์˜ ๊ฐœ์ˆ˜ M, ๊ฑฐ๋ฆฌ ์ •๋ณด K, ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ X๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 300,000, 1 ≤ M ≤ 1,000,000, 1 ≤ K ≤ 300,000, 1 ≤ X ≤ N) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๋‘ ๊ฐœ์˜ ์ž์—ฐ์ˆ˜ A, B๊ฐ€ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” A๋ฒˆ ๋„์‹œ์—์„œ B๋ฒˆ ๋„์‹œ๋กœ ์ด๋™ํ•˜๋Š” ๋‹จ๋ฐฉํ–ฅ ๋„๋กœ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ๋‹ค. (1 ≤ A, B ≤ N) ๋‹จ, A์™€ B๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์ž์—ฐ์ˆ˜์ด๋‹ค.

 

์ถœ๋ ฅ 

 

X๋กœ๋ถ€ํ„ฐ ์ถœ๋ฐœํ•˜์—ฌ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋„์‹œ ์ค‘์—์„œ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ K์ธ ๋ชจ๋“  ๋„์‹œ์˜ ๋ฒˆํ˜ธ๋ฅผ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ์ด๋•Œ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋„์‹œ ์ค‘์—์„œ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ K์ธ ๋„์‹œ๊ฐ€ ํ•˜๋‚˜๋„ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด

- ์ดˆ๊ธฐ ์ฝ”๋“œ: ์‹œ๊ฐ„ ์ดˆ๊ณผ

import sys

def bfs(graph, start, depth):
    visited=[start]
    queue=[start]
    while queue:
        temp=queue.pop(0)
        for i in graph[temp]:
            if i not in visited:
                visited.append(i)
                depth[i]=depth[temp]+1
                queue.append(i)

if __name__=='__main__':
    #๋„์‹œ์˜ ๊ฐœ์ˆ˜, ๋„๋กœ์˜ ๊ฐœ์ˆ˜, ๊ฑฐ๋ฆฌ ์ •๋ณด, ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ
    N,M,K,X=map(int,sys.stdin.readline().split())

    graph={}
    depth={}
    for i in range(N):
        graph[i+1]=[]
        depth[i+1]=0

    for i in range(M):
        A,B=map(int,sys.stdin.readline().split())
        graph[A].append(B)

    bfs(graph,X,depth)

    result=[]
    for key,value in depth.items():
        if value==K:
            result.append(key)

    if len(result)==0:
        print(-1)
    else:
        result.sort()
        for i in result:
            print(i)

 

์ฒซ ๋„์ „... ๋˜ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ๋”๋Š” ์ƒ๊ฐ๋‚˜์ง€ ์•Š์•„ ์ž ์‹œ ๋‚ด๋ฒ„๋ ค ๋‘๊ณ  ๋‹ค๋ฅธ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์—ˆ๋‹ค. ๊ทธ ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์ด ๋– ์˜ฌ๋ผ ๋‹ค์‹œ ๋„์ „ -!

 

โ“ ์™œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”๊ฐ€?

depth๋กœ ์ถฉ๋ถ„ํžˆ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ์—๋„ visited ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์˜€๋‹ค. ์—ฌ๊ธฐ์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

 

์œ„์˜ ๋ฌธ์ œ์ ์„ ์ฝ”์นœ ์ฝ”๋“œ์ด๋‹ค.

 

- ๋‘ ๋ฒˆ์งธ ์ฝ”๋“œ: ์‹œ๊ฐ„ ์ดˆ๊ณผ

import sys

def bfs(graph, start, depth):
    queue=[start]
    while queue:
        temp=queue.pop(0)
        for i in graph[temp]:
            if depth[i]==0 and i!=start:
                depth[i]=depth[temp]+1
                queue.append(i)

if __name__=='__main__':
    #๋„์‹œ์˜ ๊ฐœ์ˆ˜, ๋„๋กœ์˜ ๊ฐœ์ˆ˜, ๊ฑฐ๋ฆฌ ์ •๋ณด, ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ
    N,M,K,X=map(int,sys.stdin.readline().split())

    graph={}
    depth={}
    for i in range(N):
        graph[i+1]=[]
        depth[i+1]=0

    for i in range(M):
        A,B=map(int,sys.stdin.readline().split())
        graph[A].append(B)

    bfs(graph,X,depth)

    result=[]
    for key,value in depth.items():
        if value==K:
            result.append(key)

    if len(result)==0:
        print(-1)
    else:
        result.sort()
        for i in result:
            print(i)

 

๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ ๋ฌธ์ œ์ ์„ ๊ณ ์ณค์Œ์—๋„ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

 

โ“ ์™œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”๊ฐ€?

์ด๋ฒˆ์—๋Š” ์ด์œ ๋ฅผ ์ฐพ์ง€ ๋ชปํ•ด ๊ฒ€์ƒ‰์˜ ํž˜์„ ๋นŒ๋ ธ๋‹ค. ์ฐพ์•„๋ณธ ๊ฒฐ๊ณผ list๋ฅผ deque๋กœ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•˜๋‹ค.

 

 

- my solution

import sys
from collections import deque

def bfs(graph, start, depth):
    queue=deque([start])
    while queue:
        temp=queue.popleft()
        for i in graph[temp]:
            if depth[i]==0 and i!=start: #์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด
                depth[i]=depth[temp]+1
                queue.append(i)

if __name__=='__main__':
    #๋„์‹œ์˜ ๊ฐœ์ˆ˜, ๋„๋กœ์˜ ๊ฐœ์ˆ˜, ๊ฑฐ๋ฆฌ ์ •๋ณด, ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ
    N,M,K,X=map(int,sys.stdin.readline().split())

    graph={}
    depth={}
    for i in range(N):
        graph[i+1]=[]
        depth[i+1]=0

    for i in range(M):
        A,B=map(int,sys.stdin.readline().split())
        graph[A].append(B)

    bfs(graph,X,depth)

    result=[]
    for i in range(N):
        if depth[i+1]==K: # ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ K
            result.append(i+1)

    if len(result)==0: # ํ•˜๋‚˜๋„ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด
        print(-1)
    else: # ๋„์‹œ์˜ ๋ฒˆํ˜ธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅ
        result.sort()
        for i in result:
            print(i)

 

  • bfs
    - queue: deque๋กœ ์„ ์–ธ
    - queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต: ์ ค ์ฒ˜์Œ ๊ฐ’ pop ํ•ด์„œ ๊ทธ์™€ ์—ฐ๊ฒฐ๋œ ์ด์›ƒ ๋„์‹œ์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ฑฐ๋ฆฌ(ํ˜„์žฌ ๋„์‹œ depth์˜ +1) ์ €์žฅ ๋ฐ queue์— ์ถ”๊ฐ€
  • main
    - ์ž…๋ ฅ
    - graph: dict, ๋„์‹œ ์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„
    - depth: dict, ๊ฑฐ๋ฆฌ ์ •๋ณด ์ €์žฅ
    - ๋ฐฉํ–ฅ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„
    - bfs ํ˜ธ์ถœ
    - depth ์ˆœํšŒ๋ฅผ ํ†ตํ•ด ์ตœ๋‹จ ๊ฑฐ๋ฆฌ K์™€ ๊ฐ™์€ ๋„์‹œ๊ฐ€ ์žˆ๋‹ค๋ฉด result์— ์ถ”๊ฐ€ -> ํ•˜๋‚˜๋„ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด -1 ์ถœ๋ ฅ/ ์กด์žฌํ•˜๋ฉด ๋„์‹œ์˜ ๋ฒˆํ˜ธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅ

์ƒ๊ฐ๐Ÿค”

 

์‹œ๊ฐ„ ์ดˆ๊ณผ์—์„œ ๋ฒ—์–ด๋‚˜์ง€ ๋ชปํ•  ์ค„ ์•Œ์•˜์ง€๋งŒ... deque๋ฅผ ํ†ตํ•ด ํƒˆ์ถœ ์™„๋ฃŒ -!

๊ธฐ์–ตํ•˜์ž deque -!