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:
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.
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
这个题是真的有意思,就像是那种累积起来的酒杯,当你往上面的节点里注水,那么他的子孙节点就会被注满水;当你把下面的某一节点的水放出来之后,那么他的直系亲属 父亲,爷爷辈的节点中的水也会留出,注意只是那一条链哦
现在给你三个命令:
1、往某个节点注水(子孙节点肯定也有水了)
2、将某个节点的水抽出来(parent节点肯定没水了)
3、查询某一个点是否有水,0表示没水,1表示有水
很明显这是一个父节点控制自己的子节点的dfs序问题,我们先将树形的关系转化为线型的
这有一篇关于dfs序的blog :https://blog.csdn.net/qq_44786250/article/details/100046030
这里需要使用线段树使用
注水很简单,就是进行区间修改,直接将子孙节点就修改掉了;
但是抽水怎么办呢?我们可没有直接修改树中的某一条链的操作,这里就是题目最有意思的地方了:
我们只将给定的那个节点的水放掉,虽然说他的父亲爷爷的水都被放掉了,但我们先不进行修改
当我们查询某个节点的时候,我们可以先算一下这个点控制的区间的含水的数目,然后与这个区间的长度进行比较(当然了,这里的区间肯定是在一棵子树上的,即可以有某个节点来控制,那么区间端点就是他的两个时间戳了)
如果区间长度等于这个点下的含水量,很明显这是都有水,否则他的儿子中肯定至少有一个没水的,儿子没水,那么漏上来之后,这个祖先肯定就没水喽,确实有意思,嘻嘻
这个题还有一个坑点。。。
当你把一个节点(根节点除外)的水放掉之后,他的父亲爷爷那条链上应该是没有水的,但是我们却没有修改他的父亲爷爷...这时我们再把这个点及其以下的注水之后,再来查询他的父亲节点,来事了!!!下面是满满的啊,父亲也是有水的,查到的父亲肯定是有水的,但其实已经被放掉了啊。。。这个坑是真的没看出来啊啊啊啊~~~~~
那么怎么去解决呢?不知道哪个大佬想到的nb办法,太妙了,膜拜一下先 Orz
这里的方法是在你每一次注水前先查询一下这个点的父亲节点,如果父亲节点控制的区间含水量和区间长度不等,说明父亲应该是没有水的,我们把这个点置空,这样即使你去查询上上方的节点也不怕了。想想还真的是这样呢
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include