1.introduction
As everyone knows, my friend Billy2007 is not good at The algorithm of tree, So I and he write this blog.
LCA , that is, the recent Common ancestor, refers to the root tree, find out one or two nodes u and v recent Common ancestor.
2.how to ask LCA?
For example:
Given a tree with multiple branches, the public ancestor that is closest to the specified two points is requested.
The first line contains three positive integers,
Unable to parse markup [type=CF_MATHJAX]
$ which respectively represent the number of nodes in the tree, the number of inquiries, and the number of root nodes. Next, n-1n-1 rows each contain two positive integers x, yx,y, indicating that there is a directly connected edge between xx node and yy node (the data is guaranteed to form a tree). Next, MM rows each contain two positive integers a, ba, and b, which means that the nearest common ancestor of aa and bb is inquired.