Coloring Roads
Time limit4sMemory limit1024 MB
Color every edge on a root path with a given color and then count colors used on exactly m edges, answering Q updates online.
- Level
Hard8 of 10
- Topics
- Tree, Segment tree, Hash map, Implementation
- Solved
- No attempts yet
Problem
In RUN-land there are cities numbered to . Some pairs of cities are connected by a bidirectional road. There are roads in total, and for any two cities there is a unique path from one to the other.
City is the capital. Initially no road has a color. Alex, the king of RUN-land, asks you to perform the following query times.
- : Given a city , a color , and an integer , color every road on the unique path from to the capital in color . If a road already has a color, change its color to . After coloring, compute the number of colors in which exactly roads are colored.
Given queries in total, compute the answer to the second part of each query.
Input
The first line of the input contains three integers (), separated by a single space: the number of cities in RUN-land, the number of possible colors, and the number of queries.
Each of the next lines contains two integers (), meaning that a bidirectional road directly connects the cities numbered and .
Each of the next lines contains a query, which consists of integers as described in the statement. (, , )
Output
Print lines, one for each query. Each line must contain one integer, the answer to the corresponding query.