Groups of Friends
In a class, some pairs of students are friends. A group is made of students linked by friendship, directly or through friends of friends, and a student with no friends is a group alone. The class comes as a graph, one vertex per student and one edge per friendship. Print how many groups there are.
How a graph is written
The first line has the number of vertices n and the number of edges m. Then come m lines, one per edge, each with the two vertices it joins. The vertices are numbered from 0 to n - 1, and an edge works both ways.
0 --- 1 --- 2 3 --- 4 5
This graph is written:
6 3
0 1
1 2
3 4
Vertex 5 has no edges, so it shows up only in the count 6.
The starter code already reads the graph into adj, where adj[u] lists the neighbours of u, then prints what count_groups returns. You write count_groups.
Input
- Line 1:
nandm. There are between 1 and 500 vertices and at most 2000 edges. - The next
mlines: the edges. No edge joins a vertex to itself, and no two edges join the same two vertices.
Output
The number of groups.
Examples
Input:
6 3
0 1
1 2
3 4
Output:
3
0, 1 and 2 are one group, 3 and 4 another, and 5 is alone.
Input:
5 4
0 4
4 2
2 1
1 3
Output:
1
0 and 3 aren't friends, but a chain of friends links them, so everyone is in one group.
Input:
4 0
Output:
4