PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000096. Thế giới độc nhất (Unique World)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Thế giới độc nhất (Unique World)

Unique World

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Unique World has nn houses, numbered 11 through nn, connected by mm 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 cc, 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 ss, a destination house tt, and an exact required total cost CC. The traveler may use only roads belonging to the unique simple path from ss to tt. 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 tt, may not be repeated. Thus every extra detour on an earlier path edge of cost cc increases the total cost by 2c2c and the traversal count by 22.

For instance, suppose the simple path from ss to tt contains roads with costs 22 and 44, in that order. Its direct cost is 66, with two traversals. To reach cost 1818, the first road (cost 22) must be traversed in three extra round trips: the total cost is 6+3⋅(2⋅2)=186+3\cdot(2\cdot2)=18 and the traversal count is 2+3⋅2=82+3\cdot2=8. The final road (cost 44) cannot be repeated.

For each independent query, determine whether a journey with total cost exactly CC 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 n,mn,m. It is guaranteed that m=n−1m=n-1 and the roads form a tree.
  • Each of the next mm lines contains u,v,cu,v,c, describing an undirected road of cost cc.
  • The next line contains qq, the number of queries.
  • Each of the next qq lines contains s,t,Cs,t,C. Source and destination are distinct.

Output

Print one line for each query:

  • No if the exact cost cannot be attained.
  • Yes x otherwise, where xx is the minimum number of traversals.

Match capitalization exactly and use one space between Yes and xx.

Subtasks

  • Subtask 1 (20 points): 2≤n≤102\le n\le10, m=n−1m=n-1, 1≤q≤51\le q\le5, 1≤c≤201\le c\le20, 1≤C≤2001\le C\le200; 1≤u,v,s,t≤n1\le u,v,s,t\le n, u≠vu\ne v, s≠ts\ne t; the edge costs are pairwise distinct and the edges form a tree.
  • Subtask 2 (30 points): 2≤n≤252\le n\le25, m=n−1m=n-1, 1≤q≤101\le q\le10, 1≤c≤5001\le c\le500, 1≤C≤50001\le C\le5000; 1≤u,v,s,t≤n1\le u,v,s,t\le n, u≠vu\ne v, s≠ts\ne t; the edge costs are pairwise distinct and the edges form a tree.
  • Subtask 3 (50 points): 2≤n≤502\le n\le50, m=n−1m=n-1, 1≤q≤201\le q\le20, 1≤c≤1000001\le c\le100000, 1≤C≤1000001\le C\le100000; 1≤u,v,s,t≤n1\le u,v,s,t\le n, u≠vu\ne v, s≠ts\ne t; 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 1→21\to2 costs 22 and uses one road. The path 2→32\to3 costs 2+3=52+3=5, so five is possible using two traversals, while six is impossible because it requires an odd increment of one. The path 4→54\to5 costs 4+5=94+5=9, so the requested ten is also impossible. For 2→42\to4 with required cost 1818, repeat the first edge of cost two for three round trips, giving eight traversals.