示例#1
0
 public WeightedUnionFindNode()
 {
     _height    = 0;
     _groupSize = 1;
     _parent    = this;
     Weight     = 0;
 }
示例#2
0
                public bool Unite(WeightedUnionFindNode other, long weight)
                {
                    weight += Weight;
                    weight -= other.Weight;

                    var thisRoot  = this.FindRoot();
                    var otherRoot = other.FindRoot();

                    if (thisRoot == otherRoot)
                    {
                        return(false);
                    }

                    if (thisRoot._height < otherRoot._height)
                    {
                        thisRoot._parent      = otherRoot;
                        otherRoot._groupSize += thisRoot._groupSize;
                        otherRoot._height     = Math.Max(thisRoot._height + 1, otherRoot._height);
                        thisRoot.Weight       = -weight;
                        return(true);
                    }
                    else
                    {
                        otherRoot._parent    = thisRoot;
                        thisRoot._groupSize += otherRoot._groupSize;
                        thisRoot._height     = Math.Max(otherRoot._height + 1, thisRoot._height);
                        otherRoot.Weight     = +weight;
                        return(true);
                    }
                }
示例#3
0
                public WeightedUnionFindNode FindRoot()
                {
                    if (_parent != this) // not ref equals
                    {
                        var root = _parent.FindRoot();
                        _weight += _parent._weight;
                        _parent  = root;
                    }

                    return(_parent);
                }
示例#4
0
 public bool IsInSameGroup(WeightedUnionFindNode other) => this.FindRoot() == other.FindRoot();