The start of the school year means that Nathan is solely responsible for walking his dog, Rex. Out of all places, Deer Lake Park, a popular park in Burnaby, is Rex's favourite. However, the park also has many squirrels, which Rex likes to chase.
The park is made of
Rex is not friends with the squirrels and will chase them whenever he sees them. When Rex enters an area with a squirrel, the squirrel will run away, using some of the paths, to the nearest area with a tree. If there are multiple areas at the same distance, the squirrel will choose the smaller numbered area. Rex will follow the squirrel until it reaches a tree, at which point the squirrel will climb up and disappear, never to be seen again.
If Rex enters an area with a squirrel while already chasing a squirrel, he will herd them together and chase all of them. It can be shown that the squirrels will all run to the same tree.
Nathan cannot stop Rex from chasing a squirrel and has to follow Rex during his chases. However, when Rex is not chasing any squirrel, he will follow Nathan and can be directed. Nathan can only end their walk when Rex is not chasing any squirrels.
Nathan wants to plan his walks in the park in such a way that he can visit different areas without wasting too much time chasing squirrels. He has
Constraints
Subtask 1 [15%]
Subtask 2 [20%]
Subtask 3 [20%]
Subtask 4 [45%]
No additional constraints.
Input Specification
The first line of input contains three integers
The second line of input contains
The third line of input contains
The next
Note that this problem will be online enforced, meaning that input will be given in an encrypted format. To encrypt the data, the values
Output Specification
For each question, output one line containing the correct answer to it.
Sample Input (Encrypted)
5 2 3
1 5
1 2 2 3
1 3
2 3
7 0
Sample Input (Unencrypted)
5 2 3
1 5
1 2 2 3
1 3
4 5
2 5
Sample Output
6
5
4
Explanation for Sample
For the first question, Nathan and Rex start at area 1. Rex chases the squirrel up the tree at area 1. This takes negligible time. They walk to area 2, where Rex chases the squirrel to area 1. This takes 2 minutes. They walk back to area 2 and continue to area 3. This takes 2 minutes. In area 3, Rex chases the squirrel to area 5. This takes 1 minute. They walk back to area 3. This takes 1 minute. The total time is 2 + 2 + 1 + 1 = 6 minutes.
For the second question, Nathan and Rex start at area 4, where Rex chases the squirrel to area 1. This takes 2 minutes. Then they walk to area 2 (the squirrel is already at area 1). This takes 1 minute. Then they walk to area 3, where Rex chases the squirrel to area 5. This takes 2 minutes. Rex stops chasing the squirrel at area 5. The total time is 2 + 1 + 2 = 5 minutes.
For the third question, Nathan and Rex start at area 2, where Rex chases the squirrel to area 1. Then they walk to area 3, where Rex chases the squirrel to area 5. The squirrel runs up the tree at area 5, meaning they have completed their walk. The total time is 4 minutes.
Comments