Search

92343. ์–‘๊ณผ ๋Š‘๋Œ€

ํ”Œ๋žซํผ
BOJ
๋ถ„๋ฅ˜
์•Œ๊ณ ๋ฆฌ์ฆ˜
์ค‘์š”๋„
ํ‹ฐ์–ด
๋งํฌ
๋‹ค์‹œ ํ’€์–ด๋„ ํ‹€๋ฆด ๊ฒƒ ๊ฐ™์€ ๋ฌธ์ œ
Created At
2023/04/15 14:36
Updated At
2023/04/15 14:42
Writer
์ƒํƒœ

Problem

Code

ย #1

Idea

ํ’€์ง€ ๋ชปํ•ด์„œ ๋ธ”๋กœ๊ทธ ํ’€์ด ์ฐธ๊ณ ํ–ˆ์Œ
๋…ธ๋“œ๋ฅผ ์™”๋‹ค๊ฐ”๋‹ค ํ•ด์•ผํ•˜๋ฏ€๋กœ, ๋ถ€๋ชจ๋…ธ๋“œ์—์„œ ์ž์‹๋…ธ๋“œ๋กœ ํŒŒ๊ณ  ๋“ค์–ด๊ฐ€๋Š” ์ผ๋ฐ˜์ ์ธ dfs, bfs ๋กœ๋Š” ํ•ด๊ฒฐ์ด ์•ˆ ๋œ๋‹ค.
edges ์—์„œ [๋ถ€๋ชจ ๋…ธ๋“œ, ์ž์‹ ๋…ธ๋“œ] ๋ฅผ ๋ฐ›์•„์™€์„œ
๋ถ€๋ชจ๋…ธ๋“œ๋Š” ๋ฐฉ๋ฌธํ–ˆ๊ณ , ์ž์‹๋…ธ๋“œ(๋ณธ์ธ)๋Š” ๋ฐฉ๋ฌธ ์•ˆ ํ•œ ๋…ธ๋“œ๋งŒ dfs ๋ฅผ ํ•ด์•ผํ•œ๋‹ค.
๋งค ๋ฐฉ๋ฌธ๋งˆ๋‹ค answer ๋ฆฌ์ŠจํŠธ์— ํ˜„์žฌ์˜ sheep ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ , ๋ชจ๋“  dfs ๊ฐ€ ๋๋‚  ์‹œ max(answer) ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

ย Code

def solution(info, edges): visited = [0] * len(info) answer = [] def dfs(sheep, wolf): if sheep > wolf: answer.append(sheep) else: return for parent, child in edges: # ๋ถ€๋ชจ๋Š” ๋ฐฉ๋ฌธํ–ˆ๊ณ , ์ž์‹๋…ธ๋“œ(๋ณธ์ธ)๋Š” ๋ฐฉ๋ฌธ ์•ˆ ํ•œ ๋…ธ๋“œ๋งŒ dfs if visited[parent] and not visited[child]: visited[child] = 1 if info[child] == 0: dfs(sheep+1, wolf) else: dfs(sheep, wolf+1) visited[child] = 0 visited[0] = 1 dfs(1, 0) return max(answer)
Python
๋ณต์‚ฌ

Commentary

ํ’€์ด ์ฐธ๊ณ 

#2

Idea

ย Code

Python
๋ณต์‚ฌ

Solution

Commentary