Number of languages passed through
Statement
A translation office works with languages numbered from 1 to N and keeps M dictionaries on its shelves. One dictionary covers K languages. With a single dictionary a document can be carried from any one of the languages that dictionary covers to any other.
A document written in language 1 has come into the office. It has to be carried over to language N. One step picks a dictionary, picks one of the languages that dictionary covers, and rewrites the document in that language. A dictionary that does not cover the language the document is written in at that moment cannot be used. This step is repeated as many times as needed to bring the document all the way to language N.
The person in charge of the office wants the number of languages the document passes through to be as small as possible. That count includes language 1 at the start and language N at the end.
Write a program that takes the languages each dictionary covers and finds the smallest number of languages the document passes through.
For example, suppose there are N=5 languages, K=3, and M=2, and the languages each dictionary covers are the ones in the table below.
| Dictionary | Languages covered |
|---|---|
| 1 | 1, 2, 3 |
| 2 | 3, 4, 5 |
The document written in language 1 is carried to language 3 with dictionary 1, and then to language 5 with dictionary 2. Both dictionaries cover language 3, so it serves as the stepping stone from dictionary 1 to dictionary 2. Writing the steps out in order gives the table below.
| Step | 1 | 2 | 3 |
|---|---|---|---|
| Language of the document | 1 | 3 | 5 |
| Dictionary used | 1 | 2 |
The languages the document passed through are 1, 3, and 5, so there are 3 of them. Paths through language 2 or language 4 also exist, but they raise the number of languages passed through. The answer in this case is therefore 3.
Some language may not appear in any dictionary. If there is no path at all from language 1 to language N, print -1.
Input and Output Examples
Example 1
Input
5 3 2 1 2 3 3 4 5
Output
3
Example 2
Input
10 4 3 10 1 9 8 1 3 6 8 2 3 5 10
Output
2
Example 3
Input
3 2 2 2 1 1 2
Output
-1
Input
First the number of languages N, the number of languages K that one dictionary covers, and the number of dictionaries M are given on one line, separated by spaces. N is a natural number less than or equal to 100,000, and K and M are each a natural number less than or equal to 1,000.
Then M lines follow, giving in order the numbers of the languages covered by dictionary 1 through dictionary M, separated by spaces. A language number is a natural number from 1 to N, and the count of numbers written on one line never goes over K. The total count of language numbers written in the input never goes over 1,000,000.
Output
Print the smallest number of languages the document passes through on one line. The count includes language 1 at the start and language N at the end. If there is no path from language 1 to language N, print -1. Only one number is printed, so no space is left at the end of the line.
Requirements
- A document can be carried between any two languages one dictionary covers in a single step
- The answer is the count of languages the document passed through, so count language 1 at the start and language N at the end as well
- When N is 1 the first language and the last language are the same, so the answer is 1
- Print -1 if there is no path to language N
- Treating every language and every dictionary as a node and joining them gives at most 101,000 nodes
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
3 2 2 2 1 1 2
-1
10 4 3 10 1 9 8 1 3 6 8 2 3 5 10
2
5 3 2 1 2 3 3 4 5
3
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.