链接:
https://ac.nowcoder.com/acm/contest/57362/D
来源:牛客网
You are given a rooted tree with
nnn nodes labeled from
111 to
nnn, the root of which is the
111-st node, and the
iii-th node has a given value
aia_iai for each
i=1,2,…,ni = 1, 2, \ldots, ni=1,2,…,n.
Given a parameter
mmm and then
qqq queries, the
iii-th of which is represented by two numbers
xix_ixi and
kik_iki, your task is to maximize
dis(u,v)+dis(v,w)+dis(w,u)dis(u,v)+dis(v,w)+dis(w,u)dis(u,v)+dis(v,w)+dis(w,u) by choosing three distinct nodes
uuu,
vvv and
www in the subtree of
xix_ixi such that
au+av+aw≡ki(modm)a_u+a_v+a_w \equiv k_i \pmod mau+av+aw≡ki(modm). Here
dis(u,v)dis(u,v)dis(u,v) is the length of the shortest path between the
uuu-th and the
vvv-th nodes on the tree. If there is no solution for a query, the answer is
000.