欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1572A.Book
Book
You are given a book with 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.
Input
Each test contains multiple test cases. The first line contains the number of test cases ().
The first line of each test case contains a single integer () — number of chapters.
Then lines follow. The -th line begins with an integer () — number of chapters required to understand the -th chapter. Then integers ($1 \le a_{i, j} \le n, a_{i, j} \ne i, a_{i, j} \ne a_{i, l}$ for ) follow — the chapters required to understand the -th chapter.
It is guaranteed that the sum of and sum of over all testcases do not exceed .
Output
For each test case, if the entire book can be understood, print how many times you will read it, otherwise print .
Note
In the first example, we will understand chapters in the first reading and chapters 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 in the first reading and chapter in the second reading of the book.
In the fifth example, we will understand one chapter in every reading from to .
Samples
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
2
-1
1
2
5
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |