#K56512. Team Formation
Team Formation
Team Formation
You are given a list of students and each student's preference list. Two students are connected if at least one of them chooses the other. A valid team formation is possible if and only if every connected group (component) of students has at least two members. In other words, every student must have at least one of their preferred teammates (directly or indirectly through a chain of preferences) in the same team.
Formally, construct an undirected graph where each vertex represents a student and an edge exists between two students if one of them lists the other in their preferences. The teams can be viewed as the connected components of the graph. The formation is valid if every connected component has size \( \ge 2 \).
inputFormat
The input begins with an integer \(T\) representing the number of test cases. Each test case is described as follows:
- An integer \(M\) denoting the number of students.
- A single line with \(M\) space-separated strings representing the student names.
- Then, \(M\) lines follow. The \(i\)-th line contains zero or more space-separated names indicating the preferred teammates for the \(i\)-th student. If a student has no preferences, the line will be empty.
All input is read from standard input (stdin).
outputFormat
For each test case, output a single line containing either Yes
if the teams can be formed satisfying the condition, or No
otherwise. The output is written to standard output (stdout).
5
4
Alice Bob Charlie Diana
Bob Charlie
Alice
Alice Diana Bob
Charlie Bob
3
Alice Bob Charlie
Bob
Alice
2
Alice Bob
Bob
Alice
1
Alice
4
Alice Bob Charlie Diana
Bob Charlie
Alice
Alice Diana
Charlie Bob
Yes
No
Yes
No
Yes
</p>