Problem
In a weighted tree, each edge is assigned a (usually positive) number, called its weight. The distance between two nodes in a weighted tree becomes the sum of the weights along the unique path connecting the nodes.
To generalize Newick format to the case of a weighted tree , during our repeated "key step," if leaves are neighbors in , and all these leaves are incident to , then we replace with , where is now the weight on the edge .
Given: A collection of weighted trees () in Newick format, with each tree containing at most 200 nodes; each tree is followed by a pair of nodes and in .
Return: A collection of numbers, for which the th number represents the distance between and in .
在加权树中,每个边都分配有一个(通常为正)编号,称为其权重。加权树中两个节点之间的距离成为沿着连接节点的唯一路径的权重之和。
将Newick格式概括为加权树的情况,在我们重复的“关键步骤”中,如果离开 有邻居在,所有这些叶子都入射到,那么我们替换 与 ,在哪里 现在是边缘上的重量 。
鉴于:的集合 加权树()以Newick格式,每棵树最多包含200个节点;每棵树 其次是一对节点 和 在 。
返回:的集合 数字,为此 th代表之间的距离 和 在 。
Sample Dataset
(dog:42,cat:33); cat dog ((dog:4,cat:3):74,robot:98,elephant:58); dog elephant
Sample Output
75 136