Given a connected undirected graph with mutable node values, answer queries asking the minimum possible difference between the values of two walkers' end cities.
Hard8GraphBFSBinary searchSortingNo attempts yetTime limit2sMemory limit512 MBThere are N cities numbered 1 to N, connected by M bidirectional roads. Every city is reachable from every other city. City i has economic value Si.
There are Q queries to process in order. Each query consists of three integers Ai, Bi and Ci.
If Ai=0, change the economic value of city Bi to Ci.
If Ai=1, assume two businessmen start in city Bi and city Ci. They agree on a nonnegative integer X. On each of the next X days, each businessman moves to a city adjacent to the one he is in. Staying in place is not allowed, but revisiting cities is allowed. After X days, consider the absolute difference of the economic values of the two cities they occupy, and minimize it over all choices of X and all walks. Output that minimum. The two businessmen may finish in the same city. Each query chooses its own X independently.
The first line contains N and M (1≤N≤100000, 1≤M≤200000). The second line contains the economic values S1,S2,…,SN (0≤Si≤1000000000). Each of the next M lines contains two cities ui and vi joined by a road (1≤ui,vi≤N, ui=vi). Every city is reachable from every other city. The next line contains Q (1≤Q≤100000). Each of the next Q lines contains one query Ai, Bi and Ci (0≤Ai≤1). If Ai=0 then 1≤Bi≤N and 0≤Ci≤1000000000. If Ai=1 then 1≤Bi,Ci≤N. At least one query has Ai=1.
For each query with Ai=1, print the minimum achievable difference of economic values on its own line. The value of X is independent between queries.
Study how the set of reachable cities changes as X grows. Moving to a neighbor and back extends a walk by 2, so reachability depends on the parity of X. If the graph has an odd cycle, the two businessmen can always meet in one city. If the graph is bipartite, each businessman stays on the side fixed by the start city and the parity of X. Two starts on the same side always give 0. Two starts on opposite sides give the closest pair of economic values across the two sides.