Problem 639
Checking Existence of Edge Length Limited Paths
Hard · 30 pts · Graph, Union Find, Sorting
Problem
An undirected graph has N nodes and M weighted edges (there may be several edges between the same two nodes). Each query (p, q, limit) asks whether there is a path from p to q using only edges with weight strictly less than limit. Answer all queries offline: sort the queries by limit and the edges by weight, and before answering a query, union every edge lighter than its limit with union-find; then p and q are connected exactly when they share a root.
Input
The first line contains N, M and Q. The next M lines contain edges u v w. The next Q lines contain queries p q limit.
Output
Print YES or NO for each query, one per line, in input order.
Constraints
2 <= N <= 100000; 1 <= M, Q <= 100000; 1 <= w, limit <= 10^9; p != q
Example input
3 4 2 1 2 2 2 3 4 3 1 8 1 2 16 1 2 2 1 3 5
Example output
NO YES
Explanation
With edges 1-2 (weight 2), 2-3 (weight 4), 3-1 (weight 8) and 1-2 (weight 16), the query (1, 2, 2) is NO because the only light enough edge would need weight below 2. The query (1, 3, 5) is YES through 1 → 2 → 3 with weights 2 and 4.