halfway decent generalized kruskal's algorithm