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:
Fill vertex v with water. Then v and all its children are filled with water.
Empty vertex v. Then v and all its ancestors are emptied.
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
思路:
这里又一次用到了dfs时间戳,将树形结构转化成区间;
对于将某一个节点的子树全部充满水,那就是用线段树维护区间子树的值;
对于将某一节点到根节点的水放掉,那就用到了树链剖分;
查询就是简单的线段树单点查询;
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 500090;
int n,op,v,q,idx,val[maxn<<2],tag[maxn<<2];
int sz[maxn],dep[maxn],id[maxn],fid[maxn],son[maxn],fa[maxn],tp[maxn];
vector<int> mp[maxn];
int in[maxn],out[maxn],clk;
void dfs1(int u,int f,int d)
{
sz[u]=1;
fa[u]=f;
dep[u]=d;
son[u]=0;
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(v==fa[u]) continue;
dfs1(v,u,d+1);
sz[u]+=sz[v];
if(son[u]==0||sz[son[u]]<sz[v])
son[u]=v;
}
}
void dfs2(int u,int p)
{
tp[u]=p;
id[u]=++idx;
in[u]=++clk;
fid[clk]=u;
if(son[u]) dfs2(son[u],p);
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(v==son[u]||v==fa[u]) continue;
dfs2(v,v);
}
out[u]=clk;
}
void pushdown(int rt)
{
if(tag[rt]>=0)
{
tag[rt<<1]=tag[rt<<1|1]=tag[rt];
val[rt<<1]=val[rt<<1|1]=tag[rt];
tag[rt]=-1;
}
}
void build(int l,int r,int rt)
{
tag[rt]=0;
val[rt]=0;
if(l==r) return;
int m=l+r>>1;
build(lson);
build(rson);
}
void update(int L,int R,int v,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
tag[rt]=v;
val[rt]=v;
return;
}
int m=l+r>>1;
pushdown(rt);
if(L<=m) update(L,R,v,lson);
if(m<R) update(L,R,v,rson);
}
int query(int p,int l,int r,int rt)
{
if(tag[rt]!=-1)
return tag[rt];
if(l==r) return val[rt];
int m=l+r>>1;
pushdown(rt);
if(p<=m) return query(p,lson);
else return query(p,rson);
}
void qq(int u)
{
int t=tp[u];
while(t!=1)
{
update(id[t],id[u],0,1,idx,1);
u=fa[t];
t=tp[u];
}
update(id[t],id[u],0,1,idx,1);
}
int main()
{
while(~scanf("%d",&n))
{
idx=clk=0;
for(int i=1;i<=n;i++)
mp[i].clear();
int u,v;
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
mp[u].push_back(v);
mp[v].push_back(u);
}
dfs1(1,0,1);
dfs2(1,1);
build(1,idx,1);
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&op,&v);
if(op==1)
update(id[v],id[fid[out[v]]],1,1,idx,1);
else if(op==2)
qq(v);
else printf("%d\n",query(id[v],1,idx,1));
}
}
return 0;
}