1. 程式人生 > >樹鏈剖分【CF343D】Water Tree

樹鏈剖分【CF343D】Water Tree

Description

Mad scientist Mike has constructed a rooted tree, which consists of nnvertices. Each vertex is a reservoir which can be either empty or filled with water.

The vertices of the tree are numbered from 1 to nn 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 vv with water. Then vv and all its children are filled with water.

  2. Empty vertex vv . Then vv and all its ancestors are emptied.

  3. Determine whether vertex vv 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-1n−1 lines contains two space-separated numbers \(a_{i}, b_{i}\) ( \(1<=a_{i},b_{i}<=n, a_{i}≠b_{i}\) ) — 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 \(c_{i}( 1<=c_{i}<=3\) ), \(v_{i}\)( \(1<=v_{i}<=n\) ), where \(c_{i}\) is the operation type (according to the numbering given in the statement), and \(v_{i}\) 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.

你不需要理解題意,你只需要知道,這是一個樹剖裸題(雖然我沒一遍切。)

支援三種操作(初始值全部為\(0\))

  • 1.將節點\(v\)及其子樹賦值為\(1\).
  • 2.將節點\(v\)到根節點\(1\)的路徑上的點的值置為\(0\).
  • 3.查詢當前節點\(v\)的值。(只會為\(0\)\(1\))

對於每個操作\(3\),輸出一行。(具體見程式碼好了

這是一個不完整的樹剖,我沒建樹,有沒用到反\(dfs\)序。 emmm

程式碼

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define R register

using namespace std;

const int gz=500001;

inline void in(int &x)
{
    int f=1;x=0;char s=getchar();
    while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
    while(isdigit(s)){x=x*10+s-'0';s=getchar();}
    x*=f;
}
int head[gz],tot,n,m;

struct cod{int u,v;}edge[gz<<1];

inline void add(R int x,R int y)
{
    edge[++tot].u=head[x];
    edge[tot].v=y;
    head[x]=tot;
}

int dfn[gz],idx,son[gz],f[gz],depth[gz],size[gz],top[gz];

void dfs1(R int u,R int fa)
{
    f[u]=fa;depth[u]=depth[fa]+1;size[u]=1;
    for(R int i=head[u];i;i=edge[i].u)
    {
        if(edge[i].v==fa)continue;
        dfs1(edge[i].v,u);
        size[u]+=size[edge[i].v];
        if(son[u]==-1 or size[son[u]]<size[edge[i].v])
            son[u]=edge[i].v;
    }   
}

void dfs2(R int u,R int t)
{
    dfn[u]=++idx;top[u]=t;
    if(son[u]==-1)return ;
    dfs2(son[u],t);
    for(R int i=head[u];i;i=edge[i].u)
    {
        if(dfn[edge[i].v])continue;
        dfs2(edge[i].v,edge[i].v);
    }
}

int tg[gz<<2],tr[gz<<2];

#define ls o<<1
#define rs o<<1|1

inline void down(R int o)
{
    if(tg[o]==-1)return;
    tg[ls]=tg[rs]=tg[o];
    tr[ls]=tr[rs]=tr[o];
    tg[o]=-1;
    return ;
}

void change(R int o,R int l,R int r,R int x,R int y,R int k)
{
    if(x<=l and y>=r){tr[o]=tg[o]=k;return;}
    down(o);
    R int mid=(l+r)>>1;
    if(x<=mid)change(ls,l,mid,x,y,k);
    if(y>mid)change(rs,mid+1,r,x,y,k);
}

int query(R int o,R int l,R int r,R int pos)
{
    if(l==r)return tr[o];
    down(o);
    R int mid=(l+r)>>1;
    if(pos<=mid)return query(ls,l,mid,pos);
    else return query(rs,mid+1,r,pos);
}

inline void tchange(R int x,R int y)
{
    R int fx=top[x],fy=top[y];
    while(fx!=fy)
    {
        if(depth[fx]>depth[fy])
        {
            change(1,1,n,dfn[fx],dfn[x],0);
            x=f[fx];
        }
        else 
        {
            change(1,1,n,dfn[fy],dfn[y],0);
            y=f[fy];
        }
        fy=top[y],fx=top[x];
    }
    if(dfn[x]>dfn[y])swap(x,y);
    change(1,1,n,dfn[x],dfn[y],0);
    return ;
}

int main()
{
    in(n);memset(son,-1,sizeof son);
    for(R int i=1,x,y;i<n;i++)
    {
        in(x),in(y);
        add(x,y),add(y,x);
    }
    dfs1(1,0);dfs2(1,1);memset(tg,-1,sizeof tg);
    in(m);
    for(R int i=1,opt,v;i<=m;i++)
    {
        in(opt);
        switch(opt)
        {
            case 1:in(v);change(1,1,n,dfn[v],dfn[v]+size[v]-1,1);break;
            case 2:in(v);tchange(1,v);break;
            case 3:in(v);printf("%d\n",query(1,1,n,dfn[v]));break;
        }
    }
}