๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ปAlgorithm/PS

[BOJ/๋ฐฑ์ค€] 19538 ๋ฃจ๋จธ - ํŒŒ์ด์ฌ

๋ฌธ์ œ

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

 

19538๋ฒˆ: ๋ฃจ๋จธ

์˜ˆ์ œ 1 0๋ถ„ : ์ตœ์ดˆ ์œ ํฌ์ž($1$, $6$๋ฒˆ ์‚ฌ๋žŒ)๊ฐ€ ๋ฃจ๋จธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. 1๋ถ„ : $1$๋ฒˆ ์‚ฌ๋žŒ์€ $2$, $3$๋ฒˆ ์‚ฌ๋žŒ์—๊ฒŒ ๋ฃจ๋จธ๋ฅผ ํผ๋œจ๋ฆฐ๋‹ค. $2$๋ฒˆ ์‚ฌ๋žŒ์€ ์ฃผ๋ณ€์ธ $2$๋ช… ์ค‘ $1$๋ช…์ด ๋ฃจ๋จธ๋ฅผ ๋ฏฟ๊ณ  ์žˆ์–ด ๋ฃจ๋จธ๋ฅผ ๋ฏฟ๊ฒŒ ๋œ๋‹ค. $3$

www.acmicpc.net

 

 

 

 

๋ฌธ์ œํ’€์ด

2๊ฐœ์˜ ๋ฑ์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค.

 

์ฒ˜์Œ ์œ ํฌ์ž๋ฅผ ์‹œ์ž‘์œผ๋กœ ์ƒˆ๋กœ ๋ฃจ๋จธ๋ฅผ ๋ฏฟ๋Š” ์‚ฌ๋žŒ๋“ค์„ start ๋ฑ์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฑ์ด ๋นŒ ๋•Œ๊นŒ์ง€ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ–ˆ๋‹ค.

 

์ƒˆ๋กœ ๋ฃจ๋จธ๋ฅผ ๋ฏฟ๋Š” ์‚ฌ๋žŒ๋“ค์˜ time ๊ฐ’์„ ์—…๋ฐ์ดํŠธ ํ•ด ์ค„ ๋•Œ์—๋Š” ์ฆ‰์‹œ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ,

๋”ฐ๋กœ que๋ผ๋Š” ๋ฑ์— ์ถ”๊ฐ€ํ•ด์„œ ํ•ด๋‹น ํƒ์ƒ‰์ด ๋๋‚œ ์ดํ›„์— ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

์ฆ‰์‹œ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹ค์Œ ์›์†Œ๋“ค์˜ ํƒ์ƒ‰์— ์˜ํ–ฅ์„ ์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

์ฝ”๋“œ

from collections import deque
n=int(input())
people=[]
for i in range(n):
    t=list(map(int,input().split()))
    t.pop()
    people.append(t)
people.insert(0,[])
m=int(input())
start=list(map(int,input().split()))

time=[-1]*(n+1)
for i in start:
    time[i]=0
##์ตœ์ดˆ์œ ํฌ์ž 0์œผ๋กœ ์ดˆ๊ธฐํ™”
#1๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘ ์œ ์˜**

start=deque(start)
que=deque([])
while(start):
    index=start.popleft()
    temp=people[index]
    move=[]
    for i in temp:
        count=0
        if time[i]!=-1:
            continue ##์ด์ „์— ๋ฐ”๋€œ
        leng=len(people[i])
        for j in range(leng):
            if time[people[i][j]]!=-1:
                count+=1
        if leng>count*2:
            continue
        que.append(i)
    if not start:
        while(que):
            tmp=que.popleft()
            time[tmp]=time[index]+1
            start.append(tmp)

time.remove(time[0])
print(*time)

 

๊ธฐํƒ€

์ฝ”๋“œ์— ํŠน๋ณ„ํ•œ ์ด๋ก ์ด ํ•„์š”ํ•œ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ์ธ๋ฑ์Šค ์ฒ˜๋ฆฌ ๊ณผ์ •์ด ํ—ท๊ฐˆ๋ ค์„œ ์ฝ”๋“œ ๊ตฌํ˜„์ด ์ƒ๊ฐ๋ณด๋‹ค ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

๋˜, ์ฒ˜์Œ์—๋Š” time๊ฐ’์„ ๋ฐ”๋กœ๋ฐ”๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ์–ด์„œ ๊ทธ ์ดํ›„ ๊ฐ’๋“ค์— ๋Œ€ํ•œ ์ฐจ์งˆ์ด ์ƒ๊ธฐ๊ธฐ๋„ ํ–ˆ๊ณ 

์ž…๋ ฅ ๊ฐ’๋“ค์„ zero base๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ์ด์ƒํ•œ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์™€์„œ ๊ณ„์‚ฐํ•˜๊ธฐ๋„ ํ–ˆ๋‹ค๐Ÿ˜…๐Ÿ˜