-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
7-janghw0126 #145
7-janghw0126 #145
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΄ λ¬Έμ λ μμΉμ μΌλ‘ λ°±νΈλνΉ/μμ νμμΌλ‘ ν μ μλλ°, μΉ΄μΉ΄μ€μμ μ μν ν μ€νΈμΌμ΄μ€κ° λΉμ½ν΄μ ν΅κ³Όκ° λ©λλ€. νμλμ΄ μ¬λ €μ£Όμ μ½λλ μΌμ’ μ μμ νμμΈλ°, μλμ κ°μ ν μ€νΈ μΌμ΄μ€μ λν΄ μκ°μ΄κ³Όκ° λ°μν©λλ€.
info = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
edges = [[0, 1], [0, 2], [1, 3], [1, 4], [2, 5], [2, 6], [3, 7], [3, 8], [4, 9], [4, 10], [5, 11], [5, 12], [6, 13], [6, 14], [7, 15], [7, 16]]
Return = 17
μμΈν μ€λͺ
μ μκ³Ό λλ νμ΄ λΈλ‘κ·Έλ₯Ό νμΈν΄λ³΄μλ©΄ λ©λλ€ :)
μμ½νμλ©΄, μ ν΄λ [λΉνΈλ§μ€νΉμ μ¬μ©ν΄ νμ₯λ μν 곡κ°μμμ flood fill]μ
λλ€.
def solution(info, edges):
n = len(info)
visited = [False] * (1 << n)
graph = [[] for _ in range(n)]
for p, c in edges:
graph[p].append(c)
answer = 0
def dfs(mask: int = 1):
nonlocal answer
if visited[mask]:
return
visited[mask] = True
sheep, wolf = 0, 0
for node in range(n):
if mask & (1 << node):
sheep += (info[node] == 0)
wolf += (info[node] == 1)
if sheep <= wolf:
return
answer = max(answer, sheep)
for parent in range(n):
if not mask & (1 << parent):
continue
for child in graph[parent]:
dfs(mask | (1 << child))
dfs(1)
return answer
|
||
# κΉμ΄ μ°μ νμ(DFS)μ μννλ μ¬κ· ν¨μ μ μ | ||
def dfs(node, sheep, wolf, next_nodes): | ||
global max_count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ¦¬λ·°κ° λ¦μ΄μ μ£μ‘ν©λλ€ π₯²
μ¬μν λΆλΆμ΄μ§λ§ νμ λ μ½λ μ€μ max_countλ₯Ό μ μ λ³μλ‘ μ μΈν λΆλΆμ΄ μμ΄μ μ μ λ³μλ₯Ό μ¬μ©νμ§ μκ³ νμ΄νλ μ½λλ₯Ό μμ±ν΄λ΄€μ΅λλ€!! νμ λ μ½λλ₯Ό μΌλΆ μμ νμ΅λλΉ
def solution(info, edges):
# κ° λ
Έλμ μμ λ
Έλλ₯Ό μ μ₯ν 리μ€νΈ μ΄κΈ°ν
child = [[] for _ in range(len(info))]
# edgesμ λ°λΌ λΆλͺ¨-μμ κ΄κ³λ₯Ό μ€μ
for p, c in edges:
child[p].append(c)
# κΉμ΄ μ°μ νμ(DFS)μ μννλ μ¬κ· ν¨μ μ μ
def dfs(node, sheep, wolf, next_nodes):
# λλ μκ° μ μλ³΄λ€ λ§μμ§λ©΄ νμ μ€μ§
if wolf >= sheep:
return sheep - 1 # νμ¬ μμ μμμ 1μ λΊ κ°μ λ°ν
# νμ¬ λ
Έλμ μμ λ
Έλλ€μ νμ κ°λ₯ λ
Έλ 리μ€νΈμ μΆκ°
next_nodes.extend(child[node])
max_sheep = sheep # νμ¬κΉμ§μ μ΅λ μμ μ
# λ€μμΌλ‘ μ΄λν μ μλ λ
Έλλ€μ μν
for next_node in next_nodes:
# λ€μ λ
Έλκ° νμ¬ μμΉκ° μλ κ²½μ°μλ§ μ΄λ
new_next_nodes = [n for n in next_nodes if n != next_node]
# λ€μ λ
Έλμ λλκ° μλ κ²½μ°
if info[next_node]:
max_sheep = max(max_sheep, dfs(next_node, sheep, wolf + 1, new_next_nodes))
# λ€μ λ
Έλμ μμ΄ μλ κ²½μ°
else:
max_sheep = max(max_sheep, dfs(next_node, sheep + 1, wolf, new_next_nodes))
return max_sheep # νμλ μμ μ΅λ μλ₯Ό λ°ν
# λ£¨νΈ λ
Έλ(0λ²)μμ DFS μμ (μ²μμ μ 1λ§λ¦¬κ° μμ)
return dfs(0, 1, 0, child[0])
π λ¬Έμ λ§ν¬
νλ‘κ·Έλλ¨Έμ€ | μκ³Ό λλ
βοΈ μμλ μκ°
1μκ°
β¨ μλ μ½λ
μ΄λ² λ¬Έμ λ DFSμ μ΄μ©νμ¬ νμμ μννκ³ , μμ μλ₯Ό μ΅λλ‘ μ μ§νλ©΄μ νμμ μ§ννλ λ¬Έμ μμ΅λλ€. μ΄ λ¬Έμ μ ν΅μ¬μ νμ¬ μμΉμμ μκ³Ό λλμ μλ₯Ό μΆμ νλ©°, 쑰건μ λ°λΌ νμμ μ€λ¨νκ±°λ κ³μνλ κ²μ λλ€!
π§ λ¬Έμ μ κ·Ό λ°©λ²
1. λ¬Έμ λΆμ:
λλμ μκ° μμ μμ κ°κ±°λ λ λ§μμ§λ©΄, λ μ΄μ νμμ μ§νν μ μλ€.
λΌκ³ λͺ μλμ΄ μμ΄, λλμ μκ° μμ μλ₯Ό μ΄κ³Όνλ κ²½λ‘λ νμνμ§ μλ κ²μ΄ μ€μνλ€κ³ μκ°νμ΅λλ€. κ·Έλμ DFSλ₯Ό μ¬μ©ν΄ λͺ¨λ κ²½λ‘λ₯Ό νμνλ, λλμ μκ° μμ μλ³΄λ€ λ§μμ§λ μκ° νμμ μ€λ¨νλ μμ΄λμ΄λ₯Ό μ΄μ©νμ΅λλ€.λ£¨νΈ λ ΈλλΆν° μΆλ°νμ¬ κ°λ₯ν λͺ¨λ κ²½λ‘λ₯Ό νμνλ©΄μ μμ μλ₯Ό μ΅λλ‘ λ§λ€ μ μλ λ°©λ²μ μ°ΎμμΌ νλ€.
λΌλ μ§λ¬Έμ ν΅ν΄, λͺ¨λ κ²½λ‘λ₯Ό νμν΄μΌ νλ λ¬Έμ λΌλ μ μμ DFSκ° μ ν©νλ€κ³ νλ¨νμ΅λλ€.2. DFS νμ:
β λ¬Έμ λ‘μ§
π₯ νΈλ¬λΈ μν
λλ μμ μ μ λΉκ΅ λΆλΆμμ μ΄μκ° μμμ΅λλ€. μ²μμλ
wolf > sheep
쑰건μμ νμμ μ€λ¨νμΌλ, ν μ€νΈ κ³Όμ μμ λλμ μκ° μκ³Ό κ°μ κ²½μ°μλ νμμ΄ μλͺ»λλ κ²½μ°κ° λ°μνμ΅λλ€. κ·Έλμ λ¬Έμ μ 쑰건μ λ§μΆμ΄wolf >= sheep
μΌλ‘ 쑰건μ μμ ν¨μΌλ‘μ¨, μ νν κ²°κ³Όλ₯Ό μ»μ μ μμμ΅λλ€.π©βπ» μ΅μ’ μ½λ
π μλ‘κ² μκ²λ λ΄μ©
μ΄λ² PRμμλ κ²½λ‘λ₯Ό νμνλ λμ€μ 쑰건μ μΆκ°νμ¬ λΆνμν κ²½λ‘λ₯Ό 미리 μ°¨λ¨ν¨μΌλ‘μ¨ ν¨μ¨μ±μ λμ΄λ κ²μ΄ μ€μνλ€λ κ²μ κΉ¨λ¬μμ΅λλ€.