# Distances in Trees solved by 824

Aug. 7, 2012, midnight by Rosalind Team

Topics: Phylogeny

## Paths in Trees

For any two nodes of a tree, a unique path connects the nodes; more specifically, there is a unique path connecting any pair of leaves. Why must this be the case? If more than one path connected two nodes, then they would necessarily form a cycle, which would violate the definition of tree.

The uniqueness of paths connecting nodes in a tree is helpful in phylogenetic analysis because a rudimentary measure of the separation between two taxa is the distance between them in the tree, which is equal to the number of edges on the unique path connecting the two leaves corresponding to the taxa.

## 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 $T$. A collection of leaves $v_1, v_2, \ldots, v_n$ of $T$ are neighbors if they are all adjacent to some internal node $u$. Newick format for $T$ is obtained by iterating the following key step: delete all the edges $\{v_i, u\}$ from $T$ and label $u$ with $(v_1, v_2, \ldots, v_n)u$. 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 $T$, then we simply leave blank the space occupied by the node. In the key step, we can write $(v_1, v_2, \ldots, v_n)$ in place of $(v_1, v_2, \ldots, v_n)u$ if the $v_i$ are labeled; if none of the nodes are labeled, we can write $(, , \ldots, )$.

A second variation of Newick format occurs when $T$ is unrooted, in which case we simply select any internal node to serve as the root of $T$. 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 $T$ in Newick format; see Figure 1.

Given: A collection of $n$ trees ($n \leq 40$) in Newick format, with each tree containing at most 200 nodes; each tree $T_k$ is followed by a pair of nodes $x_k$ and $y_k$ in $T_k$.

Return: A collection of $n$ positive integers, for which the $k$th integer represents the distance between $x_k$ and $y_k$ in $T_k$.

## Sample Dataset

(cat)dog;
dog cat

(dog,cat);
dog cat


## Sample Output

1 2
{"ft_lang_label":"__label__en","ft_lang_prob":0.8882413,"math_prob":0.99674416,"size":2428,"snap":"2020-45-2020-50","text_gpt3_token_len":660,"char_repetition_ratio":0.11014851,"word_repetition_ratio":0.00456621,"special_character_ratio":0.26523888,"punctuation_ratio":0.14119923,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.998983,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-27T20:13:57Z\",\"WARC-Record-ID\":\"<urn:uuid:cbaa74c2-4c3d-43bc-be2f-7f5736347a60>\",\"Content-Length\":\"17472\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:23e04316-f8a5-4af0-a11c-4398c1840ab4>\",\"WARC-Concurrent-To\":\"<urn:uuid:0afd293f-6070-4ff2-9160-69607e975f94>\",\"WARC-IP-Address\":\"13.94.194.131\",\"WARC-Target-URI\":\"http://rosalind.info/problems/nwck/\",\"WARC-Payload-Digest\":\"sha1:LN3ZRNWMFD3E3BTYDHN6WYSY6LRFPZQW\",\"WARC-Block-Digest\":\"sha1:QBC3BL2IB3F47PC2W6B4KX5PDG4J2KWV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141194171.48_warc_CC-MAIN-20201127191451-20201127221451-00245.warc.gz\"}"}