Back
Close

Magic Castle

Statement

 Goal

A Magic Castle has n locked rooms labeled from 0 to n - 1 and you have a key only to room 0.

You want to get treasures from all the rooms, but you need to have a key to enter a locked room.

In every room random keys are waiting for you with numbers pointing to a corresponding door.
And you can take all of them with you to use later.

Given a count of rooms in a castle and key sets in each of them you need to check if you can grab all the treasures.
Input
line 1: n - number of rooms in a castle.
The next n lines: The set of m keys in a room separated by spaces.
Output
Please output number of unreachable rooms. If all rooms are reachable - it should be 0.
Constraints
2 <= n <= 99
1 <= m <= 10
Example
Input
4
1 2
2 1
3 0
0
Output
0

Game modes
Fastest

Test cases
Test 1 Test
Input
4 1 2 2 1 3 0 0
Output
0

Validator 1 Validator
Input
3 1 2 0
Output
0

Test 2 Test
Input
3 0 1 2
Output
2

Validator 2 Validator
Input
2 0 1
Output
1

Test 3 Test
Input
3 1 1 2 0 2
Output
0

Validator 3 Validator
Input
3 0 1 1 2 2
Output
0

Test 4 Test
Input
4 1 3 3 0 1 2 0
Output
1

Validator 4 Validator
Input
4 0 2 1 2 2 0 3 2 0
Output
2

Test 5 Test
Input
10 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
Output
0

Validator 5 Validator
Input
10 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Output
0

Test 6 Test
Input
5 4 3 1 3 2 4 2 4 0 3
Output
1

Validator 6 Validator
Input
5 0 1 2 1 2 4 3 0 2
Output
4

Solution language

Solution

Stub generator input