LCC '24 Contest 3 S1 - Space Taxi

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

In recent years space travel has become significantly cheaper. With this advancement in technology, space transit has become a booming business, and you just got hired as the logistics coordinator!

You have been provided with a list of N nearby planets, each with a unique integer id \(a_i\space(a_i>0)\).

Each planet a_i is also supplemented with a list of all \(M\space(0\le M\le N)\) planets, with unique integer ids \(m_i\space(m_i>0)\), that a spaceship could reach with one tank of gas.

You have been tasked with writing a program to find the minimum number of stops D that a spaceship needs to make in order to transport a client from their starting planet A to their destination planet B.

If the trip is not possible, output -1

Input Specification

The first line of input contains two space separated integers, A and B.

The next line of input contains the number of nearby planets N.

The following N lines contain an integer a_i, followed by an integer M, followed by M integers m_i.

Output Specification

Output one integer D, the minimum number of stops required, or -1.

Constraints

Subtask 1 [40%]

1\le N\le 500

Subtask 2 [60%]

1\le N\le 10^4

Sample Input

1 4
4
1 1 2
2 3 1 3 4
3 2 2 4
4 2 2 3

Sample Output

1

Sample Explanation

In this case, the spaceship starts at planet 1 and refuels at planet 2 in order to make it to planet 4, for a total of 1 stop.


Comments

There are no comments at the moment.