Ants
Time limit1sMemory limit512 MB
Each query moves every ant one step toward the current position of ant a_j, then reports how many pairs of ants share a vertex.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Implementation
- Solved
- No attempts yet
Problem
You are given a tree with vertices numbered from to . There are ants in the vertices of the tree. Initially, at each vertex , there is one ant having index .
You will be given queries. Each query contains an index of an ant that needs help. During query , each ant goes to an adjacent vertex that is closest to ant , or does not move if it is located at the same vertex as ant . After each query, print the total number of pairs of ants that are located at the same vertex.
The changes persist between queries. For example, when the ants have to move to ant , they are already in the positions reached after moving to ant . Each query requires moving to ant , which was at vertex initially but can be in some other vertex at the time of the query.
Input
The first line of input contains an integer , the size of the tree ().
Each of the next lines contains two integers and describing an edge of the tree (). It is guaranteed that the edges form a tree.
The next line contains an integer , the number of queries ().
The next lines contain integers , one per line: the numbers of ants in the queries ().
Output
For each query, print a single line with the answer to it: the number of pairs of ants that are located at the same vertex after this query.