QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 2048 MB Total points: 100 Difficulty: [show] Hackable ✓

#22886. 合并序列

Statistics

小青鱼有 $m$ 个序列,共包含 $n$ 个整数。从 $1$ 到 $n$ 的每个整数在所有序列中都恰好出现一次。

他想要将这些序列合并为一个长度为 $n$ 的排列 $p$。初始时,$p$ 为空。在每一步中,他选择一个非空序列,移除其第一个整数,并将该整数追加到 $p$ 的末尾。他重复这一过程,直到所有序列都为空。

如果排列 $p$ 中的某个整数的值等于它在 $p$ 中的位置,则称该整数为 $p$ 的一个不动点。位置从 $1$ 到 $n$ 编号。

请找出 $p$ 中不动点数量的最小可能值。

输入格式

本题包含多组测试数据。输入的第一行包含一个整数 $T$($1 \le T \le 10^6$),表示测试数据的组数。

对于每组测试数据,第一行包含两个整数 $n$ 和 $m$($1 \le m \le n \le 10^6$),分别表示整数的总数与序列的数量。

接下来 $m$ 行描述这些序列。其中第 $i$ 行包含一个整数 $k_i$($1 \le k_i \le n$),随后是 $k_i$ 个整数 $a_{i,1}, a_{i,2}, \ldots, a_{i,k_i}$($1 \le a_{i,j} \le n$),按顺序描述第 $i$ 个序列。

保证在每组测试数据中,所有序列的 $k_i$ 之和为 $n$,且从 $1$ 到 $n$ 的每个整数在所有序列中都恰好出现一次。

保证所有测试数据的 $n$ 之和不超过 $10^6$,且所有测试数据的 $m$ 之和不超过 $10^6$。

输出格式

对于每组测试数据,输出一行一个整数,表示 $p$ 中不动点数量的最小可能值。

样例

输入格式 1

4
1 1
1 1
3 2
1 1
2 2 3
3 2
2 1 3
1 2
4 2
3 2 1 3
1 4

输出格式 1

1
0
1
0

说明

在第一组样例测试数据中,唯一可能的排列是 $[1]$,它有一个不动点。

在第二组样例测试数据中,小青鱼可以得到 $p = [2, 3, 1]$,它没有不动点。

在第三组样例测试数据中,$p = [1, 3, 2]$ 是一个最优排列,它有一个不动点。

在第四组样例测试数据中,他可以得到 $p = [2, 4, 1, 3]$,它没有不动点。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.