Why do so many animals have same-sex relationships? Scientists might have worked it out