CF2185G.Mixing MEXes

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given nn arrays a1,a2,…,ana_1, a_2, \ldots, a_n.

The following operation is performed exactly once:

  • Choose any array among a1,a2,…,ana_1, a_2, \ldots, a_n. Suppose you've chosen array aia_i (1≤i≤n1 \leq i \leq n).
  • Choose any element in array aia_i. Suppose you've chosen the jj-th element of aia_i, denoted by ai,ja_{i,j} (1≤j≤∣ai∣1 \leq j \leq |a_i|, where ∣ai∣|a_i| denotes the length of array aia_i).
  • Choose any other array among a1,a2,…ana_1, a_2, \ldots a_n that is not aia_i. Suppose you've chosen aka_k (1≤k≤n,k≠i1 \leq k \leq n, k \neq i).
  • Add ai,ja_{i,j} to the back of array aka_k. Then, remove ai,ja_{i,j} from aia_i.
  • The value of this operation (i,j,k)(i,j,k) is defined as the sum of each array's MEX⁡\operatorname{MEX} after the operation is performed. More formally, the value of an operation after the operation is performed is ∑i=1nMEX⁡(ai)\sum_{i=1} ^{n} \operatorname{MEX}(a_i).

Evaluate the sum of the values of all possible distinct independent operations. Two operations are distinct if the ordered triple of integers (i,j,k)(i,j,k) is different.

MEX⁡(a)\operatorname{MEX}(a) is defined as the smallest non-negative integer that is not present in the array. For example, MEX⁡([1,2,0,5])\operatorname{MEX}([1, 2, 0, 5]) is 33, and MEX⁡([1,2,4,9])\operatorname{MEX}([1, 2, 4, 9]) is 00.

给你 nn 个数组 a1,a2,…,ana_1, a_2, \ldots, a_n。

以下操作恰好执行一次:

  • 在 a1,a2,…,ana_1, a_2, \ldots, a_n 中任选一个数组。假设你选择了数组 aia_i(其中 1≤i≤n1 \leq i \leq n)。
  • 在数组 aia_i 中任选一个元素。假设你选择了 aia_i 的第 jj 个元素,记为 ai,ja_{i,j}(其中 1≤j≤∣ai∣1 \leq j \leq |a_i|,∣ai∣|a_i| 表示数组 aia_i 的长度)。
  • 在 a1,a2,…,ana_1, a_2, \ldots, a_n 中另选一个不同于 aia_i 的数组。假设你选择了 aka_k(其中 1≤k≤n1 \leq k \leq n 且 k≠ik \neq i)。
  • 将 ai,ja_{i,j} 添加到数组 aka_k 的末尾;然后从 aia_i 中删除 ai,ja_{i,j}。
  • 此操作 (i,j,k)(i,j,k) 的值定义为:操作执行后,所有数组的 MEX⁡\operatorname{MEX} 值之和。更准确地说,该操作的值为 ∑i=1nMEX⁡(ai)\sum_{i=1} ^{n} \operatorname{MEX}(a_i)。

请计算所有可能的、互不相同的独立操作的值之和。若两个操作对应的三元组 (i,j,k)(i,j,k)(有序整数三元组)不同,则称这两个操作互不相同。

MEX⁡(a)\operatorname{MEX}(a) 定义为数组 aa 中未出现的最小非负整数。例如,MEX⁡([1,2,0,5])=3\operatorname{MEX}([1, 2, 0, 5]) = 3,而 MEX⁡([1,2,4,9])=0\operatorname{MEX}([1, 2, 4, 9]) = 0。

输入格式

The first line of the input contains a single integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the number of arrays.

The next nn lines start with lil_i (1≤li≤1051 \le l_i \le 10^5) — the length of the iith array — then contain lil_i integers a1,a2,…,alia_1, a_2, \ldots, a_{l_i} (0≤aij≤1060 \le a_{i_j} \le 10^6) — the array aia_i.

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

输入的第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4)—— 表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 表示数组的个数。

接下来的 nn 行,每行以 lil_i(1≤li≤1051 \le l_i \le 10^5)开头,表示第 ii 个数组的长度;随后是 lil_i 个整数 a1,a2,…,alia_1, a_2, \ldots, a_{l_i}(0≤aij≤1060 \le a_{i_j} \le 10^6)—— 即数组 aia_i。

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

输出格式

For each test case, output the sum of the values of all possible distinct operations.

对于每个测试用例,输出所有可能的不同操作的值的总和。

输入输出样例

  • 输入#1

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

    输出#1

    6
    0
    50
    8
    43
    19202

说明/提示

For the first test case, we have 3 possible distinct operations:

  • i=1,j=1,k=2i = 1, j = 1, k = 2: The arrays are now [] and [0,1,20, 1, 2], which have a mex⁡\operatorname{mex} of 00 and 33 respectively, so the value of the operation is 33.
  • i=2,j=1,k=1i = 2, j = 1, k = 1: The arrays are now [0,10, 1] and [22], which have a mex⁡\operatorname{mex} of 22 and 00 respectively, so the value of the operation is 22.
  • i=2,j=2,k=1i = 2, j = 2, k = 1: The arrays are now [0,20, 2] and [11], which have a mex⁡\operatorname{mex} of 11 and 00 respectively, so the value of the operation is 11.

For the second test case, since no array contains a zero, the value of all operations will be 00.

对于第一个测试用例,我们有 3 种可能的互不相同的操作:

  • i=1,j=1,k=2i = 1, j = 1, k = 2:此时两个数组分别为 [] 和 [0,1,20, 1, 2],它们的 mex⁡\operatorname{mex} 值分别为 00 和 33,因此该操作的值为 33。
  • i=2,j=1,k=1i = 2, j = 1, k = 1:此时两个数组分别为 [0,10, 1] 和 [22],它们的 mex⁡\operatorname{mex} 值分别为 22 和 00,因此该操作的值为 22。
  • i=2,j=2,k=1i = 2, j = 2, k = 1:此时两个数组分别为 [0,20, 2] 和 [11],它们的 mex⁡\operatorname{mex} 值分别为 11 和 00,因此该操作的值为 11。

对于第二个测试用例,由于没有任何数组包含 00,所有操作的值均为 00。

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

首页