Province Capture
Time limit2sMemory limit1024 MB
Given a tree where provinces are toggled as captured, answer each path query with the number of path nodes inside the minimal subtree spanning all captured nodes.
- Level
Hard8 of 10
- Topics
- Tree, Segment tree
- Solved
- No attempts yet
Statement
Deadpool has decided to get back at the Canadian government. He cannot manage it alone, so he will carry out his plan together with the inimitable Copycat.
Deadpool has decided to capture all of Canada. Then he will let it go, just for fun. Canada is divided into several administrative units, which we will simply call provinces. Some provinces are connected by two-way roads. Canada has provinces and roads in total. There is exactly one simple path between any two provinces, so Canada is a tree.
As the plan unfolds, Deadpool captures some provinces and releases others. After each action, the provinces under his control are those that belong to the minimal connected subgraph containing all captured provinces. Every captured province is under his control, but not every province under his control is captured.
At certain moments, emergency medical trips take place from one province to another. A trip is more dangerous the more provinces under Deadpool's control it passes through.
For each trip, determine how many provinces under Deadpool's control lie on its path. Initially, all provinces are free. If one or both endpoints of a trip are under Deadpool's control, they are counted too.
Input
The first line contains a natural number (), the number of provinces. Each of the next lines contains two natural numbers , (), the provinces connected by a two-way road. The given graph is guaranteed to be a tree.
The next line contains a natural number (), the number of events. Each of the next lines describes an event in chronological order. First comes a natural number (), the type of the query.
If , one natural number () follows, the province on which Deadpool acts. He captures it if it was free before, and releases it if it was captured before.
If , two natural numbers , () follow, the provinces between which the trip is made.
Output
For each trip, print how many provinces under Deadpool's control lie on its path.