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. 学习c的第8天

    #include <stdio.h> int main() { char ch; printf("请输入分数等级(A,B,C,D):"); scanf("%c ...

  2. SequoiaDB创始人:比MongoDB领先一到两年 打造企业级NoSQL数据库

    CSDN.NET   这几年来, NoSQL数据库凭借其易扩展.高性能.高可用.数据模型灵活等特色吸引到了大量新兴互联网公司的青睐,包括国内的淘宝.新浪.京东商城.360.搜狗等都已经在局部尝试NoS ...

  3. 管理员 修改MySQL 5.7.9 新版本的root密码方法以及一些新变化整理

    MySQL 5.7版本开始,增强密码验证机制,网上说安装的时候会在/root/.mysql_secret  文件中生成默认密码,这一点自 5.7.6版本以后也去掉了. 针对如果生成默认密码,网上有一个 ...

  4. 【转】DCC32的参数详解

    完整的内容如下: // DCC32编译器的设置说明. // Dcc32 [options] filename [options] // DCC32 [操作选项] 文件名称 [操作选项] // -A&l ...

  5. API - .addBack()

    从jQuery官网上提供的Example来看,这个API的意思是: 1   先圈定一个范围,[A] $( "div.after-addback" ) 2 再用find过滤一出一个更 ...

  6. DevExpress汉化(WinForm)

    /* *隔壁老王原创,2013-09-21,转载请保留本人信息及本文地址. *本文地址:http://wallimn.iteye.com/blog/1944191 */ 最简单的方式就是使用汉化资源, ...

  7. C#自定义导出数据到Excel中的类封装

    using System; using System.IO; using System.Data; using System.Collections; using System.Data.OleDb; ...

  8. C#读取和写入配置文件

    使用.Net2.0中的ConfigurationManager可以方便的实现对配置app.config的读取和写入. ConfigurationManager默认没有自动载入项目,使用前必须手动添加, ...

  9. openstack命令

    整理了Openstack命令: openstack aggregate add host openstack aggregate createopenstack aggregate deleteope ...

  10. SQL Server 一些关键字详解(二)

    1.LEFT JOIN 容易让人误解的地方 背景:因为在网上搜了下 LEFT JOIN 和 OUTER APPLY 的区别,时发现,有的网友解释为: 1) A   left  join  B  的连接 ...