Holiday decorations

5

3 votes
Datastructures, Implementation, Graph, Queues, Data Structures, Basics of Queues, Structured data, Basics of Implementation
Problem

Recently, the first model of decoration was assembled from multi-colored glowing light bulbs at the holiday jewelry factory. The prototype of the decoration was assembled as follows:

  • First, two light bulbs were taken and connected with a wire.
  • Then, a light bulb was taken for  times and it was connected with a wire to one of the previously added to the garland light bulbs.

The result was a decoration of colored light bulbs. The factory has bulbs of different colors. When the prototype was ready, it was handed over to the Beauty Department. In this department, it was decided to consider the beauty of jewelry the number of pairs of light bulbs of the same color, connected by a wire.
Employees of the Beauty Department repaint some of the light bulbs in one of the  colors for some reason known only to them. All they need to produce the perfect jewelry is to quickly determine the beauty of the product, after repainting the next light bulb.
The staff of the beauty department asks you, the best programmer, to write a program that will determine the beauty of jewelry according to the given prototype of jewelry and the sequence of repainting light bulbs.

Input format

  • The first line contains three integers , ,  denoting the number of bulbs in the product prototype, the number of repaints, and the number of different colors of bulbs available at the factory.
  • The second line contains positive integers  denoting the colors of the bulbs in the order of addition to the product.
  • The third line contains positive integers denoting the number of the light bulb to which the light bulb was connected.
  • The next lines contain two integers and  denoting the number of the repainted lamp and the color in which it is repainted, respectively. The numbers in the lines are separated by single spaces.

Output format

Print lines. The  line should contain a single integer denoting the number of pairs of light bulbs of the same color, connected by a wire, after execution and repainting.

Constraints

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first case, all bulbs are connected in a chain (first-second-third) and have different colors. Then they are repainted in the same color. First, a pair of light bulbs 1 2 becomes a single color (the second light bulb is repainted in color 1), and then 2 3 (the third light bulb is repainted in color 1). After repainting the second bulb in the color of 2 single-color pairs of bulbs connected by a wire, does not remain.

Editor Image

?