Tree Colors and Queries 2
Time limit2sMemory limit256 MB
Given a rooted tree with colored vertices, cut parent edges online and count the distinct colors among the vertices still connected to each queried vertex.
- Level
Hard8 of 10
- Topics
- Tree, Union-find, Segment tree
- Solved
- No attempts yet
Problem
A tree with vertices is given. The vertices are numbered 1 to , and each vertex has a color, which is a natural number from 1 to 100,000. Vertex 1 is the root. Since the graph is a tree, there is exactly one path between any two distinct vertices.
If a path between and exists, you can go from to .
You must process two kinds of queries.
- 1 a: Remove the edge connecting vertex and its parent. This query is only given when that edge exists.
- 2 a: Looking only at the vertices you can reach from vertex , print the number of distinct colors.
Input
The first line contains () and (), the number of vertices and the number of type 2 queries. The second line has integers. The -th integer is the parent of vertex . The third line has integers. The -th integer is the color of vertex . The next lines contain the queries: of type 1 and of type 2. Each query is a line with its type () and a non-negative integer . The vertex to process is , and . is bitwise XOR. is the result of the most recent type 2 query, and its initial value is 0.
Output
For each of the type 2 queries, print the answer on its own line, in order.