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/>}