CF983E.NN country

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

In the NN country, there are nn cities, numbered from 11 to nn , and n1n - 1 roads, connecting them. There is a roads path between any two cities.

There are mm bidirectional bus routes between cities. Buses drive between two cities taking the shortest path with stops in every city they drive through. Travelling by bus, you can travel from any stop on the route to any other. You can travel between cities only by bus.

You are interested in qq questions: is it possible to get from one city to another and what is the minimum number of buses you need to use for it?

输入格式

The first line contains a single integer nn ( 2n21052 \le n \le 2 \cdot 10^5 ) — the number of cities.

The second line contains n1n - 1 integers p2,p3,,pnp_2, p_3, \ldots, p_n ( 1pi<i1 \le p_i < i ), where pip_i means that cities pip_i and ii are connected by road.

The third line contains a single integer mm ( 1m21051 \le m \le 2 \cdot 10^5 ) — the number of bus routes.

Each of the next mm lines contains 22 integers aa and bb ( 1a,bn1 \le a, b \le n , aba \neq b ), meaning that there is a bus route between cities aa and bb . It is possible that there is more than one route between two cities.

The next line contains a single integer qq ( 1q21051 \le q \le 2 \cdot 10^5 ) — the number of questions you are interested in.

Each of the next qq lines contains 22 integers vv and uu ( 1v,un1 \le v, u \le n , vuv \neq u ), meaning that you are interested if it is possible to get from city vv to city uu and what is the minimum number of buses you need to use for it.

输出格式

Print the answer for each question on a separate line. If there is no way to get from one city to another, print 1-1 . Otherwise print the minimum number of buses you have to use.

输入输出样例

  • 输入#1

    7
    1 1 1 4 5 6
    4
    4 2
    5 4
    1 3
    6 7
    6
    4 5
    3 5
    7 2
    4 5
    3 2
    5 3
    

    输出#1

    1
    3
    -1
    1
    2
    3
    
  • 输入#2

    7
    1 1 2 3 4 1
    4
    4 7
    3 5
    7 6
    7 6
    6
    4 6
    3 1
    3 2
    2 7
    6 3
    5 3
    

    输出#2

    1
    -1
    -1
    1
    -1
    1
    

说明/提示

Routes for first sample are marked on the picture.

首页