Universal Cup Judging System

Universal Cup

时间限制: 2 s 内存限制: 2048 MB 总分: 100 难度: [显示] 可 Hack ✓
统计

小青魚有 $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.