Antarctic Exploration
Time limit30sMemory limit512 MB
Maintain a dynamic forest under link operations and point updates, answering whether two islands are connected and the total penguins on the path between them.
- Level
Medium7 of 10
- Topics
- Union-find, Tree, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
Sanggeun owns the travel agency "Dreaming of Ice." The agency has bought islands near Antarctica and runs day trips there. The most popular animal among tourists is the emperor penguin, and it is easy to find on the islands.
The agency has grown more popular, to the point where moving tourists by boat is no longer efficient. Sanggeun plans to build bridges between the islands and move tourists by bus. He will manage the bridge-building process with a computer program.
The islands are numbered through . At first there are no bridges, and the number of penguins living on each island is known. The number of penguins can change, but it is always between and inclusive.
Sanggeun's program must handle three commands.
bridge A B: build a bridge between islands and . ( and are different.) The bridge must be built only when and cannot be reached from each other using the bridges built so far. Printyesif the bridge must be built, andnoif it is already possible to travel between them and no bridge is needed.penguins A X: a recount shows that island now has penguins. Print nothing.excursion A B: tourists take a route that starts at island and ends at island . If can be reached from , compute and print the total number of penguins on all islands along the route. (Include and .) If the route is impossible, printimpossible.
Write Sanggeun's program.
No further command is given before the answer to a bridge or excursion command is printed. Therefore, the standard output buffer must be flushed after printing.
Input
The first line gives the number of islands ().
The second line gives the number of penguins on each island.
The third line gives the number of commands ().
The next lines each contain one of the commands described in the problem.
Output
Print a line each time a bridge or excursion command is given.