## Editorial for GlobeX Cup '18 J5 - Errands

Remember to use this editorial

**only**when stuck, and**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.**Submitting an official solution before solving the problem yourself is a bannable offence.**Author:

For the first subtask, it suffices to BFS from to and from to for each query. Then, to get the answer for the queries we can just sum the distance from to and to .

For this subtask, if is unreachable through either the first or second BFS, print `This is a scam!`

.

**Time Complexity:**

For the second subtask, note that we can instead BFS from to and instead of the other way around. With that information in mind, we can also note that stays constant throughout the queries, so we only need to run one BFS at the beginning from to every other node.

Now, all we have to do for each query is sum and .

Note that if either or are unreachable, print `This is a scam!`

.

**Time Complexity:**

## Comments