Efficient UPGMA implementation