-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from collections import defaultdict, deque | ||
from heapq import * | ||
import sys | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
N = int(input()) | ||
ant = [int(input()) for _ in range(N)] | ||
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 | ||
|
||
cost = [int(1e9) for _ in range(N+1)] | ||
heap = [(0,1)] | ||
parent = [0 for idx in range(N+1)] | ||
|
||
while heap: | ||
now_cost, now_node = heappop(heap) | ||
|
||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. π«‘π«‘π«‘ππ»ππ»ππ»ππ» |
||
if now_cost + next_cost < cost[next_node]: | ||
heappush(heap, (now_cost + next_cost, next_node)) | ||
parent[next_node] = now_node | ||
|
||
leaf_node = deque() | ||
for node, value in enumerate(neighbor_count): | ||
if node in (0, 1): continue | ||
if value == 1: leaf_node.append(node) | ||
Comment on lines
+35
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
... |
||
|
||
#print(leaf_node) | ||
|
||
def check(mid, now_road, energy): | ||
if now_road[0][1] - now_road[mid][1] <= energy: return True | ||
return False | ||
|
||
answer = [0 for _ in range(N+1)] | ||
|
||
#print() | ||
#print(ant) | ||
for leaf in leaf_node: | ||
now_node = leaf | ||
now_road = deque() | ||
while now_node != 0: | ||
now_road.append((now_node, cost[now_node])) | ||
now_node = parent[now_node] | ||
|
||
while now_road: | ||
now_idx, now_distance = now_road[0] | ||
now_energy = ant[now_idx-1] | ||
|
||
if answer[now_idx] != 0: | ||
now_road.popleft() | ||
continue | ||
|
||
# print(now_road) | ||
|
||
left = -1 | ||
right = len(now_road) | ||
temp = 0 | ||
while left+1<right: | ||
mid = (left+right)//2 | ||
#print(left, mid, right) | ||
|
||
if check(mid, now_road, now_energy): | ||
temp = mid | ||
left = mid | ||
|
||
else: right = mid | ||
|
||
answer[now_idx] = now_road[temp][0] | ||
#print(answer[1:]) | ||
#print() | ||
now_road.popleft() | ||
|
||
for ans in answer[1:]: print(ans) |
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λ₯Ό μ¨μ κ·Έλν(νΈλ¦¬) μ 보λ₯Ό κΈ°λ‘ν©λλ€.
μ΄λ° μμΌλ‘ νλ©΄, λ°μμ 리ν λ Έλλ₯Ό νλ³ν λ
μ΄λ° μμΌλ‘ iλ²μ§Έ λ Έλμ μΈμ 리μ€νΈ κΈΈμ΄κ° 1μΈ κ²½μ°μλ§ νμΈνλ μμΌλ‘ κ²μ¬ν μ μμ΅λλ€.