Water Tree

http://codeforces.com/problemset/problem/343/D

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

树链剖分模板题

 #include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#define maxn 500005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std; int tree[maxn<<],lazy[maxn<<];
int n;
int dep[maxn],fa[maxn],siz[maxn],son[maxn],id[maxn],top[maxn],cnt;
vector<int>ve[maxn]; void pushup(int rt){
if(tree[rt<<]||tree[rt<<|])
tree[rt]=;
else
tree[rt]=;
} void pushdown(int rt){
if(lazy[rt]!=-){
lazy[rt<<]=lazy[rt];
lazy[rt<<|]=lazy[rt];
tree[rt<<]=lazy[rt];
tree[rt<<|]=lazy[rt];
lazy[rt]=-;
}
} void build(int l,int r,int rt){
lazy[rt]=-;
if(l==r){
tree[rt]=;
return;
}
int mid=(l+r)/;
build(lson);
build(rson);
pushup(rt);
} void add(int L,int R,int k,int l,int r,int rt){
if(L<=l&&R>=r){
tree[rt]=k;
lazy[rt]=k;
return;
}
int mid=(l+r)/;
pushdown(rt);
if(L<=mid) add(L,R,k,lson);
if(R>mid) add(L,R,k,rson);
pushup(rt);
} int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return tree[rt];
}
int mid=(l+r)/;
pushdown(rt);
int ans=;
if(L<=mid) if(ans||query(L,R,lson)) ans=;
if(R>mid) if(ans||query(L,R,rson)) ans=;
pushup(rt);
return ans;
} void dfs1(int now,int f,int deep){
dep[now]=deep;
siz[now]=;
fa[now]=f;
int maxson=-;
for(int i=;i<ve[now].size();i++){
if(ve[now][i]==f) continue;
dfs1(ve[now][i],now,deep+);
siz[now]+=siz[ve[now][i]];
if(siz[ve[now][i]]>maxson){
maxson=siz[ve[now][i]];
son[now]=ve[now][i];
}
}
} void dfs2(int now,int topp){
id[now]=++cnt;
top[now]=topp;
if(!son[now]) return;
dfs2(son[now],topp);
for(int i=;i<ve[now].size();i++){
if(ve[now][i]==son[now]||ve[now][i]==fa[now]) continue;
dfs2(ve[now][i],ve[now][i]);
}
} void addRange(int x,int y,int k){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
add(id[top[x]],id[x],k,,n,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
add(id[x],id[y],k,,n,);
} void addSon(int x,int k){
add(id[x],id[x]+siz[x]-,k,,n,);
} int main(){
std::ios::sync_with_stdio(false);
cin>>n;
int m;
int pos,z,x,y;
for(int i=;i<n;i++){
cin>>x>>y;
ve[x].push_back(y);
ve[y].push_back(x);
}
cnt=;
dfs1(,,);
dfs2(,);
build(,n,);
cin>>m;
for(int i=;i<=m;i++){
cin>>pos>>x;
if(pos==){
addSon(x,);
}
else if(pos==){
addRange(,x,);
add(id[x],id[x],,,n,);
}
else if(pos==){
if(query(id[x],id[x],,n,)){
cout<<<<endl;
}
else{
cout<<<<endl;
}
}
} }

Water Tree(树链剖分+dfs时间戳)的更多相关文章

  1. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

  2. Codeforces Round #200 (Div. 1) D. Water Tree 树链剖分+线段树

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

  3. CodeForces 343D water tree(树链剖分)

    Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...

  4. CF343D Water Tree 树链剖分

    问题描述 LG-CF343D 题解 树剖,线段树维护0-1序列 yzhang:用珂朵莉树维护多好 \(\mathrm{Code}\) #include<bits/stdc++.h> usi ...

  5. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  6. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  7. 树链剖分||dfs序 各种题

    1.[bzoj4034][HAOI2015]T2 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把 ...

  8. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

  9. poj 3237 Tree 树链剖分

    题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...

随机推荐

  1. DeepFM模型理论及代码实现

    论文地址:DeepFM: A Factorization-Machine based Neural Network for CTR Prediction

  2. Hibernate JavaBean.hbm.xml配置

    主键生成策略: hibernate中必须设置主键 <generator> 由数据库维护: identity:用于自动生成主键方式(没有自增主键的数据库不使用eg:oracle) seque ...

  3. 小项目,吃货联盟,java初级小项目,源代码

    1:项目的实现效果.功能如图所示. 2:项目的源代码如下: import java.util.Scanner; /** * 吃货联盟订餐管理系统 * */ public class OrderingM ...

  4. 可怕的SCN!(转载)

    一.什么是SCNSCN(System Change Number 简称 SCN)是当Oracle数据库更新后,由DBMS自动维护去累积递增的一个数字.在Oracle中,有四种SCN,分别为:系统检查点 ...

  5. DOM事件机制(事件捕获和事件冒泡和事件委托)

    内容: 1.事件复习 2.事件冒泡与事件捕获 3.事件委托 1.事件复习 (1)事件 事件是用来处理响应的一个机制,这个响应可以来自于用户(点击, 鼠标移动, 滚动), 也可以来自于浏览器 下面的链接 ...

  6. UVA127

    模拟游戏,规则如下把卡牌一张接一张,从左到右依次摊开,不可以重叠,每当某张卡片和左边(左边第三张)卡片匹配,它就能放到另外一张卡片上,匹配的规则是他们有一样的级别或者花色,在每次移动完成以后,还需要再 ...

  7. shell $*与$@的区别

    [root@bgx shell]# cat tt.sh #! /bin/bash test() { echo "未加引号,二者相同" echo $* echo $@ echo &q ...

  8. JAVA SpringMVC + FormDate + Vue + file表单 ( 实现 js 单文件和多文件上传 )

    JS 部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...

  9. @Repository的作用

    一.@Repository 是用来注解接口的 如: @Repository("UserDao")public interface IUserDao {} 二,为什么有时候我们不用@ ...

  10. discuz 修改积分策略( 在周期中添加"每周" )

    在  source/admincp/admincp_credits.php 文件中, ctrl+f 搜索  $lang['setting_credits_policy_cycletype_1'] 处, ...