Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏
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
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.
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.
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
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) 收藏的更多相关文章
- MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏
我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...
- 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 ...
- 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 库 ( ...
- 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 ...
- 使用Broadcast实现android组件之间的通信 分类: android 学习笔记 2015-07-09 14:16 110人阅读 评论(0) 收藏
android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...
- 使用JavaScriptSerializer序列化集合、字典、数组、DataTable为JSON字符串 分类: 前端 数据格式 JSON 2014-10-30 14:08 169人阅读 评论(0) 收藏
一.JSON简介 JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式. JSON是"名值对"的集合.结构由大 ...
- vs2008 多人同时开发项目时的代码注释规范格式 分类: C#小技巧 2014-04-23 14:12 297人阅读 评论(0) 收藏
多人同时开发一个项目,区分项目的那个窗体是谁开发的,例:下面的格式 /************************************************ 模块:服务器设置 ...
- 代码对齐 分类: C#小技巧 2014-04-17 14:45 166人阅读 评论(0) 收藏
开发项目时,为了是代码层次清晰.美观,常常需要调整多行,使之对齐.在网上也看到一些方法,感觉不好用,偶尔发现一个小技巧. (1)多行代码同时右移 同时选中几行,按"Tab"键,就会 ...
- HDU 1272 小希的迷宫(并查集) 分类: 并查集 2015-07-07 23:38 2人阅读 评论(0) 收藏
Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
随机推荐
- HTML5 对于手机页面长按会粘贴复制的禁用 (解决方案)
解决方案: 直接在CSS 文件中添加下面的代码,就可以实现了在手机端禁止粘贴复制的功能: *{ -webkit-touch-callout:none; /*系统默认菜单被禁用*/ -we ...
- scan的filter使用
本次操作的hbase的t1表的数据是: hbase(main)::> scan 't1' ROW COLUMN+CELL column=f1:age, timestamp=, value= co ...
- jquery美化select,自定义下拉框样式
select默认的样式比较丑,有些应用需要美化select,在网上找到一个很好的美化样式效果,本人很喜欢,在这里分享一下. <!DOCTYPE html PUBLIC "-//W3C/ ...
- MySQL: ON DUPLICATE KEY UPDATE 用法 避免重复插入数据
INSERT INTO osc_visit_stats(stat_date,type,id,view_count) VALUES (?,?,?,?) ON DUPLICATEKEY UPDATE vi ...
- 用过的一些js函数[备份用的]
1.类似php的htmlspecialchars函数,如需要可以自行增加其它代替 function _htmlspecialchars(str) { str = str.replace(/&/ ...
- php对mysql简单读取的实例
读取mysql数据库 例. <?php $link=mysql_connect("localhost","root","之前的管理员密码& ...
- 通过Messenger与后台连接(单向操作,activity向service发送数据)
xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- Android开发之Source无法覆写public void onClick(View v)
初学Android开发,在为一个按钮[该按钮继承OnClickListener()]写监听时,发现无法在Source中引入public void onClick(View v),当时非常纳闷,平常情况 ...
- android空鼠修改
抛弃盒子自带遥控器后,又不满意改键红外遥控器,选择飞鼠及无线键鼠成为最终方案.问题是:菜单键如何实现!其实很简单:即插即用USB无线飞鼠及键鼠套装只涉及2个文件:system/usr/layout/G ...
- MVC MVVM Knockout 常遇问题总结
1.模板绑定(使用插件jquery.tmpl) var ViewModel={Product:ko.observable()} <div data-bind="template:{na ...