#P7654. Buckets and Valves Simulation
Buckets and Valves Simulation
Buckets and Valves Simulation
There are \(N\) identical buckets placed on a horizontal plane, each with a capacity of \(100\) units. Every pair of buckets is connected by a pipe that has a valve at its bottom. Each valve has two states: open or closed. Initially, all valves are closed. When a valve is open, the liquid between the two connected buckets instantly equalizes (by the principle of communicating vessels). When closed, no liquid flows through the pipe.
You are given a sequence of operations. There are two types of operations:
- P n m: Pour \(m\) units of liquid into bucket number \(n\). The liquid will immediately equalize among all buckets that are connected by open valves to bucket \(n\).
- V n1 n2: Toggle the valve on the pipe connecting bucket \(n_{1}\) and bucket \(n_{2}\) (i.e. if it is closed it becomes open, and vice versa). When a valve is toggled from closed to open, if it connects two different connected groups (which may have different liquid levels), then the liquid in the merged group instantly equalizes among all buckets in the group.
Note that you should ignore any liquid that may be in the pipes. If any pour operation would cause the liquid level in any bucket (after equalization) to exceed \(100\) units, then you must output OVERFLOW and stop processing further operations.
inputFormat
The input begins with a line containing two integers \(N\) and \(M\) (the number of buckets and the number of operations, respectively). The following \(M\) lines each describe an operation in one of the following formats:
- P n m: Pour \(m\) units of liquid into bucket number \(n\).
- V n1 n2: Toggle the valve between buckets \(n_{1}\) and \(n_{2}\). Note that "V n1 n2" and "V n2 n1" refer to the same valve.
Initially, all buckets are empty and each bucket can hold at most 100 units. The liquid instantly equalizes among all buckets in the connected component (via open valves) after every operation (pouring or opening a valve). When a valve is closed, the connection is removed; if the bucket group splits, the buckets retain their current liquid amounts.
outputFormat
If all operations are executed without any bucket exceeding 100 units, output a single line containing the final liquid amount in each bucket (from bucket 1 to bucket N), each formatted to six decimal places and separated by a space. If a pouring operation would cause any bucket to exceed 100 units, immediately output OVERFLOW (without quotes) and terminate.
sample
3 4
P 1 50
V 1 2
P 2 100
V 1 2
75.000000 75.000000 0.000000