#C9154. Distinct Tree Species Counting
Distinct Tree Species Counting
Distinct Tree Species Counting
You are given a forest represented as several rows. Each row contains a list of integers where each integer represents the species number of a tree. Your task is to count the number of distinct tree species in the entire forest and output the distinct species in ascending order.
The input is provided via stdin and the output must be printed to stdout.
Note: A row may be empty (i.e. contain no tree species). Ensure you correctly handle such cases.
The answer should be output in two lines. The first line contains the total count of distinct species. The second line contains the distinct species numbers in ascending order separated by a single space. If there are no species, print an empty second line.
inputFormat
The first line of input contains an integer n, representing the number of rows in the forest.
Each of the following n lines contains a space-separated list of integers, representing the species numbers in that row. A line may be empty if there are no trees in that row.
outputFormat
The output consists of two lines.
- The first line is an integer representing the total number of distinct species.
- The second line is a space-separated list of the distinct species numbers in ascending order. If there are no species, print an empty line.
3
1 2 3
3 4 5
5 6 1
6
1 2 3 4 5 6
</p>