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

Sample test(s)
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 <bits/stdc++.h>
using namespace std;
const int mx = 5e5 + 5; vector<int> g[mx];
set<int> Empty;
int L[mx], R[mx], fa[mx];
int timer, to; void dfs(int v, int p = -1)
{
L[v] = ++timer;
for (int i = 0; i < g[v].size(); ++i)
if ((to = g[v][i]) != p)
fa[to] = v, dfs(to, v);
R[v] = timer;
if (R[v] == L[v]) Empty.insert(L[v]);
} bool empty(int v)
{
set<int>::iterator it = Empty.lower_bound(L[v]);
return it != Empty.end() && *it <= R[v];
} void display()
{
set<int>::const_iterator p;
for (p = Empty.begin(); p != Empty.end(); ++p)
cout << *p << " ";
cout << "\n";
} int main()
{
int n, q, a, b, op, v;
scanf("%d", &n);
for(int i=1;i<n;i++)
{
scanf("%d%d", &a, &b);
g[a].push_back(b), g[b].push_back(a);
} //存储树
dfs(1);
// display();
scanf("%d", &q);
while (q--)
{
scanf("%d%d", &op, &v);
if (op == 1)
{
if (fa[v] && empty(fa[v])) Empty.insert(L[fa[v]]);
Empty.erase(Empty.lower_bound(L[v]), Empty.upper_bound(R[v]));
}
else if (op == 2) Empty.insert(L[v]);
else puts(empty(v) ? "0" : "1");
//display();
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏的更多相关文章

  1. MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏

    我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...

  2. A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏

    A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...

  3. nginx 安装手记 分类: Nginx 服务器搭建 2015-07-14 14:28 15人阅读 评论(0) 收藏

    Nginx需要依赖下面3个包 gzip 模块需要 zlib 库 ( 下载: http://www.zlib.net/ ) zlib-1.2.8.tar.gz rewrite 模块需要 pcre 库 ( ...

  4. RedHat Enterprise Linux 6.4使用Centos 6 的yum源 分类: 服务器搭建 Nginx 2015-07-14 14:11 5人阅读 评论(0) 收藏

    转载自:http://blog.sina.com.cn/s/blog_50f908410101cto6.html 思路:卸载redhat自带yum,然后下载centos的yum,安装后修改配置文件 1 ...

  5. 使用Broadcast实现android组件之间的通信 分类: android 学习笔记 2015-07-09 14:16 110人阅读 评论(0) 收藏

    android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...

  6. 使用JavaScriptSerializer序列化集合、字典、数组、DataTable为JSON字符串 分类: 前端 数据格式 JSON 2014-10-30 14:08 169人阅读 评论(0) 收藏

    一.JSON简介 JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式. JSON是"名值对"的集合.结构由大 ...

  7. vs2008 多人同时开发项目时的代码注释规范格式 分类: C#小技巧 2014-04-23 14:12 297人阅读 评论(0) 收藏

    多人同时开发一个项目,区分项目的那个窗体是谁开发的,例:下面的格式 /************************************************       模块:服务器设置   ...

  8. 代码对齐 分类: C#小技巧 2014-04-17 14:45 166人阅读 评论(0) 收藏

    开发项目时,为了是代码层次清晰.美观,常常需要调整多行,使之对齐.在网上也看到一些方法,感觉不好用,偶尔发现一个小技巧. (1)多行代码同时右移 同时选中几行,按"Tab"键,就会 ...

  9. HDU 1272 小希的迷宫(并查集) 分类: 并查集 2015-07-07 23:38 2人阅读 评论(0) 收藏

    Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...

随机推荐

  1. php实现显示网站运行时间-秒转换年月日时分秒

    <?php // 设置时区 date_default_timezone_set('Asia/Shanghai'); /** * 秒转时间,格式 年 月 日 时 分 秒 * * @author w ...

  2. Yii中使用PHPexcel获取excel中数据

    1.view中代码如下: <form name="frmBatchSettle" id="" action="" method=&qu ...

  3. 【Zend Studio】10.6.0版本设置默认字体

    1.打开Windows->Prefefences 2.找到General->Appearance->Colors and Fonts->Basic->Text Font- ...

  4. 6)Java中String类

    1)String对象的初始化   由于String对象特别常用,所以在对String对象进行初始化时,Java提供了一种简化的特殊语法,格式如下:     String s = “abc”;     ...

  5. 为你的Windows7设置动态壁纸

    From:http://www.cnblogs.com/killerlegend/p/3644014.html By KillerLegend DreamScene是Vista上的一个功能,可以让你设 ...

  6. 奇怪的函数 (codevs 3538/1696) 题解

    [题目描述] 给定n,使得x^x达到或超过n位数字的最小正整数x是多少? [样例输入] 11 [样例输出] 10 [解题思路] 首先想到枚举,但是范围有点大,n<=2*10^9,果断用二分.其实 ...

  7. VC下的人人对弈五子棋(dos)

    #include"stdio.h"#include"stdlib.h"#include"string.h"#include "io ...

  8. ubuntu server获取并自动设置最快镜像的方法

    一,安装方法1 add-apt-repository ppa:ossug-hychen/getfastmirrorapt-get install getfastmirror 如果添加了临时源,这样移除 ...

  9. Spark 3000门徒第一课随笔

    昨晚听了王家林老师的Spark 3000门徒系列课程的第一课,把scala基础过了一遍,对ArrayBuffer有了新的认识: Array本身创建后不可修改ArrayBuffer可修改import s ...

  10. 条款5:了解C++提供的默认函数

    当我们定义一个类时,如果没有声明任何函数,那么C++编译器会默认提供4个函数:默认构造函数.复制构造函数.赋值操作符函数.析构函数,并且这些函数默认都是public且inline的.因此,当你定义如下 ...