Travelling Businessmen Problem
Time limit2sMemory limit512 MB
Given a connected undirected graph with mutable node values, answer queries asking the minimum possible difference between the values of two walkers' end cities.
- Level
Hard8 of 10
- Topics
- Graph, BFS, Binary search, Sorting
- Solved
- No attempts yet
Problem
There are cities numbered 1 to , connected by bidirectional roads. Every city is reachable from every other city. City has economic value .
There are queries to process in order. Each query consists of three integers , and .
If , change the economic value of city to .
If , assume two businessmen start in city and city . They agree on a nonnegative integer . On each of the next 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 days, consider the absolute difference of the economic values of the two cities they occupy, and minimize it over all choices of and all walks. Output that minimum. The two businessmen may finish in the same city. Each query chooses its own independently.
Input
The first line contains and (, ). The second line contains the economic values (). Each of the next lines contains two cities and joined by a road (, ). Every city is reachable from every other city. The next line contains (). Each of the next lines contains one query , and (). If then and . If then . At least one query has .
Output
For each query with , print the minimum achievable difference of economic values on its own line. The value of is independent between queries.
Hint
Study how the set of reachable cities changes as grows. Moving to a neighbor and back extends a walk by 2, so reachability depends on the parity of . 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 . 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.