People like people
Time limit1sMemory limit128 MB
From each voter list of up to three liked students, find the largest group where everyone voted, likes only members, and is liked by a member.
- Level
Medium6 of 10
- Topics
- Graph, Queue, Topological sort
- Solved
- No attempts yet
Problem
The students in Mr. Kim's class held a popularity vote to find out who was popular in the class. Each student wrote down the ID numbers of at most three students, other than himself or herself, whom he or she liked.
After the vote was over and the popular students were chosen, Mr. Kim looked at the result and became curious about the following question: what is the largest core?
A set of students is called a core if every student in the set
- voted,
- liked some students inside the set but no one outside it, and
- was liked by someone inside the set.
More formally, when the class has students, each student is represented by an ID number from to . For example, the table below shows the result of a vote by students in Mr. Kim's class.

Students , , and were absent and did not vote, so they do not appear in the left column of the table.
Mr. Kim found the set to be a core: every student in it voted, liked some students inside the set but no one outside it, and was liked by someone inside the set. Specifically, likes , likes , likes , likes , and likes . Together they like exactly , which is the set itself. This set has five students. is also a core, but it is smaller. In fact, no core with six or more students is possible from this table, so the size of the largest core for the vote above is five.
Write a program that, given a vote result, finds the size of the largest core.
Input
The input consists of test cases. The number of test cases is given on the first line.
The first line of each test case contains two integers and , where () is the number of students in the class and is the number of students who voted.
Each of the next lines describes one student's vote: it begins with an integer () representing a student ID, followed by an integer () representing how many students that student wrote down, followed by the student IDs written down by student .
Output
For each test case, print exactly one line containing a single integer: the size of the largest core.