Story: LCA and multiplication (2)

Source: Internet
Author: User

Before that, we need to know how to multiply

The story about it cannot help you. You can refer to the following link. It is absolutely amazing.

Vernacular Multiplication

After reading this, you may not be able2b White RabbitCheers.

In the evening, the brochure measures the ends of the earth, and the magic of the day despise the crowd

In computer termsEach time, based on the obtained information, the scale to be considered is doubled to achieve acceleration.

It has two functions

First, acceleration of state transfer when the change rules are the same

For example, if you throw a coin, the coin is either a positive coin or a coin is thrown twice at a time. What about four or eight times?

Second, acceleration interval operations

This is the key to the use of LCA, such as the story of the white rabbit. If you want to step into the sky, you only need to use the book!

Here you can clearly see that this is actuallyExchange Space for timeThis will inevitably lead to huge space loss, so we need to make rational use of it.

As for the context of doubling, this is the end, because the LCA still gives us

For more information, see Zhu's analysis.

This article also benefits

Well, let's get to know how multiplication and LCA are created on this day.

From the above, we know that we conduct DFS for the two vertices in the Process of final LCA. After one vertex is searched, its ancestor is saved, and then another vertex starts searching and storing the ancestor for a comparison.

It's too much trouble to think about it.

Isn't the last step of tracing very similar to the process in which DFS was used to clear their relationships? It's just that the purpose is different.

View code

1. Clear DFS relationships

Void DFS (int u, int FA) // U is the current node. Fa is its father's day point {d [u] = d [fa] + 1; // U is younger than FA. For (INT I = head [u]; I! =-1; I = E [I]. Next) // find its son {int v = E [I]. V; If (V! = FA) // not a father, or a son DFS (v, U); // a father }}

2. Find the LCA

For (INT I = head [u]; I; I = edge [I]. NEX) {int v = edge [I]. to; If (d [v] <D [u]) // {for (INT I = 1; I <= ans; I ++) if (V = mark [I]) {cout <"Found !! "; Return;} // check whether your current ancestor succeeded in the other's ancestor. Otherwise, continue with I = V; // otherwise, continue with the search }}

Is it very similar? Do you think that the first step is to clear the relationship between DFS?Has the potential to become a rabbit paper.?

There is no error. We only need to use preprocessing to implement our own small Ben, which can be shaken up to 90 thousand miles !!

But how? Well, since it's a small book, we need to open an array. For example, we can take a few steps here to get there.

Well, that's him.Http://dt.ap-southeast-1.maxcompute.aliyun-inc.com;

Then, through preprocessing, we can use it perfectly.

Void DFS (int u, int FA) // U is the parent node of the current node fa {d [u] = d [fa] + 1; // acknowledge that Father P [u] [0] = fa; // it means that the minor book [current position] [takes a few steps] where 0 indicates 20 = 1, that is, u takes a step up. It is obviously his father for (INT I = 1; (1 <I) <= d [u]; I ++) P [u] [I] = P [p [u] [I-1] [I-1]; // take the door so you understand it, preprocessing for (INT I = head [u]; I! =-1; I = E [I]. Next) // continue normally. {Int v = E [I]. V; If (V! = FA) DFS (v, u );}}

This little book is done

We can quickly find the LCA.

Int LCA (int A, int B) // very standard LCA lookup {If (d [a]> d [B]) Swap (A, B ); // ensure that A is above Node B, that is, the depth of A is smaller than the depth of B for (INT I = 20; I> = 0; I --) if (d [a] <= d [B]-(1 <I) B = P [B] [I]; // first move B to the same depth as a if (a = B) return a; // It is the same as a if B comes up, then you can directly return the answer. For (INT I = 20; I> = 0; I --) // start from the stride, {If (P [a] [I] = P [B] [I]) // in this case, do not rush to continue; else A = P [a] [I], B = P [B] [I]; // A and B move up together} return P [a] [0]; // perfect job reception}

Yeah, you have succeeded. You can find out the LCA. What can we do? Suddenly, my heart was quite stunned ..

Well, it is very useful,

Based on the characteristics of the tree

One is the two-point Shortest Path of the tree.

Second, it facilitates interval operations.

Based on these features, you will see that tree difference and tree chain partitioning are big artifacts that achieve O (N + M) and O (logn) excellent efficiency.

For more information, see

Big Tree difference of Gu Z

And chnihh tree links

In addition, you can go to Luogu to answer some questions to enrich your spiritual life.

So this article is over.

I am not very knowledgeable. If you have any academic errors, I hope you will not mention them.

This article has many references. If there is infringement, the villain should look down and identify the mistake.

 

Story: LCA and multiplication (2)

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.