We give an algorithm for deciding whether a planar polynomial differential system has a first integral which factorizes as a product of defining polynomials of curves with only one place at infinity. In the affirmative case, our algorithm computes a minimal first integral. In addition, we solve the Poincaré problem for the class of systems which admit a polynomial first integral as above in the sense that the degree of the minimal first integral can be computed from the reduction of singularities of the corresponding vector field.