363. How Many Connected Pieces
Print the number of connected components: the number of separate pieces the graph falls into.
This is the problem where starting from vertex 1 stops being enough. A traversal from vertex 1 reaches its own component and nothing else, so the rest of the graph would be invisible.
Loop over every vertex instead. When you meet one that has not been visited, it belongs to a component nobody has seen, so count one more and run a traversal from it to mark everything in that piece.
The traversal itself is unchanged from the previous two problems. What is new is the loop around it.
Constraints - `1 ≤ n ≤ 100000` - `0 ≤ m ≤ 200000` - There are no self-loops and no repeated edges.
Input
The first line contains two integers n and m, the number of vertices and
edges. Each of the next m lines contains two integers u and v, an
undirected edge between those vertices. Vertices are numbered 1 to n.
Output
Print one integer, the number of connected components.