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. InputThe 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.
用dfs序+线段树维护树上信息。
因为结点到根的路径不好维护,所以操作2直接单点修改,每个节点维护信息“子树中是否有0”,操作1先查询子树中是否有0,如果有就标记父节点为0,然后区间修改子树。
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int L[500010],R[500010],fa[500010],n,tot;
bool val[5000010],mk[5000010];
vector<int> to[500010];
void dfs(int u,int f)
{
int i,j,k,v;
L[u]=++tot;
if (f!=-1) fa[u]=f;
for (i=0;i<to[u].size();i++)
if (to[u][i]!=f)
dfs(to[u][i],u);
R[u]=tot;
}
void down(int p)
{
if (mk[p])
{
val[p]=1;
mk[p*2]=mk[p*2+1]=1;
mk[p]=0;
}
}
bool qry(int p,int LL,int RR,int l,int r)
{
down(p);
if (LL==l&&RR==r) return val[p];
int i,j,k,M=(LL+RR)/2;
if (r<=M) return qry(p*2,LL,M,l,r);
if (l>M) return qry(p*2+1,M+1,RR,l,r);
return qry(p*2,LL,M,l,M)*qry(p*2+1,M+1,RR,M+1,r);
}
void m_0(int p,int LL,int RR,int x)
{
down(p);
val[p]=0;
if (LL==RR) return;
int M=(LL+RR)/2;
if (x<=M) m_0(p*2,LL,M,x);
else m_0(p*2+1,M+1,RR,x);
}
void m_1(int p,int LL,int RR,int l,int r)
{
down(p);
if (LL==l&&RR==r)
{
mk[p]=1;
return;
}
int M=(LL+RR)/2;
if (r<=M) m_1(p*2,LL,M,l,r);
else
{
if (l>M) m_1(p*2+1,M+1,RR,l,r);
else
{
m_1(p*2,LL,M,l,M);
m_1(p*2+1,M+1,RR,M+1,r);
}
}
down(p*2);
down(p*2+1);
if (val[p*2]*val[p*2+1]) val[p]=1;
}
int main()
{
int i,j,k,m,p,q,x,y,z,u,v;
scanf("%d",&n);
for (i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
to[x].push_back(y);
to[y].push_back(x);
}
dfs(1,-1);
scanf("%d",&m);
while (m--)
{
scanf("%d%d",&x,&y);
if (x==1)
{
if (y!=1&&!qry(1,1,tot,L[y],R[y])) m_0(1,1,n,L[fa[y]]);
m_1(1,1,tot,L[y],R[y]);
}
if (x==2) m_0(1,1,tot,L[y]);
if (x==3) printf("%d\n",qry(1,1,tot,L[y],R[y]));
}
}