23 path - MarisaOJ: Marisa Online Judge

23 path

Time limit: 1000 ms
Memory limit: 256 MB
Given an undirected graph with $n$ vertices and $m$ weighted edges. The weight of each edge can be expressed in the form $2^a \times 3^b$. For $q$ queries of the form $u,v,a,b$, check whether there exists a path from $u$ to $v$ such that the least common multiple (LCM) of the weights of the edges on the path equals $2^a \times 3^b$. A path can include repeated edges. ### Input - The first line contains two integers $n$ and $m$. - The next $m$ lines each contain four integers $u,v,a,b$, representing an edge from $u$ to $v$ with weight $2^a \times 3^b$. - The next line contains an integer $q$. - The next $q$ lines each contain four integers $u,v,a,b$, a query. ### Output - For each query, print `Yes` if there exists a valid path; otherwise, print `No`. ### Constraints - $1 \le n,q \le 5 \times 10^4$. - $1 \le m \le 10^5$. - $1 \le u,v \le n$. - $1 \le a,b \le 10^9$. ### Sample Test Input: ``` 4 5 1 4 2 1 1 3 1 2 3 4 2 2 1 2 1 3 2 4 3 2 5 4 2 2 3 1 4 3 3 1 3 4 4 2 3 2 2 1 3 2 2 ``` Output ``` Yes Yes No No Yes ```