Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏
2014-10-05 14:38
483 查看
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:
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.
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
Output
感觉好神奇的方法,用俩数组表示出一棵树;
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.
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; }
相关文章推荐
- UITableView基础(一) 分类: ios开发 2014-12-09 14:38 167人阅读 评论(0) 收藏
- Brush Mode --- Nyoj 236 分类: Brush Mode 2014-04-02 06:56 116人阅读 评论(0) 收藏
- codeforces 438D 分类: codeforces 2015-03-28 10:18 38人阅读 评论(0) 收藏
- Keil C51 的printf 分类: 嵌入式开发学习 2015-03-31 14:15 98人阅读 评论(0) 收藏
- Brush Mode --- Nyoj 737 分类: Brush Mode 2014-03-25 08:10 202人阅读 评论(0) 收藏
- Poj 2528 Mayor's posters 分类: Brush Mode 2014-07-23 09:12 84人阅读 评论(0) 收藏
- Linux TOP命令 按内存占用排序和按CPU占用排序 分类: 测试 ubuntu 虚拟机 2013-11-06 14:38 396人阅读 评论(0) 收藏
- Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏
- __int64 与long long 的区别 分类: Brush Mode 2014-08-14 10:22 64人阅读 评论(0) 收藏
- CursorAdapter类的使用 分类: Android界面和组件 2014-06-12 14:34 98人阅读 评论(0) 收藏
- codeforces #310 div2 D 分类: codeforces 2015-06-29 20:03 29人阅读 评论(0) 收藏
- Poj 2559 最大矩形面积 v单调栈 分类: Brush Mode 2014-11-13 20:48 81人阅读 评论(0) 收藏
- Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏
- Eclipse断点调试 分类: Java 2015-07-28 14:38 12人阅读 评论(0) 收藏
- NYOJ 119 士兵杀敌(三)【ST算法】 分类: Brush Mode 2014-11-13 20:56 101人阅读 评论(0) 收藏
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
- Codeforce 438D-The Child and Sequence 分类: Brush Mode 2014-10-06 20:20 102人阅读 评论(0) 收藏
- Overview 分类: Cocos2d-x 2015-02-09 18:16 98人阅读 评论(0) 收藏
- Beautiful People 分类: Brush Mode 2014-10-01 14:33 100人阅读 评论(0) 收藏