Summer Training day6 codeforces343D dfs序、线段树

来源:互联网 发布:java session的用法 编辑:程序博客网 时间:2024/06/08 04:58

D. Water Tree
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 aibi (1 ≤ ai, bi ≤ nai ≠ 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.

Examples
input
51 25 12 34 2121 12 33 13 23 33 41 22 43 13 33 43 5
output
00010101
题解:

套路题,由于进行的是子树上的操作,因此把整棵树用dfs搜索得到其dfs序(长度为2n,因为要记录每个点出栈和入栈的时间),然后构建一颗线段树。

我们记线段树上每个点如果为0的话代表它到根节点的链上是空的。

对于操作1,就相当于把v的子树全部设置为1,在这之前判断一下子树中有没有为0的点,如果有的话把v的父亲设置为0.

对于操作2,把v设置为0

对于操作3,判断v的子树里面有没有0即可。

代码:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAXN = 1e6;int head[MAXN];int cnt;struct edge{     int v;     int next;     int cost; }Es[MAXN<<1];  void init(){     cnt = 0;     memset(head,-1,sizeof(head)); }inline void add_edge(int i,int j,int cost){       Es[cnt].v = j;     Es[cnt].cost = cost;     Es[cnt].next = head[i];     head[i] = cnt++; }   const int INF = 1e8;int a[MAXN];int segtree[MAXN*4];int addmark[MAXN*4];void pushdown(int root,int L,int R){if(addmark[root]){int mid = (L + R) / 2;addmark[root*2] = addmark[root] ;addmark[root*2+1] = addmark[root] ;segtree[root*2] = addmark[root] ;segtree[root*2+1] = addmark[root] ;addmark[root] = 0;}}void pushup(int root){segtree[root] = (segtree[root * 2] == 2 &&  segtree[root * 2 + 1] == 2) ? 2 : 1;}void build(int root,int begin,int end){addmark[root] = 0;//设置延迟标记域     if(begin == end)    {        segtree[root] = a[begin];    }    //递归得到    else    {        build(root*2,begin,(begin+end)/2);        build(root*2+1,(begin+end)/2+1,end);        pushup(root);    }}bool query(int root,int begin,int end,int left,int right){    /*begin,end为当前结点所存储的区间 left,right为要查询的区间*/    if(left > end||right < begin)        return 1;    /*如果完全包含节点区间,那么直接返回*/    if(begin>=left&&end<=right)        return segtree[root] == 2;    pushdown(root,begin,end);        /*不然有交叉的话*/bool a = query(root*2,begin,(begin+end)/2,left,right);bool b = query(root*2+1,(begin+end)/2+1,end,left,right);return a && b  ;}void update(int root,int begin,int end,int ubegin,int uend,int val){    if(ubegin > end || uend < begin)        return ;if(begin >= ubegin && end <= uend){addmark[root] = val;segtree[root] = val;return ;}pushdown(root,begin,end);int m = (begin+end)/2;update(root*2,begin,m,ubegin,uend,val);update(root*2+1,m+1,end,ubegin,uend,val);    pushup(root);}int n,q;int idx = 0;int IN[MAXN],OUT[MAXN],pa[MAXN];void dfs(int x,int fa){pa[x] = fa;IN[x] = ++idx;for(int e = head[x];e != -1;e = Es[e].next){int v = Es[e].v;if(v != fa){dfs(v,x);}}OUT[x] = ++idx;}int main(){for(int i = 0;i < MAXN;i++) a[i] = 1;init();scanf("%d",&n);build(1,1,2*n);for(int i = 0;i < n-1;i++){int a,b;scanf("%d%d",&a,&b);add_edge(a,b,1);add_edge(b,a,1);}dfs(1,-1);scanf("%d",&q);while(q--){int op,v;scanf("%d%d",&op,&v);if(op == 1){bool flag = query(1,1,2*n,IN[v],OUT[v]);if(!flag) {update(1,1,2*n,IN[pa[v]],IN[pa[v]],1);update(1,1,2*n,OUT[pa[v]],OUT[pa[v]],1);}update(1,1,2*n,IN[v],OUT[v],2);}else if(op == 2){update(1,1,2*n,IN[v],IN[v],1);update(1,1,2*n,OUT[v],OUT[v],1);}else if(op == 3){printf("%d\n",query(1,1,2*n,IN[v],OUT[v]) ? 1: 0);}}return 0;}