Flight
Time limit1sMemory limit256 MB
Given a tree and queries (u, v, d), find the fewest steps from u to v when each step must join two vertices at distance at least d, or -1 if unreachable.
- Level
Hard8 of 10
- Topics
- Tree, Shortest path, Greedy
- Solved
- No attempts yet
Problem
You are given a tree with vertices. The distance between two vertices is the number of edges on the simple path between them.
There are queries. Each query gives two vertices and and an integer . A pair of vertices is called good if the distance between them is at least . In each step, you can only move between a good pair of vertices. Find the minimum number of steps needed to get from to . If the destination cannot be reached, the answer is .
The queries are generated from a formula, so there are many of them.
Input
The first line contains three integers , , and : the number of vertices, the number of queries, and the upper bound for . (, , ).
The second line contains integers , , , . For every with , there is an edge between vertices and ().
The third line contains six integers , , , , , and (, , ). The first query is given by , , and .
For , the -th query is generated as follows:
,
,
.
Here, is the answer to query .
Output
Output the integer