#P3247. [HNOI2016] 最小公倍数

[HNOI2016] 最小公倍数

Description

Given an undirected graph with NN vertices and MM edges (the vertices are numbered 1,2,,N1, 2, \ldots, N). Each edge has a weight, and every weight can be factorized as 2a×3b2^a \times 3^b.

There are qq queries. In each query, you are given four parameters u,v,au, v, a, and bb. Please determine whether there exists a path from vertex uu to vertex vv such that the least common multiple of the weights of the edges traversed along the path is exactly 2a×3b2^a \times 3^b.

Note: The path does not have to be simple.

Below are some definitions that may be useful. If they differ from other sources, use the definitions below for this problem:
Least common multiple: The least common multiple of kk numbers a1,a2,,aka_1, a_2, \ldots, a_k is the smallest positive integer that is divisible by each aia_i.

Path: A vertex sequence P ⁣:P1,P2,,PkP \colon P_1, P_2, \ldots, P_k is a path if and only if k2k \geq 2, and for any 1i<k1 \leq i < k, there is an edge between PiP_i and Pi+1P_{i+1}.

Simple path: If in a path P ⁣:P1,P2,,PkP \colon P_1, P_2, \ldots, P_k, it holds that PsPtP_s \neq P_t for any 1stk1 \leq s \neq t \leq k, then PP is called a simple path.

Input Format

The first line contains two integers NN and MM, the number of vertices and edges of the graph.

Each of the next MM lines contains four integers u,v,a,bu, v, a, b, representing an edge between vertices uu and vv with weight 2a×3b2^a \times 3^b.

The next line contains an integer qq, the number of queries.

Each of the next qq lines contains four integers u,v,au, v, a, and bb, representing one query. See the problem description for details.

Output Format

For each query, output one line: Yes if such a path exists, otherwise output No (note: the first letter is uppercase, the rest are lowercase).

4 5
1 2 1 3
1 3 1 2
1 4 2 1
2 4 3 2
3 4 2 2
5
1 4 3 3
4 2 2 3
1 3 2 2
2 3 2 2
1 3 4 4
Yes 
Yes 
Yes 
No 
No

Hint

Constraints: 1N,q5×1041 \leq N, q \leq 5 \times 10^4, 1M1051 \leq M \leq 10^5, 0a,b1090 \leq a, b \leq 10^9.

Translated by ChatGPT 5