#P6231. Dynamic Bus Network
Dynamic Bus Network
Dynamic Bus Network
Nanjing has n bus stops numbered from 1 to n. There may be a direct bus between any two different stops x and y (the bus goes both ways). Such a bus line is represented as an undirected edge.
The network is maintained with the invariant that at any moment every bus stop is connected to at most 2 bus lines, and these edges never form a cycle. In other words, the bus routes always form a set of disjoint chains (each chain has two terminal stops).
You will receive q interaction messages in time order. Each message is one of the following five operations:
add x y z
: At the current moment, add a direct bus between stops x and y with operating time z. This add operation is processed only if after adding the edge, every stop’s degree does not exceed 2 and no cycle is formed. Otherwise, ignore the message.del x y
: Delete the currently operating direct bus between stops x and y, if it exists. Otherwise, ignore the message.change x y z
: Change the current operating time of the bus between stops x and y to z, if such a bus exists. Otherwise, ignore the message.reach x y
: A user queries whether it is possible to reach stop y starting from stop x using the currently operating bus lines. OutputYes
if reachable; otherwise, outputNo
.dest x y
: A user boards a bus at stop x that is going directly to stop y. After arriving at y, the user will continue their journey by taking available bus lines (each bus line may only be taken once) until reaching a terminal stop in the chain. The query asks for two things: which terminal stop is reached and the total time taken from stop x to that terminal stop. Note that if stop x cannot reach stop y, the query is considered erroneous and you should outputError
(without quotes).
Initially, before any message, no bus lines are operating.
Note: For update operations (add
, del
, change
), if the message is invalid as per the rules described above, simply ignore it and do not update the network. The query operations (reach
and dest
) must output a result as specified.
Details for dest
query: Assume the user has already taken the bus from x to y (and the travel time on that road is added). Then, starting at stop y, while there exists an adjacent bus line that has not been taken yet (the bus line used from x to y cannot be re-used), the user will continue taking that bus. Since the network forms chains, this process is unique. Output the terminal stop reached and the total accumulated travel time from x to that terminal stop, separated by a space.
inputFormat
The first line contains two integers n and q — the number of bus stops and the number of operations.
Each of the next q lines contains one operation, which is one of the following formats:
add x y z
del x y
change x y z
reach x y
dest x y
outputFormat
For each reach
query, output a single line containing either Yes
or No
.
For each dest
query, if stop x can reach stop y, output a line with two values: the terminal stop reached and the total travel time from x to that stop, separated by a space. If x cannot reach y, output Error
instead.
sample
5 6
add 1 2 3
add 2 3 4
reach 1 3
dest 1 2
change 2 3 10
dest 1 2
Yes
3 7
3 13
</p>