Simple Graph and Strange Queries
Time limit2sMemory limit1024 MB
Given an unweighted undirected graph, answer for each query whether a walk from a to b has a length that is a multiple of k.
- Level
Medium7 of 10
- Topics
- Graph, BFS, Number theory
- Solved
- No attempts yet
Problem
A simple undirected weighted graph with vertices and edges is given. Vertices are numbered from to , and the weight of every edge is exactly . There are queries of the following form.
a b k: If there is a path from vertex to vertex whose length is a multiple of , printYes. Otherwise, printNo.
A path may repeat vertices or edges. The length of a path is the sum of the weights of the edges on it, so an edge that appears several times adds its weight once per traversal. Handle the queries correctly.
Input
The first line contains three integers , , and , separated by spaces. (; ; )
Each of the next lines contains three integers , , and , meaning that edge connects and and has weight . Each pair of vertices is connected by at most one edge. (; ; )
Each of the next lines, starting from line , contains a query in the form a b k. (; ; )
All input values are integers.
Output
For each query, print Yes if there is a path from to whose length is a multiple of , and No otherwise. Print one answer per line, in the order of the queries.