time limit per test 4 seconds
memory limit per test 256 megabytes
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:
Input
The first line of the input contains an integer n(1 ≤ n ≤ 500000) n ( 1 ≤ n ≤ 500000 ) — the number of vertices in the tree. Each of the following n−1 n − 1 lines contains two space-separated numbers ai,bi(1 ≤ ai, bi ≤ n,ai ≠ bi) 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) q ( 1 ≤ q ≤ 500000 ) — the number of operations to perform. Each of the following q q lines contains two space-separated numbers ci(1 ≤ ci ≤ 3),vi(1 ≤ vi ≤ n) c i ( 1 ≤ c i ≤ 3 ) , v i ( 1 ≤ v i ≤ n ) , where ci c i 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
要维护某棵子树和某结点到root的链,就是典型的线段树维护树剖。
在dfs序上跑线段树,期望时间复杂度 O(qlogn) O ( q l o g n ) 。
#include
#define N 500010
using namespace std;
struct Node{
int to, nxt;
}e[N << 1];
int cnt, tot, anc[N], len[N], dad[N], size[N], son[N], lst[N], df[N], si[N << 2];
inline void add(int u, int v) {
e[++cnt].to = v;
e[cnt].nxt = lst[u];
lst[u] = cnt;
}
void dfs1(int x, int fa) {
size[x] = 1;
dad[x] = fa;
for (int i = lst[x]; i; i = e[i].nxt) {
if (e[i].to == fa) continue;
dfs1(e[i].to, x);
size[x] += size[e[i].to];
if (size[e[i].to] > size[son[x]])
son[x] = e[i].to;
}
}
void dfs2(int x, int fa) {
df[x] = ++tot;
if (son[x]) {
anc[son[x]] = anc[x];
len[son[x]] = len[x] + 1;
dfs2(son[x], x);
for (int i = lst[x]; i; i = e[i].nxt) {
if (e[i].to == fa || e[i].to == son[x]) continue;
anc[e[i].to] = e[i].to;
len[e[i].to] = 1;
dfs2(e[i].to, x);
}
}
}
void pushdown(int rt) {
int ls = rt << 1, rs = rt << 1 | 1;
si[ls] = si[rt];
si[rs] = si[rt];
si[rt] = -1;
}
void update(int ll, int rr, int l, int r, int id, int va) {
if (ll <= l && r <= rr) {
si[id] = va;
return;
}
int mid = (l + r) >> 1, ls = id << 1, rs = id << 1 | 1;
if (si[id] != -1) pushdown(id);
if (mid >= ll) update(ll, rr, l, mid, ls, va);
if (mid + 1 <= rr) update(ll, rr, mid + 1, r, rs, va);
if (si[ls] == si[rs] && si[ls] != -1) si[id] = si[ls];
else si[id] = -1;
}
int query(int po, int l, int r, int id) {
if (si[id] != -1) return si[id];
int mid = (l + r) >> 1;
if (si[id] != -1) pushdown(id);
if (l <= po && po <= mid) return query(po, l, mid, id << 1);
else return query(po, mid + 1, r, id << 1 | 1);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs1(1, 0);
anc[1] = 1;
len[1] = 1;
dfs2(1, 0);
int q;
scanf("%d", &q);
for (int i = 1; i <= q; ++i) {
int c, v;
scanf("%d%d", &c, &v);
if (c == 1) {
update(df[v], df[v] + size[v] - 1, 1, n, 1, 1);
}
if (c == 2) {
for (int now = v; now; now = dad[anc[now]]) {
update(df[anc[now]], df[anc[now]] + len[now] - 1, 1, n, 1, 0);
}
}
if (c == 3) {
printf("%d\n", query(df[v], 1, n, 1));
}
}
return 0;
}