#P9235. Maximizing Communication Stability

    ID: 22390 Type: Default 1000ms 256MiB

Maximizing Communication Stability

Maximizing Communication Stability

In a local area network, there are \(n\) devices and \(m\) physical connections. The stability of the \(i\)-th connection is given as \(w_i\). For any path from device \(A\) to device \(B\) that passes through one or more physical connections, the stability of that path is defined as the minimum \(w_i\) among all connections on the path. The communication stability between devices \(A\) and \(B\) is defined as the maximum stability over all feasible paths between \(A\) and \(B\).

You are given the physical connection information of the network. Your task is to answer several queries. Each query gives a pair of devices \(x_i\) and \(y_i\); for each query, output the communication stability between the two devices. If there is no path between them, output \(-1\).

Note: All connections are bidirectional.

inputFormat

The first line contains two integers \(n\) and \(m\) (the number of devices and connections respectively).

The next \(m\) lines each contain three integers \(u\), \(v\) and \(w\) representing a bidirectional connection between devices \(u\) and \(v\) with stability \(w\).

The following line contains an integer \(q\), the number of queries.

Each of the next \(q\) lines contains two integers \(x_i\) and \(y_i\) representing a query between devices \(x_i\) and \(y_i\).

outputFormat

For each query, output a single integer representing the communication stability between the given pair of devices. If there is no path connecting them, output \(-1\).

sample

4 4
1 2 3
2 3 2
2 4 4
3 4 1
3
1 2
1 3
1 4
3

2 3

</p>