CodeForces 343D Water Tree

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.

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. 1 表示把v点及其子树全部变为1
  2. 2 表示把v点及其祖先全部变为0
  3. 3 查询v点的值

思路

先把这棵树转化成DFS序,这样每个点就可以对其子树进行区间更新。
那么对于点 v 出现的时间 in[v] 和消失的时间 out[v] ,一定会把 v 子树的所有节点都夹在
[ in[v],out[v] ] 之中。
对于操作1,就是把 [ in[v], out[v] ]的值改成1。

对于操作2,把路径自下往上去掉水,显然是不存在这样直接到达根部的链,所以单点更新 in[v] = 0,代表 [ in[fa[v]],out[fa[v]] ] 区间没有水。

对于操作3,若询问 [ in[u], out[u] ] 时,区间内存在一个0,则 u 子树下存在0,即u是没有水的。

值得注意的是,就是操作1,如果v的父节点子树内存在0,那么v的父节点就应该更新为0,因为你的操作1是改变的 v 及其子树节点,不会对其父节点产生影响。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<ctype.h>
#include<vector>
#include<algorithm>
#include<sstream>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
const int inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f;
const int maxn = 601000;
struct Node{
    int to;
    int Next;
}edge[maxn<<1];
int head[maxn],cnt,dfn;
int in[maxn],out[maxn],fa[maxn];
void Init(int n)
{
    cnt = dfn = 0;
    for(int i=0;i<=n;i++){
        head[i] = -1;
        fa[i] = -1;
        in[i] = out[i] = 0;
    }
}
void add(int u,int v)
{
    edge[cnt].to = v;
    edge[cnt].Next = head[u];
    head[u] = cnt++;
}
void dfs(int x)
{
    in[x] = ++dfn;
    for(int i=head[x];i!=-1;i=edge[i].Next){
        int y = edge[i].to;
        if(fa[x]==y)    continue;
        fa[y] = x;
        dfs(y);
    }
    out[x] = dfn;
}
struct node
{
    int l,r;
    int x,laz;
}tree[maxn<<2];
void build(int k,int l, int r){
    tree[k].l = l,tree[k].r = r;
    tree[k].x = 0;
    tree[k].laz = 0;
    if(l==r) return ;
    int mid = (l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
}
void pushdown(int k) {
    if(tree[k].laz != -1) {
        tree[k<<1].laz = tree[k<<1|1].laz = tree[k].laz;
        tree[k<<1].x = tree[k<<1|1].x = tree[k].laz;
        tree[k].laz = -1;
    }
}

void pushup(int k) {
    tree[k].x = tree[k<<1].x&&tree[k<<1|1].x;
    if(tree[k<<1].laz==tree[k<<1|1].laz)
        tree[k].laz = tree[k<<1].laz;
    else tree[k].laz = -1;
}

void modify(int k,int l,int r,int val) {
    if(l <= tree[k].l && tree[k].r <= r) {
        tree[k].x = tree[k].laz = val;
        return ;
    }
    int mid = (tree[k].l+tree[k].r)>>1;
    pushdown(k);
    if(l <= mid) modify(k<<1,l,r,val);
    if(r > mid) modify(k<<1|1,l,r,val);
    pushup(k);
}

int query(int k,int l,int r) {
    if(l <= tree[k].l && tree[k].r <= r) return tree[k].x;
    int mid = (tree[k].l+tree[k].r)>>1;
    pushdown(k);
    int ans = 1;
    if(l <= mid) ans &= query(k<<1, l, r);
    if(r > mid) ans &= query(k<<1|1, l, r);
    return ans;
}

int main() {
    int n,m,op,x;
    int u, v;
    while(~scanf("%d", &n)) {
        Init(n);
        for(int i = 1; i < n; i++) {
            scanf("%d%d", &u, &v);
            add(u, v);
            add(v, u);
        }
        dfs(1);
        build(1,1,n);

        scanf("%d", &m);
        while(m--){
            scanf("%d%d", &op, &x);
            if(op==1){
                if(fa[x]!=-1&&query(1,in[fa[x]],out[fa[x]])<=0)
                    modify(1,in[fa[x]],in[fa[x]],0);
                //cout<<in[x]<<' '<<out[x]<<endl;
                modify(1,in[x],out[x],1);
            }
            else if(op==2) {
                modify(1,in[x],in[x],0);
            }
            else{
                int ret = query(1,in[x],out[x]);
                printf("%d\n", ret);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值