Skip to content
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

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@
| 56μ°¨μ‹œ | 2023.05.18 | BFS | <a href="https://www.acmicpc.net/problem/17836">κ³΅μ£Όλ‹˜μ„ ꡬ해라!</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/193
| 57μ°¨μ‹œ | 2023.05.26 | λΆ„ν•  정볡 | <a href="https://www.acmicpc.net/problem/2263">트리의 순회</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/197
| 58μ°¨μ‹œ | 2023.05.30 | λ°±νŠΈλž˜ν‚Ή | <a href="https://www.acmicpc.net/problem/1799">λΉ„μˆ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/204
| 59μ°¨μ‹œ | 2023.06.03 | λ‹€μ΅μŠ€νŠΈλΌ + 이뢄 탐색 | <a href="https://www.acmicpc.net/problem/14942">개미</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/207
---
85 changes: 85 additions & 0 deletions tgyuuAn/λ‹€μ΅μŠ€νŠΈλΌ/개미.py
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
Comment on lines +9 to +18

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인 κ²½μš°μ—λ§Œ ν™•μΈν•˜λŠ” μ‹μœΌλ‘œ 검사할 수 μžˆμŠ΅λ‹ˆλ‹€.


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():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ”•μ…”λ„ˆλ¦¬ 자료ꡬ쑰λ₯Ό 많이 μ•ˆμ¨μ„œ μƒμ†Œν•œλ°

items()κ°€ λ”•μ…”λ„ˆλ¦¬ key, valueλ₯Ό λ°˜ν™˜ν•΄μ£ΌλŠ” ν•¨μˆ˜λ„€μš” 쀍쀍

λ”°λ‘œ μ •μ˜ν•œμ€„ μ•Œκ³  μ°Ύκ³  μžˆμ—ˆμŠ΅λ‹ˆλ‹€.. γ…Žγ…Ž

Copy link
Member Author

Choose a reason for hiding this comment

The 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

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:
        ...


#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)
Loading