Get the App
SLTechnology News&Howtos  ›  Development  › 

C++ and check what are the knowledge points.

Shulou Source: shulou.com Published: 2022-06-02 12:50:48 10月02日 Update

This article mainly introduces "C++ and find out what knowledge points are there". In daily operation, I believe many people have doubts about C++ and collecting knowledge points. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "C++ and collect knowledge points". Next, please follow the editor to study!

And lookup set is a kind of tree data structure, which is used to deal with some disjoint merge and query problems.

A federation-lookup algorithm defines two operations for this data structure:

Find: determines which subset the element belongs to. It can be used to determine whether two elements belong to the same subset. Union: merges two subsets into the same set.

The problem of searching sets is mainly used in merging elements and querying whether two elements are in the same set, which is widely involved in informatics competitions.

Initialize:

At first, each element is a collection. For example, he is the only one in the "family" in which each person belongs. We also need an f array, which contains the number of his "father". The initial value is f [1] = 1 ~ F [2] = 2. F [n] = n, as shown in the figure

2. Find function: find (a) is used to find the "ancestor" of a. For example, if we want to find the ancestor of a, we have to find the father of a. Until there is a person b whose father is himself, then the ancestor code of b is as follows: int find (int x) {if (f [x] = = x) return x X X * Find function: find (a) is used to find the "ancestor" of a. For example, if we want to find the ancestor of a, we have to find the father of a. Until there is a person b whose father is himself, then the ancestor code of b is as follows: int find (int x) {if (f [x] = = x) return x X X *

Union function:

Union (aformab) is the family that merges the two people of ameme b, and only needs to point the father of a's ancestor to the ancestor of b.

How to merge? Let's set u=find (a), v=find (b), as long as we point the father number of one person to the other, for example, f [u] = v

The code is as follows:

Void Union (int b) / / since union is a reserved word of C++, we have to capitalize u {int u = find (a), v = find (b); / / to find the ancestor of int b, f [u] = v pincer / merging family} Union (amemb)

There is another point to note when merging: why not point a's father directly to b? Because this does not merge completely, the father of a, the father of a. Can't be merged into b's family (think about it, why? ), as shown

5. Query whether the two elements are in the same collection: to query whether find b is in the same collection is to see whether their ancestors are equal, and if so, they are in the same set, otherwise the code is not as follows: if (find (a) = = find (b)) cout

Tags: Ancestor father that is output two elements code query knowledge censor personal point knowledge point function family path learning subset location data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn MariaDB Shulou Tech Info Shulou Technology