CF1819D.Misha and Apples

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Schoolboy Misha got tired of doing sports programming, so he decided to quit everything and go to the magical forest to sell magic apples.

His friend Danya came to the magical forest to visit Misha. What was his surprise when he found out that Misha found a lot of friends there, the same former sports programmers. And all of them, like Misha, have their own shop where they sell magic apples. To support his friends, who have changed their lives so drastically, he decided to buy up their entire assortment.

The buying process works as follows: in total there are nn stalls, numbered with integers from 11 to nn, and mm kinds of magic apples, numbered with integers from 11 to mm. Each shop sells some number of kinds of apples. Danya visits all the shops in order of increasing number, starting with the first one. Upon entering the shop he buys one magic apple of each kind sold in that shop and puts them in his backpack.

However, magical apples wouldn't be magical if they were all right. The point is that when two apples of the same type end up together in the backpack, all of the apples in it magically disappear. Importantly, the disappearance happens after Danya has put the apples in the backpack and left the shop.

Upon returning home, Danya realized that somewhere in the forest he had managed to lose his backpack. Unfortunately, for some shops Danya had forgotten what assortment of apples there was. Remembering only for some shops, what kinds of magical apples were sold in them, he wants to know what is the maximum number of apples he could have in his backpack after all his purchases at best.

学编程的少年米沙厌倦了竞技编程,于是决定放弃一切,前往魔法森林售卖魔法苹果。

他的朋友丹亚来到魔法森林探望米沙。令他惊讶的是,米沙在那里结识了许多新朋友——他们也都是从前的竞技编程选手。而且,和米沙一样,他们每个人都拥有自己的店铺,出售魔法苹果。为了支持这些人生发生巨变的朋友,丹亚决定买下他们店铺中全部种类的苹果。

购买过程如下:一共有 nn 个摊位,编号为 11 到 nn;共有 mm 种魔法苹果,编号为 11 到 mm。每个店铺出售其中若干种苹果。丹亚按摊位编号从小到大的顺序依次访问所有摊位,即从第 11 个摊位开始。当他进入某个摊位时,他会购买该摊位所售每一种苹果各一个,并将它们放入自己的背包中。

然而,若魔法苹果毫无魔力可言,那便称不上“魔法”了。关键在于:当背包中出现两个(或更多)同种类的苹果时,背包中所有苹果会瞬间神奇地消失。重要的是,这种消失发生在丹亚将苹果放入背包并离开该摊位之后。

回到家后,丹亚意识到自己在森林某处把背包弄丢了。不幸的是,他对某些摊位所售苹果的种类已经记不清了。他只记得其中部分摊位所售的魔法苹果种类。他想知道:在所有可能的情形中,他最终背包里最多可能剩下多少个苹果?

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤2⋅1051 \le t \le 2 \cdot 10^5) —the number of test cases. The description of test cases follows.

The first line contains two integers nn and mm (1≤n,m≤2⋅1051 \leq n, m \leq 2 \cdot 10^5) —the number of stalls and kinds of apples.

Each of the following nn lines describes the assortment of the next stall in the format described below.

Each line starts with an integer kik_i (0≤ki≤2⋅1050 \le k_i \le 2 \cdot 10^5). This is followed by kik_i of different integers aija_{ij} (1≤aij≤m1 \le a_{ij} \le m) —the kinds of apples sold in the ii-th stall. If ki=0k_i = 0, then Danya does not remember what assortment was in that shop, and the set of apple kinds can be anything (including empty).

It is guaranteed that the sum of all kik_i over all test cases does not exceed 2⋅1052 \cdot 10^5 and the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤2⋅1051 \le t \le 2 \cdot 10^5),表示测试用例的数量。随后是各测试用例的描述。

第一行包含两个整数 nn 和 mm(1≤n,m≤2⋅1051 \leq n, m \leq 2 \cdot 10^5),分别表示摊位数量和苹果种类数量。

接下来的 nn 行,每行描述一个摊位的商品种类,格式如下:

每行以一个整数 kik_i(0≤ki≤2⋅1050 \le k_i \le 2 \cdot 10^5)开头,其后跟着 kik_i 个互不相同的整数 aija_{ij}(1≤aij≤m1 \le a_{ij} \le m),表示第 ii 个摊位所售苹果的种类。若 ki=0k_i = 0,则达尼亚不记得该摊位的商品种类,此时该摊位所售苹果种类可以是任意集合(包括空集)。

保证所有测试用例中 kik_i 的总和不超过 2⋅1052 \cdot 10^5,且所有测试用例中 nn 的总和也不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the maximum number of apples that could be in Dani's backpack after visiting all the shops at best.

对于每个测试用例,输出一个整数——即丹尼在最优地访问所有商店后,背包中可能拥有的苹果最大数量。

输入输出样例

  • 输入#1

    4
    3 4
    2 1 2
    2 4 1
    2 1 2
    4 4
    2 1 2
    2 3 4
    0
    1 1
    2 5
    0
    0
    5 3
    0
    3 1 2 3
    2 3 1
    0
    1 3

    输出#1

    2
    1
    5
    3

说明/提示

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 22 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 11.

In the third test case, the first shop may sell all kinds of apples, and the second shop may sell nothing. Then all 55 apples will be left at the end.

在第一个测试用例中,Danya 记住了所有商店,因此整个过程是确定性的。他将在第一家商店拿走 2 个苹果,在第二家商店再拿走 2 个苹果;但当他把这些苹果放进背包后,它们便会消失。因此最终只剩下 22 个苹果,他将在第三家商店取走这 2 个苹果。

在第二个测试用例中,若第三家商店为空,则在访问第四家商店后,所有苹果都会消失;而在其他任何情况下,苹果都将在访问第三家商店后消失,此时 Dan 在第四家商店可以拿走 1 个苹果,因此答案为 11。

在第三个测试用例中,第一家商店可能出售各种苹果,而第二家商店可能什么也不出售。此时最终将剩下全部 55 个苹果。

输入解题思路,AI测评打分。不知道怎么写?

首页