CF983E.NN country
省选/NOI-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
In the NN country, there are n cities, numbered from 1 to n, and n−1 roads, connecting them. There is a roads path between any two cities.
There are m 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 q 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?
在 NN 国,共有 n 座城市,编号从 1 到 n,以及 n−1 条连接这些城市的道路。任意两座城市之间都存在一条道路路径。
城市之间有 m 条双向公交线路。公交车沿两座城市之间的最短路径行驶,并在途经的每一座城市停靠。乘坐某条公交线路时,你可以在该线路的任意两个停靠站之间往返。你只能通过公交车在城市间旅行。
你将回答 q 个询问:从一座城市能否到达另一座城市?若能,最少需要乘坐几趟公交车?
输入格式
The first line contains a single integer n (2≤n≤2⋅105) — the number of cities.
The second line contains n−1 integers p2,p3,…,pn (1≤pi<i), where pi means that cities pi and i are connected by road.
The third line contains a single integer m (1≤m≤2⋅105) — the number of bus routes.
Each of the next m lines contains 2 integers a and b (1≤a,b≤n, a=b), meaning that there is a bus route between cities a and b. It is possible that there is more than one route between two cities.
The next line contains a single integer q (1≤q≤2⋅105) — the number of questions you are interested in.
Each of the next q lines contains 2 integers v and u (1≤v,u≤n, v=u), meaning that you are interested if it is possible to get from city v to city u and what is the minimum number of buses you need to use for it.
第一行包含一个整数 n(2≤n≤2⋅105),表示城市的数量。
第二行包含 n−1 个整数 p2,p3,…,pn(1≤pi<i),其中 pi 表示城市 pi 与城市 i 之间有一条道路相连。
第三行包含一个整数 m(1≤m≤2⋅105),表示公交线路的数量。
接下来的 m 行,每行包含两个整数 a 和 b(1≤a,b≤n,a=b),表示城市 a 与城市 b 之间存在一条公交线路。两座城市之间可能存在多条公交线路。
下一行包含一个整数 q(1≤q≤2⋅105),表示你关心的查询数量。
接下来的 q 行,每行包含两个整数 v 和 u(1≤v,u≤n,v=u),表示你想知道:是否能从城市 v 到达城市 u,以及在此过程中最少需要乘坐多少趟公交车。
输出格式
Print the answer for each question on a separate line. If there is no way to get from one city to another, print −1. Otherwise print the minimum number of buses you have to use.
每个问题的答案单独占一行输出。如果无法从一个城市到达另一个城市,则输出 −1;否则输出所需乘坐的最少公交车数量。
输入输出样例
输入#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.
第一个样例的路线已在图中标出。
输入解题思路,AI测评打分。不知道怎么写?