CF1572A.Book
普及+/提高
通过率:0%
时间限制:1.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a book with n chapters.
Each chapter has a specified list of other chapters that need to be understood in order to understand this chapter. To understand a chapter, you must read it after you understand every chapter on its required list.
Currently you don't understand any of the chapters. You are going to read the book from the beginning till the end repeatedly until you understand the whole book. Note that if you read a chapter at a moment when you don't understand some of the required chapters, you don't understand this chapter.
Determine how many times you will read the book to understand every chapter, or determine that you will never understand every chapter no matter how many times you read the book.
你有一本包含 n 章的书。
每一章都指定了一个必须先理解的其他章节列表;要理解某一章,你必须在理解该章所要求的所有章节之后,再阅读该章。
目前你尚未理解任何一章。你将从头到尾反复阅读这本书,直到完全理解整本书为止。注意:若你在某时刻阅读某一章时,尚有该章所要求的某些章节未被理解,则此次阅读不会使你理解该章。
请确定:你需要完整阅读这本书多少次才能理解全部章节;或者判断无论阅读多少次,你都不可能理解全部章节。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤2⋅104).
The first line of each test case contains a single integer n (1≤n≤2⋅105) — number of chapters.
Then n lines follow. The i-th line begins with an integer ki (0≤ki≤n−1) — number of chapters required to understand the i-th chapter. Then ki integers ai,1,ai,2,…,ai,ki (1≤ai,j≤n,ai,j=i,ai,j=ai,l for j=l) follow — the chapters required to understand the i-th chapter.
It is guaranteed that the sum of n and sum of ki over all testcases do not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤2⋅104)。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 章节数量。
接下来是 n 行。第 i 行以一个整数 ki(0≤ki≤n−1)开头——理解第 i 章所需的前提章节数量。随后是 ki 个整数 ai,1,ai,2,…,ai,ki(1≤ai,j≤n,且 ai,j=i,当 j=l 时 ai,j=ai,l)—— 理解第 i 章所需的前提章节编号。
保证所有测试用例中 n 的总和以及所有 ki 的总和均不超过 2⋅105。
输出格式
For each test case, if the entire book can be understood, print how many times you will read it, otherwise print −1.
对于每个测试用例,如果整本书都能被理解,则输出你将阅读它的次数;否则输出 −1。
输入输出样例
输入#1
5 4 1 2 0 2 1 4 1 2 5 1 5 1 1 1 2 1 3 1 4 5 0 0 2 1 2 1 2 2 2 1 4 2 2 3 0 0 2 3 2 5 1 2 1 3 1 4 1 5 0
输出#1
2 -1 1 2 5
说明/提示
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.
在第一个例子中,我们将在第一次阅读时理解第 2 和 4 章,在第二次阅读时理解第 1 和 3 章。
在第二个例子中,每一章的理解都依赖于其他某些章节,因此无法理解整本书。
在第三个例子中,每一章仅依赖于书中排在其前面的章节,因此我们可以一次性理解全部内容。
在第四个例子中,我们将在第一次阅读时理解第 2、3 和 4 章,在第二次阅读时理解第 1 章。
在第五个例子中,我们将从第 5 章到第 1 章,每次阅读理解一章。
输入解题思路,AI测评打分。不知道怎么写?