#QHD0000096. Thế giới độc nhất (Unique World)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Thế giới độc nhất (Unique World)
Unique World
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
Unique World has houses, numbered through , connected by undirected roads. There is exactly one simple path between every pair of houses: the road network is a tree. Each road has a positive integer traversal cost , and distinct roads have distinct costs. A journey's cost is the sum of the costs of every road traversal, including repeated traversals, and its length is the number of traversals, counting repeats.
A query specifies a source house , a destination house , and an exact required total cost . The traveler may use only roads belonging to the unique simple path from to . Before continuing toward the destination, the traveler may traverse a road and return over that road to increase the total cost. The final road of the simple path, which enters , may not be repeated. Thus every extra detour on an earlier path edge of cost increases the total cost by and the traversal count by .
For instance, suppose the simple path from to contains roads with costs and , in that order. Its direct cost is , with two traversals. To reach cost , the first road (cost ) must be traversed in three extra round trips: the total cost is and the traversal count is . The final road (cost ) cannot be repeated.
For each independent query, determine whether a journey with total cost exactly exists. If it does, find the minimum number of road traversals among all such journeys. Queries do not modify the road network.
Input
- The first line contains integers . It is guaranteed that and the roads form a tree.
- Each of the next lines contains , describing an undirected road of cost .
- The next line contains , the number of queries.
- Each of the next lines contains . Source and destination are distinct.
Output
Print one line for each query:
Noif the exact cost cannot be attained.Yes xotherwise, where is the minimum number of traversals.
Match capitalization exactly and use one space between Yes and .
Subtasks
- Subtask 1 (20 points): , , , , ; , , ; the edge costs are pairwise distinct and the edges form a tree.
- Subtask 2 (30 points): , , , , ; , , ; the edge costs are pairwise distinct and the edges form a tree.
- Subtask 3 (50 points): , , , , ; , , ; the edge costs are pairwise distinct and the edges form a tree.
Examples
Example 1
Input
5 4
1 2 2
1 3 3
1 4 4
1 5 5
5
1 2 2
2 3 5
2 3 6
4 5 10
2 4 18
Output
Yes 1
Yes 2
No
No
Yes 8
Explanation: The direct path costs and uses one road. The path costs , so five is possible using two traversals, while six is impossible because it requires an odd increment of one. The path costs , so the requested ten is also impossible. For with required cost , repeat the first edge of cost two for three round trips, giving eight traversals.