Fellow me on GitHub

Codeforces343D(SummerTrainingDay06-F dfs序+线段树)

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

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

这颗树具有一个重要的特性,当一个点是 0 的时候,这个点的全部祖先一定都是 0;一点是 1,这个点的全部子孙都是 1。

利用这个性质,如果我们要把一个节点以及他的祖先都变成 0,我们只要把这个点标记成 0 就可以了——因为这样就包含了所有的操作信息。这种观点下,再看这三种操作。

  1. u 点以及 u 点的所有子孙都赋值为 1:这个时候应该先看一看子孙有没有 0,如果有 0,那么说明 u 的祖先都是应该是 0 但是还没表现出来。所以我们把 u 的父亲标记成 0。这之后,我们再把所有的子孙赋值成 1。
  2. u 点以及 u 点的所有祖先都赋值为 0:u 标记成 0;
  3. 查询一个点 u 的值:u 以及 u 的所以子孙的标记都不是 0,u 才真正的是 1。

这样一看,好开心,这三种操作都变成了树上的单点修改或者是一整个子树的整体修改。这个解决起来就很套路了,我们把树按照 dfs 序展开,就变成了线段树连续区间修改的问题。

  1 //2017-09-01
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 #define lson (id<<1)
  7 #define rson ((id<<1)|1)
  8 
  9 using namespace std;
 10 
 11 const int N = 510000;
 12 
 13 int head[N], tot;
 14 struct Edge{
 15     int to, next;
 16 }edge[N<<2];
 17 
 18 void init(){
 19     tot = 0;
 20     memset(head, -1, sizeof(head));
 21 }
 22 
 23 void add_edge(int u, int v){
 24     edge[tot].to = v;
 25     edge[tot].next = head[u];
 26     head[u] = tot++;
 27 }
 28 
 29 int in[N], out[N], fa[N], cnt;
 30 void dfs(int u, int father){
 31     in[u] = ++cnt;
 32     for(int i = head[u]; i != -1; i = edge[i].next){
 33         int v = edge[i].to;
 34         if(v != father){
 35             fa[v] = u;
 36             dfs(v, u);
 37         }
 38     }
 39     out[u] = cnt;
 40 }
 41 
 42 bool flag;
 43 struct Node{
 44     int l, r, value, lazy;
 45 }tree[N<<2];
 46 
 47 void build(int id, int l, int r){
 48     tree[id].l = l;
 49     tree[id].r = r;
 50     tree[id].value = 0;
 51     tree[id].lazy = 0;
 52     if(l == r)return;
 53     int mid = (tree[id].l+tree[id].r)>>1;
 54     build(lson, l, mid);
 55     build(rson, mid+1, r);
 56 }
 57 
 58 void push_up(int id){
 59     if(tree[lson].value && tree[rson].value)tree[id].value = 1;
 60     else tree[id].value = 0;
 61 }
 62 
 63 void push_down(int id){
 64     if(tree[id].lazy){
 65         tree[lson].value = tree[rson].value = tree[id].lazy;
 66         tree[lson].lazy = tree[rson].lazy = tree[id].lazy;
 67         tree[id].lazy = 0;
 68     }
 69 }
 70 
 71 void update(int id, int l, int r, int op){
 72     if(l == 0)return;
 73     if(l <= tree[id].l && tree[id].r <= r){
 74         if(tree[id].value == 0)flag = 0;
 75         tree[id].value = op;
 76         if(op == 1)tree[id].lazy = op;
 77         return;
 78     }
 79     push_down(id);
 80     int mid = (tree[id].l+tree[id].r)>>1;
 81     if(l <= mid)update(lson, l, r, op);
 82     if(r > mid)update(rson, l, r, op);
 83     push_up(id);
 84 }
 85 
 86 void query(int id, int l, int r){
 87     if(l <= tree[id].l && tree[id].r <= r){
 88         if(tree[id].value == 0)flag = 0;
 89         return;
 90     }
 91     push_down(id);
 92     int mid = (tree[id].l+tree[id].r)>>1;
 93     if(l <= mid)query(lson, l, r);
 94     if(r > mid)query(rson, l, r);
 95 }
 96 
 97 int n, q;
 98 
 99 int main()
100 {
101     //freopen("inputF.txt", "r", stdin);
102     while(scanf("%d", &n) != EOF){
103         init();
104         int u, v;
105         for(int i = 0; i < n-1; i++){
106             scanf("%d%d", &u, &v);
107             add_edge(u, v);
108             add_edge(v, u);
109         }
110         fa[1] = 0;
111         cnt = 0;
112         dfs(1, 0);
113         build(1, 1, n);
114         scanf("%d", &q);
115         while(q--){
116             scanf("%d%d", &u, &v);
117             if(u == 1){
118                 flag = 1;
119                 update(1, in[v], out[v], 1);
120                 if(!flag)update(1, in[fa[v]], in[fa[v]], 0);
121             }else if(u == 2){
122                 update(1, in[v], in[v], 0);
123             }else{
124                 flag = 1;
125                 query(1, in[v], out[v]);
126                 if(flag)printf("1\n");
127                 else printf("0\n");
128             }
129         }
130     }
131 
132     return 0;
133 }

 

posted @ 2017-09-01 22:32  Penn000  阅读(188)  评论(0编辑  收藏  举报