Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C++ and check what are the knowledge points.

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report