【原题】
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.
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 a i, b i (1 ≤ a i, b i ≤ n, a i ≠ b i) — 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 c i (1 ≤ c i ≤ 3), v i (1 ≤ v i ≤ n), where c i is the operation type (according to the numbering given in the statement), and v i is the vertex on which the operation is performed.
It is guaranteed that the given graph is a tree.
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.
1 #include <algorithm> 2 #include <cmath> 3 #include <cstdio> 4 #include <cstring> 5 #include <list> 6 #include <map> 7 #include <iostream> 8 #include <queue> 9 #include <set> 10 #include <stack> 11 #include <string> 12 #include <vector> 13 #include <iomanip> 14 #define LL long long 15 #define inf 0x3f3f3f3f 16 #define F first 17 #define S second 18 #define lson rt << 1 19 #define rson rt << 1 | 1 20 using namespace std; 21 22 const int maxn = 5e5 + 7; 23 int n, m, q; 24 25 int head[maxn]; 26 struct node 27 { 28 int fr, v, w, nxt; 29 }e[maxn * 2]; 30 31 int num = 0; 32 void add(int t1, int t2) 33 { 34 e[++num].fr = t1; 35 e[num].v = t2; 36 e[num].nxt = head[t1]; 37 head[t1] = num; 38 } 39 40 int son[maxn], sz[maxn], f[maxn], dep[maxn];//重儿子, 子结点个数, 父节点, 节点深度 41 //int dis[maxn];边权 42 void dfs1(int u, int fa) 43 { 44 sz[u] = 1; 45 for (int i = head[u]; i; i = e[i].nxt) 46 { 47 int v = e[i].v; 48 if (v == fa) continue; 49 dep[v] = dep[u] + 1; 50 //dis[v] = dis[u] + e[i].w; 51 f[v] = u; 52 dfs1(v, u); 53 sz[u] += sz[v]; 54 if (!son[u] || sz[v] > sz[son[u]]) son[u] = v; 55 } 56 } 57 int top[maxn], id[maxn]; 58 int a[maxn], w[maxn]; //节点所在链的顶端, 节点dfs序编号, dfs序编号下当前点的点值, 节点的点值 59 60 int cnt = 0; 61 void dfs2(int u, int t) 62 { 63 id[u] = ++cnt; 64 a[cnt] = w[u]; 65 top[u] = t; 66 if (son[u]) dfs2(son[u], t); 67 for (int i = head[u]; i; i = e[i].nxt) 68 { 69 int v = e[i].v; 70 if (v == f[u] || v == son[u]) continue; 71 dfs2(v, v); 72 } 73 } 74 75 int tree[maxn * 4]; 76 int lz[maxn * 4]; 77 78 void push_down(int node, int l, int r) { 79 if (lz[node] != -1) { 80 lz[node * 2] = lz[node]; 81 lz[node * 2 + 1] = lz[node]; 82 tree[node * 2] = tree[node]; 83 tree[node * 2 + 1] = tree[node]; 84 lz[node] = -1; 85 } 86 return; 87 } 88 89 void update_range(int node, int l, int r, int L, int R, int add) { 90 if (l <= L && r >= R) { 91 lz[node] = add; 92 tree[node] = add; 93 return; 94 } 95 push_down(node, L, R); 96 int mid = (L + R) / 2; 97 if (mid >= l) update_range(node * 2, l, r, L, mid, add); 98 if (mid < r) update_range(node * 2 + 1, l, r, mid + 1, R, add); 99 } 100 101 int query(int node, int L, int R, int l, int r) { 102 if (L == R) return tree[node]; 103 push_down(node, L, R); 104 int mid = (L + R) / 2; 105 if (mid >= l) return query(node * 2, L, mid, l, r) ; 106 else return query(node * 2 + 1, mid + 1, R, l, r) ; 107 } 108 void update_chain(int x, int y, int z) 109 { 110 int fx = top[x], fy = top[y]; 111 while (fx != fy) 112 { 113 if (dep[fx] < dep[fy]) 114 { 115 swap(x, y); 116 swap(fx, fy); 117 } 118 update_range(1, id[fx], id[x], 1, n, z); 119 x = f[fx], fx = top[x]; 120 } 121 if (id[x] > id[y]) swap(x, y); 122 update_range(1, id[x], id[y], 1, n, z); 123 } 124 125 int main() 126 { 127 ios::sync_with_stdio(false); 128 cin.tie(0); 129 cin >> n; 130 int ta, tb; 131 memset(lz, -1, sizeof(lz)); 132 for (int i = 1; i <= n - 1; i++) 133 { 134 cin >> ta >> tb; 135 add(ta, tb); 136 add(tb, ta); 137 } 138 f[1] = 1; 139 dfs1(1, 0); 140 dfs2(1, 1); 141 cin >> q; 142 int c, x; 143 for (int i = 1; i <= q; i++) 144 { 145 cin >> c >> x; 146 switch (c) 147 { 148 case 1: 149 { 150 update_range(1, id[x], id[x] + sz[x] - 1, 1, n, 1); 151 break; 152 } 153 case 2: 154 { 155 update_chain(1, x, 0); 156 break; 157 } 158 case 3: 159 { 160 cout << query(1, 1, n, id[x], id[x]) << endl; 161 } 162 } 163 } 164 }