problems / 115

Latency Threshold Queries

medium union-find sorting graphs 2000 ms · 256 MB

Latency Threshold Queries

A datacenter has n hosts, numbered from 1 to n, connected by m physical links. The i-th link joins hosts u_i and v_i and has a measured latency w_i. Links can be used in either direction, there can be several links between the same pair of hosts, and a link may even join a host to itself (a loopback).

The SLA team runs compliance checks of the form: can host u reach host v using only links whose latency is strictly less than some threshold L? A host can always reach itself, even with no links at all. Checks may repeat, and the same pair of hosts may be asked about with different thresholds.

You are given all the links once, then q independent checks. For each check (u, v, L), decide whether u and v are connected in the subgraph formed by only the links with latency < L.

Input

The first line contains three integers n, m and q (1 ≤ n ≤ 10^5, 0 ≤ m ≤ 2·10^5, 1 ≤ q ≤ 2·10^5): the number of hosts, the number of links, and the number of checks.

Each of the next m lines contains three integers u_i, v_i and w_i (1 ≤ u_i, v_i ≤ n, 1 ≤ w_i ≤ 10^9): a link between hosts u_i and v_i with latency w_i.

Each of the next q lines contains three integers u_i, v_i and L_i (1 ≤ u_i, v_i ≤ n, 1 ≤ L_i ≤ 10^9 + 1): a check asking whether u_i and v_i are connected using only links with latency strictly less than L_i.

Output

Print q lines. The i-th line must be YES if the i-th check's hosts are connected using only links with latency < L_i, and NO otherwise.

Notes

In the first example, sorting the links by latency gives (2,3,3), (1,2,5), (1,3,8), (4,5,10).

  • Check 1 3 4 only allows the link of latency 3, so 1 and 3 are not yet connected: NO.
  • Check 1 3 9 allows every link except (4,5,10), joining 1, 2 and 3: YES.
  • Check 4 5 10 excludes (4,5,10) itself, since the latency must be strictly less than 10: NO.
  • Check 4 5 11 allows (4,5,10): YES.
  • Check 2 2 1 asks whether host 2 reaches itself, which is always true, even with a threshold of 1 and no usable links: YES.

In the second example, (3,3,5) is a loopback and the two copies of (1,2,2) are parallel links; neither changes which hosts are reachable from which. The check 1 3 7 excludes the link (2,3,7) (not strictly below 7), so 1 and 3 are not yet connected, but raising the threshold to 8 includes it and connects them.

Example 1

Input

5 4 5
1 2 5
2 3 3
4 5 10
1 3 8
1 3 4
1 3 9
4 5 10
4 5 11
2 2 1

Output

NO
YES
NO
YES
YES

Example 2

Input

4 5 4
1 2 2
1 2 2
3 3 5
2 3 7
1 4 100
1 3 7
1 3 8
1 4 100
1 4 101

Output

NO
YES
NO
YES

Hints · reveal one at a time if you're stuck

Hint 1

Each check asks about the subgraph of links below its threshold, and those subgraphs only grow as the threshold grows. Could you answer the checks in some order other than the one given?

Hint 2

Process the checks in increasing order of threshold, adding each link once its latency falls below the current threshold, so the set of connections only ever grows. Sort both the links and the checks, and keep a pointer into the sorted links.

Hint 3

Use a disjoint-set union structure to join hosts as links are added, and answer each check by comparing the representatives of its two hosts. Remember the strict inequality, so a link whose latency equals the threshold is not yet added, and print the answers in the original order.

report an issue Prefer your own editor? sooks prep 115 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.