"Data structure" discussion on multiplication and evaluation of LCA

Source: Internet
Author: User
Ideas

Using tree multiplication method can efficiently find two x, y common ancestor LCA.

We set F[X][K] to represent the 2k generation ancestors of X

F[x][0] is the parent node of X

Because 2k from the X-to-root node can be seen as 2k-1 step from X to 2k-1 step .

So for 1≤k≤logn there is F[x][k]=f[f[x][k-1]][k-1] (similar to the dichotomy of thought)

Pretreatment:

So we can iterate over the tree and get all the f[x][0] and calculate all the values of the F-array.

To seek LCA:

Set Dep[x] to the depth of X dep[x]≥dep[y] (otherwise you can exchange X and y)

Use binary split to adjust x and Y to the same depth

If this

"Data structure" discussion on multiplication and evaluation of LCA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.