This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import sys
from collections import defaultdict
def solve():
n, k = map(int, input().split())
graph = defaultdict(list)
for _ in range(n - 1):
a, b, w = map(int, input().split())
graph[a].append((b, w))
graph[b].append((a, w))
def is_feasible(mid):
nonlocal k
nonlocal graph
def dfs(node, parent, mid):
nonlocal count
nonlocal k
nonlocal graph
if count >= k:
return
for neighbor, weight in graph[node]:
if neighbor != parent:
if weight > mid:
count += 1
dfs(neighbor, node, mid)
count = 0
dfs(1, -1, mid)
return count <= k
left, right = 0, 1e18
while abs(right - left) > 1e-7:
mid = (left + right) / 2
if is_feasible(mid):
right = mid
else:
left = mid
print("{:.7f}".format(right))
def build_parks(node, parent, mid):
nonlocal count
nonlocal k
nonlocal graph
if count >= k:
return
for neighbor, weight in graph[node]:
if neighbor != parent:
if weight > mid:
count += 1
build_parks(neighbor, node, mid)
count = 0
build_parks(1, -1, right)
parks = [i for i in range(1, n + 1) if i <= k]
print(" ".join(map(str, parks)))
if __name__ == "__main__":
solve()
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |