from Fourth Algorithm Practical Skills Test
M - brush painting
AC: python
def solve(N, Q, edges, QS, ordered_edges):
from collections import defaultdict
color = [0] * N
uplink = [None] * N
root = 0
# graph to tree
children, parents = graph_to_tree(N, edges, root)
# ready lca
construct(N, children, root, parents)
def paint(start, end, c):
cur = start
while cur != end:
if color[cur] == 0:
color[cur] = c
uplink[cur] = end
cur = parents[cur]
else:
if query(uplink[cur], end) != end:
# uplink if avobe end
return
cur = uplink[cur]
for a, b, c in reversed(QS):
a -= 1
b -= 1
lca = query(a, b)
paint(a, lca, c)
paint(b, lca, c)
for frm, to in ordered_edges:
if parents[frm] == to:
print(color[frm])
else:
print(color[to])
I've been making these kinds of mistakes related to mistaking vertices and edges.
python
def paint(start, end, c):
cur = start
while cur != end:
if color[cur] == 0:
color[cur] = c
uplink[cur] = end
else:
if query(uplink[cur], end) != end:
# uplink if avobe end
return
cur = uplink[cur]
if cur == end: return
cur = parents[cur]
This page is auto-translated from /nishio/PAST4M using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.