CF1572A Book
题目描述
给你一本有 $n$ 章的书。
每一章都有一个为理解本章而需要理解的其他章节的特定列表。要理解一个章节,你必须在理解了所需列表中的每一章后再阅读。
目前你还不了解任何章节。你要从头到尾反复阅读这本书,直到你理解整本书。请注意,如果您在阅读某一章节时不理解某些必要章节,则表示您暂时不能理解本章。
输出你要读多少次书才能理解每一章。如果你永远不会理解整本书,那么输出 $-1$。
输入格式
第一行一个整数 $T$($1≤t≤2⋅10^
4$),表示数据组数。
对于每组数据,第一行输入一个整数 $n$($1≤n≤2⋅10^5$),表示书的章节数。接下来 $n$ 行,第 $i$ 第一个整数 $k_i$($0≤k_i≤n-1$),表示要理解第 $i$ 章所必须要理解的章节数。之后 $k_i$ 个整数,表示要理解第 $i$ 章所必须要理解的章节的编号。
输出格式
输出共 $T$ 行。对于每组数据,输出至少要看几遍书才能理解整本书。若该书无法理解,则输出 $-1$。
**translated by @xieyikai2333**
说明/提示
In the first example, we will understand chapters $ \{2, 4\} $ in the first reading and chapters $ \{1, 3\} $ in the second reading of the book.
In the second example, every chapter requires the understanding of some other chapter, so it is impossible to understand the book.
In the third example, every chapter requires only chapters that appear earlier in the book, so we can understand everything in one go.
In the fourth example, we will understand chapters $ \{2, 3, 4\} $ in the first reading and chapter $ 1 $ in the second reading of the book.
In the fifth example, we will understand one chapter in every reading from $ 5 $ to $ 1 $ .