CF1572A.Book

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a book with nn 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.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt ( 1t21041 \le t \le 2\cdot10^4 ).

The first line of each test case contains a single integer nn ( 1n21051 \le n \le 2\cdot10^5 ) — number of chapters.

Then nn lines follow. The ii -th line begins with an integer kik_i ( 0kin10 \le k_i \le n-1 ) — number of chapters required to understand the ii -th chapter. Then kik_i integers ai,1,ai,2,,ai,kia_{i,1}, a_{i,2}, \dots, a_{i, k_i} ( 1ai,jn,ai,ji,ai,jai,l1 \le a_{i, j} \le n, a_{i, j} \ne i, a_{i, j} \ne a_{i, l} for jlj \ne l ) follow — the chapters required to understand the ii -th chapter.

It is guaranteed that the sum of nn and sum of kik_i over all testcases do not exceed 21052\cdot10^5 .

输出格式

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}\{2, 4\} in the first reading and chapters {1,3}\{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}\{2, 3, 4\} in the first reading and chapter 11 in the second reading of the book.

In the fifth example, we will understand one chapter in every reading from 55 to 11 .

首页