CF1819D Misha and Apples

题目描述

给定 $n$ 个集合 $S_i$,第 $i$ 个集合的大小为 $k_i$,集合元素为 $1\sim m$ 的正整数。**特别地,若 $k_i = 0$,则 $S_i$ 可以是正整数 $1\sim m$ 的任意可空子集,由你确定**。 设 **可重集** $S$,初始为空。按编号从小到大依次遍历每个集合,往 $S$ 中加入 $S_i$ 所有元素。每次加入当前集合的所有元素后,若 $S$ 包含重复元素,则清空 $S$。注意,一个集合内的元素 **同时** 被加入 $S$。 你需要确定 $k_i = 0$ 的 $S_i$ 具体包含哪些数,使得最终的 $|S|$ 最大。求出这个最大值。 多组数据。 $1\leq T, \sum n, m\leq 2\times 10 ^ 5$,$0\leq \sum k_i\leq 2\times 10 ^ 5$,$S_i$ 的元素互不相同。 注意不保证 $\sum m$ 的数量级。

输入格式

第一行一个正整数 $T$ 表示数据组数。 对于每组数据,第一行两个正整数 $n, m$。 接下来 $n$ 行,每行先是一个整数 $k_i$,接下来 $k_i$ 个互不相同的正整数描述 $S_i$。

输出格式

对于每组数据,输出一行一个整数表示答案 —— 最终的 $|S|$ 的最大值。

说明/提示

In the first test case, Danya remembers all the shops, so the process will be deterministic. He will take two apples at the first shop and two more at the second, but after he puts them in his backpack, they will disappear. So at the end there will only be $ 2 $ apples left, which he will take at the third shop. In the second test case, if the third shop is empty, then after visiting the fourth shop all the apples will disappear. In any other case the apples will disappear after the third shop, and in the fourth shop Dan can take one apple, so the answer is $ 1 $ . In the third test case, the first shop may sell all kinds of apples, and the second shop may sell nothing. Then all $ 5 $ apples will be left at the end.