Problem
Figure 1. This tree can be represented in Newick format in a number of ways, including (C, D, (A, B)); (A, (D, C), B); and (((A, B), C), D);.
Newick format is a way of representing trees even more concisely than using an adjacency list, especially when dealing with trees whose internal nodes have not been labeled.
First, consider the case of a rooted tree . A collection of leaves of are neighbors if they are all adjacent to some internal node . Newick format for is obtained by iterating the following key step: delete all the edges from and label with . This process is repeated all the way to the root, at which point a semicolon signals the end of the tree.
A number of variations of Newick format exist. First, if a node is not labeled in , then we simply leave blank the space occupied by the node. In the key step, we can write in place of if the are labeled; if none of the nodes are labeled, we can write .
A second variation of Newick format occurs when is unrooted, in which case we simply select any internal node to serve as the root of . A particularly peculiar case of Newick format arises when we choose a leaf to serve as the root.
Note that there will be a large number of different ways to represent in Newick format; see Figure 1.
Given: A collection of 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 positive integers, for which the th integer represents the distance between and in .
Newick格式是一种比使用邻接表更简洁地表示树的方法,尤其是在处理内部节点未标记的树时。
首先,考虑一棵有根树的情况 。叶子的集合 的 是邻居,如果它们都与某个内部节点相邻。Newick格式 通过重复以下关键步骤获得:删除所有边缘 从 和标签 与 。从头到尾一直重复此过程,这时分号表示树的结尾。
存在许多Newick格式的变体。首先,如果未标记节点,那么我们只需将节点占用的空间留空。在关键步骤中,我们可以编写 代替 如果 被标记; 如果没有节点被标记,我们可以写。
Newick格式的第二种变体出现在 是无根的,在这种情况下,我们只需选择任何内部节点作为根 。当我们选择叶子作为根时,会出现一种特别特殊的Newick格式情况。
请注意,会有很多不同的方式来表示 以Newick格式;参见图1。
鉴于:的集合 树木()以Newick格式,每棵树最多包含200个节点;每棵树 其次是一对节点 和 在 。
返回:的集合 正整数,为此 第一个整数代表之间的距离 和 在 。
Sample Dataset
(cat)dog; dog cat (dog,cat); dog cat
Sample Output
1 2