Codeforces 343D Water Tree【思维+Dfs序+线段树】好题!

D. Water Tree
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 aibi (1 ≤ ai, bi ≤ nai ≠ 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


题目大意:


给出一个具有N个点的树,1号节点是根节点。现在有三种操作:

①1 x,表示给x节点灌溉水,并且其儿子都会被灌上水。

②2 x,表示将x节点的水抽干,并且其父亲节点会一路被抽干。

③3 x,表示询问x节点是否有水。有水输出1,否则输出0。


思路(思路参考自:http://blog.csdn.net/u014410316/article/details/51037532):


①很显然,我们如果一个点是有水的,那么其子树就都一定有水。

②同理,我们如果一个点是没水的,那么其父亲就一定没有水。


Dfs序大家还是都会的这里不啰嗦了。


那么我们对于查询(操作3)操作,我们只要查询这个点的子树是否都是1即可,如果都是1的话,那么输出1,否则就是0.

那么对于操作2,我们对应单点修改线段树置0即可。

那么对于操作1,如果我们当前点的子树不都有水,那么先置其父亲为0(这样再查询上边的祖先节点的时候才能保证结果正确,我们这个操作相当于将一个抽干的点上置一下,很容易理解).然后再将整个子树区间置1即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
vector<int>mp[600000];
int L[600000];
int R[600000];
int fa[600000];
int cnt;
void Dfs(int u,int from)
{
    fa[u]=from;
    L[u]=++cnt;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        Dfs(v,u);
    }
    R[u]=cnt;
}
/****************************/
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
int tree[500500*4];
int flag[500050*4];
void pushup(int rt)
{
    tree[rt]=(tree[rt*2]&&tree[rt*2+1]);
}
void pushdown(int l,int r,int rt)
{
    if(flag[rt])
    {
        int m=(l+r)/2;
        flag[rt*2]=flag[rt];
        flag[rt*2+1]=flag[rt];
        tree[rt*2]=flag[rt];
        tree[rt*2+1]=flag[rt];
        flag[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    tree[rt]=flag[rt]=0;
    if(l==r)return ;
    int m=(l+r)/2;
    build(lson);build(rson);pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        flag[rt]=tree[rt]=c;
        return ;
    }
    pushdown(l,r,rt);
    int m=(l+r)/2;
    if(L<=m)update(L,R,c,lson);
    if(R>m)update(L,R,c,rson);
    pushup(rt);
}
void update_Del(int p,int l,int r,int rt)
{
    if(l==r)
    {
        tree[rt]=0;
        return ;
    }
    pushdown(l,r,rt);
    int m=(l+r)/2;
    if(p<=m)update_Del(p,lson);
    if(p>m)update_Del(p,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        return tree[rt];
    }
    int ans=1;
    pushdown(l,r,rt);
    int m=(l+r)/2;
    if(L<=m)ans=min(ans,query(L,R,lson));
    if(R>m)ans=min(ans,query(L,R,rson));
    pushup(rt);
    return ans;
}
/****************************/
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        cnt=0;
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=1;i<=n-1;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        Dfs(1,-1);
        build(1,n,1);
        int q;scanf("%d",&q);
        while(q--)
        {
            int op,x;
            scanf("%d%d",&op,&x);
            if(op==1)
            {
                if(!query(L[x],R[x],1,n,1)&&fa[x]!=-1)update_Del(L[fa[x]],1,n,1);
                update(L[x],R[x],1,1,n,1);
            }
            if(op==2)
            {
                update_Del(L[x],1,n,1);
            }
            if(op==3)
            {
                printf("%d\n",query(L[x],R[x],1,n,1));
            }
        }
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值