예제 #1
0
 public UnionFind(int size)
 {
     Nodes = new UnionFindNode[size];
     for (int i = 0; i < size; i++)
     {
         Nodes[i] = new UnionFindNode(i);
     }
 }
예제 #2
0
        public void UnionByRank(int i, int j)
        {
            int parent_i_index = FindParent(i);
            int parent_j_index = FindParent(j);

            UnionFindNode a = Nodes[parent_i_index];
            UnionFindNode b = Nodes[parent_j_index];

            if (a.Rank > b.Rank)
            {
                b.Parent = a.Parent;
            }
            else if (a.Parent < b.Parent)
            {
                a.Parent = b.Parent;
            }
            else
            {
                a.Rank++;
                b.Parent = a.Parent;
            }
        }