#P8972. Restore the Past
Restore the Past
Restore the Past
You are given a tree with \( n \) nodes. Each node has a weight and each edge has a weight (both can be a rational number, represented as either an integer or in the form \( \frac{a}{b} \)). You are also given \( q \) queries.
For each query, you are given two integers \( x \) and \( y \). Consider the simple (i.e. without revisiting any edge) path between node \( x \) and node \( y \). Let the product of all the edge weights on this path be \( P_e \). Then define the overall product as \( P = a_x \times P_e \), where \( a_x \) is the weight of the starting node \( x \).
Your task is to determine whether \( P \) is an integer. We say the path "restores the past" if and only if \( P \) is an integer.
Note: The input rational numbers will be given as either an integer (e.g. 2) or in the form a/b (e.g. 3/4). It is guaranteed that the tree is connected and has no cycles.
inputFormat
The first line contains two integers \( n \) and \( q \) --- the number of nodes and the number of queries.
The second line contains \( n \) tokens, where the \( i\)-th token represents the weight of node \( i \). Each token is either an integer or of the form a/b representing a rational number.
Each of the next \( n-1 \) lines contains three tokens: two integers \( u \) and \( v \) and a token \( w \) representing an edge between nodes \( u \) and \( v \) with weight \( w \) (in similar format).
Each of the following \( q \) lines contains two integers \( x \) and \( y \) representing a query.
outputFormat
For each query, output a single line containing either Yes
if \( P \) is an integer, or No
otherwise.
sample
3 3
1 1/2 2
1 2 1/2
2 3 3/4
1 2
2 3
1 3
No
No
No
</p>