#K86487. Smallest Student Set Covering All Clubs
Smallest Student Set Covering All Clubs
Smallest Student Set Covering All Clubs
Given a list of students along with the clubs they are members of, your task is to select a smallest subset of students such that the union of their clubs covers all unique clubs.
You will be given the input through stdin in a specified format. In particular, the first line contains an integer n denoting the number of students. For each student, you will receive their name on a separate line, followed by a line containing an integer m (the number of clubs the student belongs to), and then m lines listing the club names. It is guaranteed that n ≥ 1.
Your output should list the selected student names sorted in lexicographical order, each printed on a separate line. If there are several valid answers, outputting any one of them is acceptable; however, for deterministic output, please use the following greedy strategy:
- At each step, choose the student who covers the maximum number of yet uncovered clubs. In the event of a tie, choose the student with the lexicographically smallest name.
- Repeat until all clubs are covered.
- Sort the resulting set of chosen student names in lexicographical order and print them.
This problem is analogous to the classic set cover problem and can be solved using a greedy approach.
inputFormat
The input is given from stdin in the following format:
n name1 m1 club1 club2 ... (m1 lines) name2 m2 club1 club2 ... (m2 lines) ... nameN mN club1 club2 ... (mN lines)
Where:
- n is the number of students.
- Each student's information consists of the student's name and the list of clubs they belong to.
- Club names may contain spaces.
outputFormat
The output should be printed to stdout and consist of the names of the selected students, one per line, sorted in lexicographical order.
## sample5
Alice
2
Math Club
Science Club
Bob
2
Science Club
Drama Club
Charlie
2
Math Club
Drama Club
David
1
Art Club
Eve
2
Art Club
Math Club
Alice
Bob
David
</p>