Summary of query Set Algorithms

Source: Internet
Author: User

The question of the query set is obvious.

The first is the basic application of the parallel query set, and the basic merging and searching are used. hdu1198 1811 poj2524 1611

Another kind of advanced applications that are represented by the "food chain" and query sets, uses the relative relationship, recursive changes of the relative relationship during path compression, poj1182 1703 2492 1988

Paste the most basic and query set template:

Int find (int x) <br/>{< br/> int r = x, I, j; <br/> while (R! = Set [R]) <br/> r = set [R]; <br/> I = x; <br/> while (I! = R) <br/>{< br/> J = set [I]; <br/> set [I] = r; <br/> I = J; <br/>}< br/> return r; <br/>}< br/> void Merge (int A, int B) <br/>{< br/> If (A> B) <br/> set [a] = B; <br/> else <br/> set [B] = A; <br/>}< br/>


Paste the set template with the relative relationship (mainly the Recursive Implementation and the change of the relative relationship ):

Int find (int x) <br/>{< br/> If (P [x] = x) <br/> return X; <br/> int temp = P [X]; <br/> P [x] = find (P [x]); <br/> r [x] = (R [x] + R [temp]) % 3; <br/> return P [x]; <br/>}< br/> void merge (int x, int y, int RX, int ry, int D) <br/> {<br/> P [ry] = RX; <br/> r [ry] = (R [x]-R [y] + D + 2) % 3; <br/>}

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.