Regular Stars
A star is an undirected tree that contains exactly one node which degree is bigger than 2. A leaf is a node which degree is 1. A star is a regular star if the distance between two leaves is the same for any pair of leaves in the star.
![]() Image 1. Examples of disconnected graphs which components are stars. The graph in case a) contains three stars two of which are regular stars. The graph in case b) contains three stars all of which are regular stars. The graph in case c) contains three stars none of which is a regular star. |
Â
The task
Find the number of regular stars in a given graph which is a union of disjoint stars.
Input
The first input line contains two integers N and M separated by space. The values indicate (in this order) the number of nodes and the number of edges in the graph. The nodes are labeled by integers 0, 1, ..., N−1. Next, there are M text lines, each describes one edge. The line contains the labels of the edge end nodes separated by space.
The end nodes of an edge and the edges in the input are listed in arbitrary order.
It is guaranteed that the input graph is a union of disjoint stars.
It holds, 4 ≤ N ≤ 105.
Output
The output contains a single text line with one integer representing the number of regular stars in the input graph.
Example 1
Input13 10 0 1 1 2 1 3 8 9 9 12 10 11 10 9 7 5 4 5 5 6Output
2The data of Example 1 are depicted in Image 1a).
Example 2
Input23 20 0 1 0 4 2 3 4 2 4 5 5 6 7 4 8 7 9 10 13 9 13 11 11 12 13 14 15 14 17 16 16 13 20 18 20 19 20 21 20 22Output
3The data of Example 2 are depicted in Image 1b).
Example 3
Input23 19 22 21 21 20 18 20 17 18 16 17 18 19 12 13 14 15 14 12 11 12 10 11 5 6 6 7 6 8 8 9 4 3 3 1 1 0 2 1Output
0The data of Example 3 are depicted in Image 1c).
Public data
The public data set is intended for easier debugging and approximate program correctness checking. The public data set is stored also in the upload system and each time a student submits a solution it is run on the public dataset and the program output to stdout and stderr is available to him/her.
Link to public data set