-
Notifications
You must be signed in to change notification settings - Fork 0
/
truck-delivery.py
65 lines (48 loc) · 1.42 KB
/
truck-delivery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Kickstart 2021: Round B, Truck Delivery
import math
def query(C, W, path):
x = 0
for L, A in path:
if L <= W:
x = A
return x
def update(prev, L, A):
i, N = 0, len(prev)
path = []
if prev:
while i < N and prev[i][0] < L:
path.append(prev[i])
i += 1
A_new = math.gcd(path[-1][-1] if path else 0, A)
if not path or path[-1][-1] != A_new:
path.append((L, A_new))
while i < N:
A_next = math.gcd(prev[i][-1], A)
if path[-1][-1] != A_next:
path.append((prev[i][0], A_next))
i += 1
return path
def solve(N, Q, graph, queries):
stack, found, paths = [0], {0}, {0: []}
while stack:
v = stack.pop()
for w in graph[v]:
if w not in found:
found.add(w)
stack.append(w)
paths[w] = update(paths[v], *graph[v][w])
return [query(C, W, paths[C]) for C, W in queries]
# I/O Code
num_cases = int(input())
for case in range(1, num_cases + 1):
N, Q = map(int, input().split())
graph = [dict() for _ in range(N)]
for _ in range(N - 1):
X, Y, L, A = map(int, input().split())
graph[X-1][Y-1] = graph[Y-1][X-1] = (L, A)
queries = []
for i in range(Q):
C, W = map(int, input().split())
queries.append((C-1, W))
y = solve(N, Q, graph, queries)
print('Case #{}:'.format(case), *y)