Christmas Tree Ornament
Time limit1sMemory limit1024 MB
A tree of N lamps is built incrementally and recolored M times; after each recolor, report how many edges join two lamps of equal color.
- Level
Medium4 of 10
- Topics
- Tree, Implementation
- Solved
- No attempts yet
Problem
Byteland Party Factory is preparing to launch a new Christmas-tree ornament. To build the prototype, two lamps were first connected to each other with a wire, and then, times, a new lamp was taken and connected with a wire to one of the already existing lamps. The result is an ornament made of colored lamps. The factory stocks different lamp colors.
Once the first prototype was ready, it was handed over to the decoration department. There it was decided that a good measure of the ornament's beauty is the number of wires that connect two lamps of the same color. The department then replaced one existing lamp with another times, and after each replacement they wanted to know the beauty of the resulting ornament.
Write a program that, given the ornament's initial prototype and the list of replacements made by the decoration department, computes the beauty of the ornament after each replacement.
Input
The first line of the input contains three integers: the number of lamps in the ornament (), the number of replacements made by the decoration department (), and the number of possible lamp colors ().
The second line contains integers (), giving the colors of the lamps in the order they were added to the ornament.
The third line contains integers (), where tells which lamp the lamp numbered was connected to.
Each of the following lines contains two integers and (, ), meaning that in the -th replacement the lamp was replaced by a lamp of color .
The lamps are numbered to in the order they were added; lamps and are the initial pair joined by a wire.
Output
Output exactly lines. On line , print the number of lamp pairs that are joined by a wire and have the same color in the configuration after the -th replacement.