CF BUDDY
← Problems·

1882B · Sets and Union

1300 · bitmasks, brute force, constructive algorithms

Problem: You have nn sets of integers S1,S2,,SnS_{1}, S_{2}, \ldots, S_{n}. We call a set SS attainable, if it is possible to choose some (possibly, none) of the sets S1,S2,,SnS_{1}, S_{2}, \ldots, S_{n} so that SS is equal to their union^{\dagger}. If you choose none of S1,S2,,SnS_{1}, S_{2}, \ldots, S_{n}, their union is an empty set.

Find the maximum number of elements in an attainable SS such that SS1S2SnS \neq S_{1} \cup S_{2} \cup \ldots \cup S_{n}.

^{\dagger} The union of sets A1,A2,,AkA_1, A_2, \ldots, A_k is defined as the set of elements present in at least one of these sets. It is denoted by A1A2AkA_1 \cup A_2 \cup \ldots \cup A_k. For example, {2,4,6}{2,3}{3,6,7}={2,3,4,6,7}\{2, 4, 6\} \cup \{2, 3\} \cup \{3, 6, 7\} = \{2, 3, 4, 6, 7\}.

Input Format: Each test contains multiple test cases. The first line contains the number of test cases tt (1t1001 \le t \le 100). The description of the test cases follows.

The first line of each test case contains a single integer nn (1n501 \le n \le 50).

The following nn lines describe the sets S1,S2,,SnS_1, S_2, \ldots, S_n. The ii-th of these lines contains an integer kik_{i} (1ki501 \le k_{i} \le 50) — the number of elements in SiS_{i}, followed by kik_{i} integers si,1,si,2,,si,kis_{i, 1}, s_{i, 2}, \ldots, s_{i, k_{i}} (1si,1<si,2<<si,ki501 \le s_{i, 1} < s_{i, 2} < \ldots < s_{i, k_{i}} \le 50) — the elements of SiS_{i}.

Output Format: For each test case, print a single integer — the maximum number of elements in an attainable SS such that SS1S2SnS \neq S_{1} \cup S_{2} \cup \ldots \cup S_{n}.

Note: In the first test case, S=S1S3={1,2,3,4}S = S_{1} \cup S_{3} = \{1, 2, 3, 4\} is the largest attainable set not equal to S1S2S3={1,2,3,4,5}S_1 \cup S_2 \cup S_3 = \{1, 2, 3, 4, 5\}.

In the second test case, we can pick S=S2S3S4={2,3,4,5,6}S = S_{2} \cup S_{3} \cup S_{4} = \{2, 3, 4, 5, 6\}.

In the third test case, we can pick S=S2S5=S2S3S5={3,5,6,8,9,10}S = S_{2} \cup S_{5} = S_{2} \cup S_{3} \cup S_{5} = \{3, 5, 6, 8, 9, 10\}.

In the fourth test case, the only attainable set is S=S = \varnothing.

Sample Cases

Case 1

Input

4
3
3 1 2 3
2 4 5
2 3 4
4
4 1 2 3 4
3 2 5 6
3 3 5 6
3 4 5 6
5
1 1
3 3 6 10
1 9
2 1 3
3 5 8 9
1
2 4 28

Output

4
5
6
0

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback