-
Notifications
You must be signed in to change notification settings - Fork 2
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
59-tgyuuAn #207
59-tgyuuAn #207
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.
μ΄λ² 리뷰λ λ¦¬λ·°κ° μλλΌ κ°μνμ λ κ°κΉμΈ κ² κ°λ€μ π―ππ
PRμ μ½λλ° μκ³ λ¦¬μ¦ μ§¬λ°₯μ΄ νλ¬λμ€λκ΅°μ .... γ·γ·
νμ μκ·Ή λ°κ³ κ°λλ€ π«
μκ³ λ¦¬μ¦ λΆλ₯κ° ν¬μ λ°°μ΄μ΄λΌκΈΈλ νλ² μ°Ύμ보μμ΅λλ€
ν¬μ λ°°μ΄μ μ΄μ©ν νμ΄
ν¬μλ°°μ΄μ νμ€λ‘ μ€λͺ
νλ©΄
'2^n λ² μ¬λΌκ°μ λμ λ
Έλ μ°ΎκΈ°' μ¦, μ΄λ€ λ
Έλμ 2^nλ²μ§Έ λΆλͺ¨μ°ΎκΈ°μΈ κ² κ°μμ.
μ΅μ곡ν΅μ‘°μ & ν¬μ λ°°μ΄
2^nλ²μ§Έ λΆλͺ¨μ λν μ 보λ₯Ό dp μ μ μ₯ν΄λκ³ μ°λ κ² κ°λ€μ!
if now_cost >= cost[now_node] and now_node != 1: continue | ||
cost[now_node] = now_cost | ||
|
||
for next_node, next_cost in graph_info[now_node].items(): |
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.
λμ λ리 μλ£κ΅¬μ‘°λ₯Ό λ§μ΄ μμ¨μ μμνλ°
items()κ° λμ λ리 key, valueλ₯Ό λ°νν΄μ£Όλ ν¨μλ€μ μ€μ€
λ°λ‘ μ μνμ€ μκ³ μ°Ύκ³ μμμ΅λλ€.. γ γ
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.
π«‘π«‘π«‘ππ»ππ»ππ»ππ»
ν¬μ λ°°μ΄ μ§±μ κΈ°νλ€,,,,,,,,,,,,,,,,,,,,μ......... |
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.
ν¬μ λ°°μ΄ μ°λ λ°©λ²μΌλ‘ ν΄λ³Όλ €λ€κ°, λμ ν 머리μ μλ€μ΄μμ PR λ³΄κ³ λκ°μ΄ λ€μ΅μ€νΈλΌ + μ΄μ§ νμμΌλ‘ μΈλΌμ΄...
from heapq import *
from collections import deque
def binary_search(paths: deque, energy: int) -> int:
lo, hi = -1, len(paths)
while lo + 1 < hi:
mid = (lo + hi) // 2
if paths[0][1] - paths[mid][1] <= energy:
lo = mid
else:
hi = mid
return 0 if lo == -1 else lo
def dijkstra(heap: list, distances: list, parents: list, tree: list):
heappush(heap, (0, 1))
distances[1] = 0
while heap:
du, u = heappop(heap)
if distances[u] < du:
continue
for v, dv in tree[u]:
if du + dv < distances[v]:
distances[v] = du + dv
parents[v] = u
heappush(heap, (distances[v], v))
def main():
input = open(0).readline
n = int(input())
energy_of_ants = [0] + [int(input()) for _ in range(n)]
tree = [[] for _ in range(n + 1)]
for _ in range(n - 1):
a, b, c = map(int, input().split())
tree[a].append((b, c))
tree[b].append((a, c))
heap = []
distances = [10**9] * (n + 1)
parents = [0] * (n + 1)
dijkstra(heap, distances, parents, tree)
answer = [0] * (n + 1)
for i in range(2, n + 1):
if len(tree[i]) > 1:
continue
node = i
paths = deque()
while node != 0:
paths.append((node, distances[node]))
node = parents[node]
while paths:
u, du = paths[0]
if answer[u] != 0:
paths.popleft()
continue
answer[u] = paths[binary_search(paths, energy_of_ants[u])][0]
paths.popleft()
print('\n'.join(map(str, answer[1:])))
if __name__ == "__main__":
main()
graph_info = defaultdict(lambda : defaultdict(int)) | ||
neighbor_count = [0 for _ in range(N+1)] | ||
|
||
for _ in range(N-1): | ||
start, destination, cost = map(int, input().split()) | ||
graph_info[start][destination] = cost | ||
graph_info[destination][start] = cost | ||
|
||
neighbor_count[start] += 1 | ||
neighbor_count[destination] += 1 |
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.
neighbour μλ₯Ό κ΅³μ΄ κΈ°λ‘ν νμ μμ΄, graph_infoμ ν¬κΈ°κ° 곧 μ΄μ λ Έλ μλ₯Ό μλ―Έν©λλ€.
μ κ²½μ°μ listλ₯Ό μ¨μ κ·Έλν(νΈλ¦¬) μ 보λ₯Ό κΈ°λ‘ν©λλ€.
tree = [[] for _ in range(n + 1)]
for _ in range(n - 1):
a, b, c = map(int, input().split())
tree[a].append((b, c))
tree[b].append((a, c))
μ΄λ° μμΌλ‘ νλ©΄, λ°μμ 리ν λ Έλλ₯Ό νλ³ν λ
for i in range(2, n + 1):
if len(tree[i]) > 1:
continue
...
μ΄λ° μμΌλ‘ iλ²μ§Έ λ Έλμ μΈμ 리μ€νΈ κΈΈμ΄κ° 1μΈ κ²½μ°μλ§ νμΈνλ μμΌλ‘ κ²μ¬ν μ μμ΅λλ€.
leaf_node = deque() | ||
for node, value in enumerate(neighbor_count): | ||
if node in (0, 1): continue | ||
if value == 1: leaf_node.append(node) |
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.
μμμ μΈκΈνλ―μ΄ λ¦¬ν λ Έλλ₯Ό νλ³νλ©΄, μ΄ κ³Όμ μ νμμμ΄μ§λλ€. μλμ κ°μ΄ λ°λ‘ κ²μ¬νλ©΄ λ©λλ€ :)
answer = [0] * (n + 1)
for i in range(2, n + 1):
if len(tree[i]) > 1:
continue
node = i
paths = deque()
while node != 0:
paths.append((node, distances[node]))
node = parents[node]
while paths:
...
π λ¬Έμ λ§ν¬
κ°λ―Έ
βοΈ μμλ μκ°
2μκ°
μ§μ§... λ¬Έμ λΆλ₯ μλ³΄κ³ ... λ νΌλ°μ€λ μλ³΄κ³ ...
μμΈ -> λΆμ°κ°λ itxμμ 5μκ°λμ ν κ²λ μκ³ ν΄μ... ν«μ€ν ν€κ³ λ ΈνΈλΆ νΌμ³μ νμλλ°
λμ€μ½λμμ κ³ κ΅°λΆν¬ νλ©΄μ μνμ νμμ΄μ... π«‘π«‘π«‘
μ§μ§... μμ μ§λ¦Ών΄..
β¨ μλ μ½λ
κ·Όλ° λ¬Έμ νκ³ λμ μκ³ λ¦¬μ¦ λΆλ₯λ₯Ό 보λκΉ ν¬μ λ°°μ΄μ΄λΌκ³ νλ€μ.
μΌλ¨ μ λ κ·Έλ κ² μ νμꡬ λ€μ΅μ€νΈλΌ + μ΄λΆ νμ μΌλ‘ νμμλλ€!
μΌλ¨ λ¬Έμ λ₯Ό μμΈν 보면,
λΌλ μ§λ¬Έμμ κ°λ―Έκ΅΄μ μλμ κ°μ ννλ‘ λ§λ€μ΄μ Έμμμ μ μ μμ΅λλ€.
λν,
μμ$O(n^2)$ μ μκ°λ³΅μ‘λμΈ μκ³ λ¦¬μ¦μ μ¬μ©νλ©΄ 1μ΅μ΄ νμ© λμ΄λ²λ¦¬λ,
μ΅λ$O(n * log(n))$ μ μκ°λ³΅μ‘λλ‘ νμ΄μΌ ν¨μ μ μ μμ΅λλ€.
μ²μμλ, AlgoLeadMe/AlgoLeadMe-6#2 λ¬Έμ μ²λΌ,
1λ² κ°λ―Έκ΅΄μμ λ€μ΅μ€νΈλΌ ν λ² λ리면$O(n * log(n))$ μΌλ‘ ν μ μμ΄μ κ°κΏ μλ ...? νλλ°,
μ€μ°μ΄μμλλ€.
μ΄μ λ λͺ¨λ κ°λ―Έκ° 체λ ₯μ΄ λ¬΄νμ΄μ΄μ λͺ¨λ 1λ² λ°©κΉμ§ κ° μ μμΌλ©΄ OK μκ² μ§λ§,
κ°λ―Έμ 체λ ₯μ΄ νμ μ μ΄λΌ 1λ² κΉμ§ λλ¬νμ§ λͺ»ν μ μκ³ ,
μ΄λ΄ κ²½μ° 1λ²κ³Ό κ°μ₯ κ°κΉμ΄ λ°©μ λ²νΈλ₯Ό μ μΆν΄μΌλ§ ν΄μ.
κ·ΈλΌ λ μ κ·Έλ¦Ό κΈ°μ€μμ 3λ² λ Έλλ₯Ό κΈ°μ€μΌλ‘ λ€μ΅μ€νΈλΌλ₯Ό λλ €μΌλ§ ν κΉμ ?
κ·Έλ¬λ©΄ λͺ¨λ λ Έλμμ λ€μ΅μ€νΈλΌλ₯Ό λ리λ κΌ΄μ΄λκΉ$O(n * n log(n))$ μ΄ λλκΉ μκ°μ΄ ν°μ§λλ€!
μμ, μ΄κ±Έ μ΄λ»κ² ν΄κ²°νλλ,
μ λ μ΄λ κ² μ κ·Όνμ΅λλ€.
μμμ μ΄μ°¨νΌ κ°λ―Έκ΅΄μ λΆνμν λ°©μ λ§λ λ€κ³ νμ§ μμμΌλ―λ‘,
κ° λ Έλλ 1λ² λ°© κΉμ§ κ°λ κ²½λ‘κ° λ¨ νλλ°μ μ‘΄μ¬νμ§ μμ΅λλ€.
μ€μΌμ΄ μ€μΌμ΄.
1λ² λ ΈλλΆν° λ€μ΅μ€νΈλΌλ₯Ό λ리λκ±° κΉμ§λ λμΌν΄μ.
κ·Έλ¦¬κ³ μΆκ°μ μΌλ‘ λ€μ΅μ€νΈλΌλ₯Ό λ릴 λ, λΆλͺ¨ λ Έλμ μ 보κΉμ§ μ ν΄μ€λλ€
μ΄ κ³Όμ μ κ·Έλ₯ νμ λ£μ λ μ΄μ λ Έλλ₯Ό λ€μ λ Έλμ λΆλͺ¨λ‘ μ€μ ν΄μ£Όλ©΄ λμ΄μ.
μ μ΄μ λ€ μμλλ€.
κ·ΈλΌ leaf λ Έλ (λ§λ¨ λ Έλ) λ§ κ΅¬νλ©΄ λΆλͺ¨ λ Έλ μ 보λ₯Ό μ΄μ©ν΄μ ν΄λΉ κ²½λ‘μ λν μ 보λ₯Ό λͺ¨λ μ»μ μ μμ΄μ.
μ κ·Έλ¦Όκ³Ό κ°μ΄
[(8, 15), (5, 10), (2, 5), (1,0)]
κ³Ό κ°μ΄ ν΄λΉ κ²½λ‘μ λ Έλ μ 보μ μλͺ¨νλ 체λ ₯μ μ μ μμ΅λλ€.μ μ½λλ μλμ κ°μ΅λλ₯.
κ·ΈλΌ μ μ½λλ₯Ό μ€ννλ €λ©΄ leaf λ Έλκ° λ¬΄μμΈμ§λ₯Ό μμμΌκ² μ£ ?
leaf λ Έλλ μ 보면 ν΄λΉ λ Έλμ μ΄μ΄μ Έ μλ λ Έλκ° 1κ° λ°μ μλ λ Έλλ€μ λλ€.
μ΄ λ 1λ² λ Έλκ° 2λ² λ Έλλλ§ μ°κ²°λμ΄μλ κ²½μ°λ μμΌλ 1λ² λ Έλλ μ μΈ ν΄μ€λλ₯.
μ κΉμ§μ κ³Όμ μ λͺ¨λ μκ° λ³΅μ‘λ$O(n * log(n)$ μΌλ‘ νμ΅λλ€.
μ μ΄μ κ·ΈλΌ μλ κ·Έλ¦Όμ²λΌ κ° leaf λ Έλ λ³λ‘ κ²½λ‘κ° λͺ¨λ μμ±λμμ΅λλ·
μλ κ·Έλ¦Όμμλ leaf λ Έλκ° 4κ°λΌ 4κ°μ κ²½λ‘κ° μμ±λμμ΄μ.
μ΄μ μ§μ§ λ€ μμ΄μ.
μκΉ μ μ₯ν κ° leaf λ Έλ λ³ κ²½λ‘μμ
[(8, 15), (5, 10), (2, 5), (1,0)]
μμ ν λΆν° pop νλ©΄μ νμ¬ λ°©μ μλ κ°λ―Έμ 체λ ₯μΌλ‘ μ΄λκΉμ§ κ° μλ μ§ μ΄λΆ νμμΌλ‘ μ°Ύμμ€λλ€.
μλ₯Ό λ€λ©΄ 8λ² λ°©μ μλ κ°λ―Έμ 체λ ₯μ΄ 10λ§νΌ μλ€κ³ νμ λ,
2λ² λ°©κΉμ§ κ° μ μκ² μ£ ? (8λ²λ°© κΉμ§μ κ±°λ¦¬κ° 15μ΄κ³ 2λ²λ°© κΉμ§μ κ±°λ¦¬κ° 5μ΄λκΉ 15- 5λ 10 μ λλ€.)
μμ κ°μ κ³Όμ μ μ΄λΆ νμμΌλ‘ μ°Ύμμ£Όλ κ² μ λλ₯.
μ΄λ¬λ©΄ κ° λ Έλ λ³λ‘$O(log(n))$ μ μκ° λ³΅μ‘λλ‘ ν μ μμΌλκΉ,
λͺ¨λ λ Έλλ€ Nκ° * log(N) νλ©΄$O(n*log(n)$ μΌλ‘ λͺ¨λ λ
Έλμ λν΄μ μ΄λκΉμ§ λ»μ μ μλμ§ λ€ κ΅¬ν μ μμ΅λλ€!!
π μλ‘κ² μκ²λ λ΄μ©