diff --git a/cs-algorithms/UnionFind/WeightedQuickUnionWithPathCompression.cs b/cs-algorithms/UnionFind/WeightedQuickUnionWithPathCompression.cs index 8eb0403..26a41be 100644 --- a/cs-algorithms/UnionFind/WeightedQuickUnionWithPathCompression.cs +++ b/cs-algorithms/UnionFind/WeightedQuickUnionWithPathCompression.cs @@ -40,16 +40,19 @@ public void Union(int p, int q) { int i = GetRoot(p); int j = GetRoot(q); - if (mSize[i] < mSize[j]) + if (i!=j) { - mParent[i] = j; - mSize[j] += mSize[i]; - } - else - { - mParent[j] = i; - mSize[i] += mSize[j]; - } + if (mSize[i] < mSize[j]) + { + mParent[i] = j; + mSize[j] += mSize[i]; + } + else + { + mParent[j] = i; + mSize[i] += mSize[j]; + } + } } public bool IsConnected(int i, int j)