Generalization of the Fortuin-Kasteleyn transformation and its application to quantum spin simulations