War
Limits: 4 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Once upon a time in a galaxy far, far away, the peaceful country of 47-land was attacked by the hostile 66-land. 47-land has \(n\) cities. The cities of 47-land are connected by roads in such a way that you can get from any city to any other city using the existing roads in the country. Also, there is only one path between each pair of cities. Before the hostilities began, all cities were controlled by 47-land.
During the war, 47-land either lost control of some cities or recaptured them. In order to finally end this war, General Petro needs your help. The war has been going on for \(q\) days. It is known that on the \(i\)-th day of the war, city \(x_i\) came under the control of the other country. For example, if the city was controlled by 47-land, now it will be controlled by 66-land, and vice versa.
Help Petro by telling him the number of regions in 47-land on the \(i\)-th day, as this will help Petro devise a strategy to defeat the occupying country. Two cities \(a\) and \(b\) are considered to be in the same region if there is a path between them that passes through cities controlled by 47-land.
Input
The first line contains two integers \(n\) and \(q\) — the number of cities in 47-land and the number of days the war lasts, respectively.
The next \(n-1\) lines contain two integers \(u\) and \(v\) — the roads between the cities.
The next \(q\) lines contain a single integer \(x_i\) — the city where the controlling country changed on the \(i\)-th day of the war.
Output
Print the answer to the problem in \(q\) lines. In the \(i\)-th line, print a single number — the number of regions of 47-land after the \(i\)-th day of the war.
Constraints
\(1 \le n, q \le 10^{5}\),
\(1 \le u, v, x_i \le n\),
Problem evaluation consists of the following blocks:
1 point — example from the statement,
9 points — a block of tests in which \(1 \le n, q \le 10^{3}\),
15 points — a block of tests in which \(1 \le n, q \le 10^{5}\),
Points for a block will be awarded only if you give the correct answer to all tests in the block.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 3 1 2 1 3 3 4 4 5 3 6 6 7 3 6 7 | 3 3 2 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
War - Editorial
The first test group can be solved in the following ways:
For each day, count the number of regions made of cities controlled by 47-land. For this we iterate over the cities. If we meet a city controlled by 47-land that we have not visited yet, we traverse all cities controlled by 47-land reachable from it. Now all these cities are visited, and this region will not be counted again. This way we can compute the answer after each day. This algorithm takes \(O(q \cdot n)\).
For each city we keep \(cnt_v\), the number of its neighboring cities controlled by 47-land. When a city stops being controlled by 47-land, instead of one region we get \(cnt_v\) regions: each neighboring city ends up in its own region. Conversely, when a city becomes controlled by 47-land, \(cnt_v\) regions merge into one. When the state of a city changes, we update \(cnt\) for each neighboring city. This solution takes \(O(q \cdot n)\).
Let’s look at the country as a tree. Take one of the cities as the root; every vertex except the root has a parent. Now, instead of \(cnt_v\), we keep \(son_v\), the number of children of this vertex controlled by 47-land. To find \(cnt_v\), it is enough to check whether the parent of this vertex belongs to 47-land, and all the other neighboring cities are children of the current vertex, of which we know \(son_v\) belong to 47-land. If the parent belongs to 47-land, \(cnt_v = son_v + 1\); otherwise \(cnt_v = son_v\). Now when a vertex changes its state, we need to update \(son\) for its parent, if it exists. Each such operation takes \(O(1)\), and all queries take \(O(q)\) in total. We also need to compute the initial values \(son_v\), which can be done in \(O(n)\) with an ordinary depth-first search or with some formulas.
Solution code in C++ \(O(n \cdot q)\)
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|