A11842 | NN country
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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?
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?
输入格式
The first line contains a single integer $n$ ( $2 \le n \le 2 \cdot 10^5$ ) — the number of cities.
The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ( $1 \le p_i < i$ ), where $p_i$ means that cities $p_i$ and $i$ are connected by road.
The third line contains a single integer $m$ ( $1 \le m \le 2 \cdot 10^5$ ) — the number of bus routes.
Each of the next $m$ lines contains $2$ integers $a$ and $b$ ( $1 \le a, b \le n$ , $a \neq 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 \le q \le 2 \cdot 10^5$ ) — the number of questions you are interested in.
Each of the next $q$ lines contains $2$ integers $v$ and $u$ ( $1 \le v, u \le n$ , $v \neq 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.
The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ( $1 \le p_i < i$ ), where $p_i$ means that cities $p_i$ and $i$ are connected by road.
The third line contains a single integer $m$ ( $1 \le m \le 2 \cdot 10^5$ ) — the number of bus routes.
Each of the next $m$ lines contains $2$ integers $a$ and $b$ ( $1 \le a, b \le n$ , $a \neq 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 \le q \le 2 \cdot 10^5$ ) — the number of questions you are interested in.
Each of the next $q$ lines contains $2$ integers $v$ and $u$ ( $1 \le v, u \le n$ , $v \neq 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.
输出格式
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
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted