CF BUDDY
← Problems·

1801C · Music Festival

1900 · binary search, data structures, dp

Problem: Vitya has a friend Masha, whom he really wants to invite to the festival, where his favorite bands perform. However, in order for a friend to agree, she must first evaluate the released novelties. Vitya knows that if Masha listens to a track that was cooler than all the previous ones, she will get 1 unit of impression. Unfortunately, albums can only be listened to in their entirety, without changing the songs in them in places.

Help Vitya find such an order of albums so that Masha's impression turns out to be as much as possible, and she definitely went to the festival with him.

Input Format: Each test consists of multiple test cases. The first line contains a single integer t (1t2000001 \le t \le 200\,000) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (1n2000001 \le n \le 200\,000) — number of albums.

The album descriptions follow. Each album description consists of two lines:

The first line contains a single integer kik_i (1ki2000001 \le k_i \le 200\,000) — the number of tracks in the iith album.

The following line contains kik_i integers ai,1, ai,2, ai,3, , ai,kia_{i, 1},\ a_{i, 2},\ a_{i, 3},\ \ldots,\ a_{i, k_i} (1ai,j2000001 \le a_{i,j} \le 200\,000) — the coolness of the tracks in the ii album.

Denote for ki\sum k_i the sum of all kik_i. It is guaranteed that ki200000\sum k_i \le 200\,000.

Output Format: For each test case print the singular number  — the maximum impression that Masha can get.

Note: In the first test example, the optimal order is listening to the 4th, 2nd, 3rd and 1st albums.

In this case, Masha will listen to the tracks in the following order: 1; 7; 8, 6; 4, 9, 4, 6, 8 and will receive 4 units of impression.

In the second test example, you must first listen to the 1st, then the 4th, and in any order the 2nd and 3rd. In this case, Masha will get the maximum impression, and for every song in the 1st and 4th albums and nothing for the 2nd and 3rd.

Sample Cases

Case 1

Input

2
4
5
4 9 4 6 8
1
7
2
8 6
1
1
4
2
3 4
2
1 8
2
2 8
2
7 9

Output

4
4

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