added weighted only implementation, without path compression