Three lessons worth remembering from this problem:
Always validate connectivity after running Kruskal by counting edges
Union-Find makes cycle detection simple and fast with path compression
Edge counting tells you if the graph is connected without extra work If you can solve this problem, you understand the basics of MST algorithms. This is the foundation for all MST work. The validation pattern appears in many graph problems.