Under construction

Still being built. Things may change

Sister Cities

Time limit2sMemory limit512 MB

Summary
For each city pair, find the smallest fuel capacity that lets two cars drive to each other's city on a connected road graph without ever meeting.
Level

Hard8 of 10

Topics
Graph, Minimum spanning tree, Union-find
Solved
No attempts yet

Problem

Indonesia has NN cities numbered from 0 to N−1N-1, and MM two-way roads numbered from 0 to M−1M-1. Each road connects two different cities. Road ii connects city UiU_i and city ViV_i, and a car needs WiW_i units of gasoline to travel along it. Every pair of cities is connected by a series of roads.

For the next QQ days, one pair of cities will try to become sister cities each day. On day jj, city XjX_j tries to become a sister city of city YjY_j. To do this, city XjX_j sends a delegation by car to city YjY_j, and city YjY_j sends a delegation by car to city XjX_j.

To prevent congestion, two cars must never meet. Specifically, two cars must never be in the same city at the same time. Also, two cars must not travel the same road in opposite directions at the same time. In addition, a car that enters a road must drive to the other end and reach the destination. In other words, a car cannot turn around in the middle of a road. However, a car may visit the same city or the same road more than once. A car may also wait in any city at any time.

A large fuel tank makes a car expensive, so the two cities want routes that minimize the maximum fuel tank capacity of the two cars. Every city has a gas station with unlimited gasoline. Therefore, the fuel tank capacity a car needs is the largest amount of gasoline required on any road the car uses.

Constraints

  • 2≤N≤1000002 \le N \le 100000.
  • N−1≤M≤200000N-1 \le M \le 200000.
  • 0≤Ui<Vi<N0 \le U_i < V_i < N.
  • At most one road connects any two cities.
  • Any two cities can reach each other by one or more roads.
  • 1≤Wi≤1091 \le W_i \le 10^9.
  • 1≤Q≤2000001 \le Q \le 200000.
  • 0≤Xj<Yj<N0 \le X_j < Y_j < N.

Examples1

  1. Example 1

    Input
    2 1
    0 1 5
    1
    0 1
    
    Expected output
    -1