#K38222. Counting Friend Groups
Counting Friend Groups
Counting Friend Groups
You are given a social network of users where each user may have zero or more friends. A friend group is defined as a set of users such that any two users in the group are connected either directly or through a chain of friendships. In other words, users u and v belong to the same group if there exists a sequence of users \(u = a_1, a_2, \dots, a_k = v\) such that each adjacent pair \(a_i, a_{i+1}\) are friends.
Your task is to count the number of friend groups (i.e. connected components in the graph) for each test case.
The relationships are provided in a slightly unusual format. For each test case, the first line contains an integer n which is the number of users. The following n lines each describe a user. The first token in a line is the user ID, followed by zero or more friend IDs separated by spaces. Note that if a friendship is listed for one user, you should assume it is mutual.
Note: All formulas are in LaTeX format. For instance, the connectivity condition can be written as \(u \sim v\) if there exists a path connecting \(u\) and \(v\).
inputFormat
The input starts with a single integer T
, the number of test cases.
For each test case:
- The first line contains an integer
n
, the number of users. - The next
n
lines each describe a user. The first integer in each line is the user ID, followed by zero or more space-separated integers representing the IDs of that user's friends.
Input is given through standard input (stdin).
outputFormat
For each test case, output a single integer on a new line representing the number of friend groups in that test case. Output is printed to standard output (stdout).
## sample2
3
1 2 3
2 1
3 1
4
1 2 3
2 1 4
3 1
4 2
1
1
</p>