Vertices connected by the same color
Time limit2sMemory limit512 MB
Process color flip and reachability queries on a tree where two vertices are connected if every vertex on their path has the same color.
- Level
Hard8 of 10
- Topics
- Tree, Union-find, DFS, Implementation
- Solved
- No attempts yet
Problem
You are given a tree with vertices. A tree is a connected undirected graph with no cycle. The vertices are numbered 1 to and the edges are numbered 1 to . Every vertex is black at the start.
Two vertices and are connected when every vertex on the path from to has the same color. The path from to is a single vertex, so is always connected to itself.
Write a program that processes the following two queries.
1 i: change the color of vertex . Black becomes white, white becomes black.2 u: count the vertices that are connected to . Count itself as well.
Input
The first line contains the number of vertices . ()
Each of the next lines contains the numbers and of the two vertices joined by an edge. (, ) The given edges form a tree.
The next line contains the number of queries . ()
Each of the next lines contains one query. A query is either 1 i or 2 u, with .
Output
For each query of type 2, print the answer on its own line, in the order the queries are given.