1. 程式人生 > >CoderForces343D:Water Tree(dfs序+線段樹&&特殊處理)

CoderForces343D:Water Tree(dfs序+線段樹&&特殊處理)

down operation ace sta 更改 lis contains AR pil

Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water.

The vertices of the tree are numbered from 1 to n with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of this vertex, and the vertex is connected with each of the children by a pipe through which water can flow downwards.

Mike wants to do the following operations with the tree:

  1. Fill vertex v with water. Then v and all its children are filled with water.
  2. Empty vertex v. Then v and all its ancestors are emptied.
  3. Determine whether vertex v is filled with water at the moment.

Initially all vertices of the tree are empty.

Mike has already compiled a full list of operations that he wants to perform in order. Before experimenting with the tree Mike decided to run the list through a simulation. Help Mike determine what results will he get after performing all the operations.

Input

The first line of the input contains an integer n

(1 ≤ n ≤ 500000) — the number of vertices in the tree. Each of the following n - 1 lines contains two space-separated numbers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the edges of the tree.

The next line contains a number q (1 ≤ q ≤ 500000) — the number of operations to perform. Each of the following q lines contains two space-separated numbers ci (1 ≤ ci ≤ 3), vi (1 ≤ vi ≤ n), where ci is the operation type (according to the numbering given in the statement), and vi is the vertex on which the operation is performed.

It is guaranteed that the given graph is a tree.

Output

For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input.

Example

Input
5
1 2
5 1
2 3
4 2
12
1 1
2 3
3 1
3 2
3 3
3 4
1 2
2 4
3 1
3 3
3 4
3 5
Output
0
0
0
1
0
1
0
1

題意:給定一棵樹,沒棵樹上有一個水池,現在有如下操作,

1,給V和其子樹裝水。

2,對V和其祖先排水。

3,問V是否有水。

思路:看到操作1,操作子樹,很自然地想到dfs序;然後但是2操作,V及其祖先在線段樹上的位置是離散的,所以需要暴力。

轉化一下思路:本來是多點更改2,單點查詢3; 改為:單點更新2,區間查詢3。

即:3操作改為,查詢V及其子樹是否都有水。

註意:對1操作,假設V的子樹有空(存在無水的點)的,則需要把V的父親改為空。 因為V的子樹有空,則V的父親為空,而且給V和子樹加水後V父親任然為空。但是由於沒有單點更改V的父親,而V的父親和其子樹都滿水,則導致誤判以為V的父親有水。 而V的父親最具有代表性,只改V的父親節點即可。

CoderForces343D:Water Tree(dfs序+線段樹&&特殊處理)