DMOPC '20 Contest 7 P4 - Mimi and Carrots II

View as PDF

Submit solution


Points: 17 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

Everything changed. At that terrible moment. We knew that home was a tree with N nodes, and carrots were food.

The Land of the Carrot Trees was once a peaceful land, with N cities connected with N1 roads. This all changed one day, when Mimi the carrot-loving cat invaded, and forced the Carrot King into exile!

The Carrot King is constantly on the run. Every day, for the next Q days, one of two types of events happens:

  1. His spies tell him that city uj has either become a safe haven where he can hide, or if it was safe, that it has been completely overrun. Initially, no city is safe.
  2. He asks how safe it is to travel from city uj to city vj. This value is equal to the number of distinct safe cities cj such that cj is either on the path from uj to vj, or there exists a city dj such that dj is on the path from uj to vj, and there exists an edge between cj and dj.

As the royal adviser, it is up to you to implement a program that answers the king's queries. Can you save the king?

Constraints

1N,Q2×105

Subtask 1 [10%]

1N,Q2×103

Subtask 2 [90%]

No additional constraints.

Input Specification

The first line of input contains 2 space-separated integers N and Q.

The next N1 lines each contain 2 space-separated integers ai and bi, indicating there is an edge from ai to bi.

The next Q lines first contain an integer tj{1,2}, indicating the type of the event:

  • If tj=1, then a single integer uj (1ujN) follows.
  • If tj=2, then 2 integers uj and vj (1uj,vjN) follow.

Output Specification

For each type 2 event, output the answer on a new line.

Sample Input

Copy
6 5
1 2
1 3
2 4
4 6
2 5
1 4
2 2 3
2 4 2
1 4
2 5 3

Sample Output

Copy
1
1
0

Comments

There are no comments at the moment.