On disjoint-set and flat

 

 

It is limited, at the problem is not the solution, a lot of understanding

 

Thank you for watching this konjac

 

 

Disjoint-set (universal basic knowledge)
1: Purpose
The purpose disjoint-set is very easy to understand, popular point that is also your relatives relatives relatives used to detect whether or not the same any two points within a set.
2: Implementing Measures
way to achieve mainly to determine whether a common ancestor as far as two points (two points is also the root of the collection is located), we can define a fa [i] array representation i's father, at the beginning of all points his father is his own (as a collection point represents the beginning of each store only one point only, so this collection is the root node points), followed by several groups will read incoming data, such as AB, and a represents the point point B is relative. Then fa [A] assigned to B or fa [B] assignment for the A? Obviously wrong, because the combined A and B do not represent only two points, but a collection of them is located. For example: you and a girl became a pro, the old grandmother of her grandmother's grandfather's grandfather ... and you do not have a cent in the previous relationship, but now Grandpa's grandmother your grandfather and grandmother ... who have become relatives, and what you represent her collection merged.
3: Merge points are in the collections of the root node
may sound hard to pronounce, it was actually very easy to understand, we can define a find function, find (x) represents the root node of the set of points where x, then we just father point to ask whether the point x for himself, if not then ask the father of x ... have been found so far root, then there is a piece of code like this:

code:

. 1  int Find ( int o)
 2  {
 . 3      IF (FA == o [o]) return o; // if point o his father point, then the root node is set according to the present point o 
. 4      return Find (FA [ o]); // if not, to ask the point o father 
5 }

4: Flat handle
the above code is not difficult to see a recursive idea, if the data is too large will cause a lot of useless waste, due to the disjoint-set the relationship between the root node requires only a set of merge point, we would simply set fa [i] where i is directly assigned to the root node of the set (because the other points useless), say there is such an original collection:

 

 

 

And after such a treatment becomes (draw no less than the middle, on the use of ellipses in place):

 

 

 

After this the original search find (10) recursively 5 times, and the flattening processing only twice. In fact, the code is very simple to write:

code:

. 1  int Find ( int O)
 2  {
 . 3      IF (O == fa [o]) return O;
 . 4      return fa [o] = Find (fa [o]); // directly fa [o] assignment for set the root node 
5 }

 

Guess you like

Origin www.cnblogs.com/nlyzl/p/11776269.html