zoukankan      html  css  js  c++  java
  • cf-343D (深搜序+线段树)

    题目

    链接 http://codeforces.com/contest/343/problem/D

    Description

    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.

    Example 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

    Example Output

    0
    0
    0
    1
    0
    1
    0
    1

    分析

    • 题目大意:给一棵树,多次操作,每次对于节点 v 有三种操作:
    • 操作1:将以 v 为根的子树全都赋值为 1
    • 操作2:将 v 以及它所有祖先都赋值为 0
    • 操作3:询问 v 的值(1或者0)
    • 出于数据的庞大,直接暴力肯定不行
    • 我们可以发现这棵树有这几个性质:
    • 若节点 v 为根的子树中存在 0,那么 v 肯定为 0
    • 若节点 v 为 1,那么以它为根的的子树中的节点肯定都是 1
    • 于是我们就可以维护一下深搜序的区间和,深搜序让我们可以很快地知道某棵子树的信息
    • 这样每个操作就能变成这样:
    • 1:若子树v存在 0 节点 则把v的父亲更新成0。然后把 v 的子树全都更新成1
    • 2:把节点 v 更新成0
    • 3:询问以 v 为根的子树内是否有0节点,没有则表示1

    程序

    // http://codeforces.com/contest/343/problem/D
    #include <cstdio>
    #define INF 500005
    #define add(x,y) (to[++num]=head[x],e[num]=y,head[x]=num)
    #define For(x) for(int h=head[x],o=e[h]; h; o=e[h=to[h]])
    #define Full(x) (tree.que(1,bg[x],ed[x])==ed[x]-bg[x]+1)	//以 x 为根的子树是否都是有水的 
    int n,cnt,num,head[INF],to[2*INF],e[2*INF];
    int fa[INF],bg[INF],ed[INF];	//原树上的一个节点, 以它为根的子树在深搜序中为区间 [bg,ed] 
    
    struct segment_tree{
    	#define X tr[x]
    	#define sz(x) (X.r-X.l+1)
    	#define Mid ((X.l+X.r)>>1)
    	#define lx (x<<1)
    	#define rx ((x<<1)+1)
    	struct data{int l,r,sum,tag;} tr[4*INF];
    	void bui(int x,int l,int r){
    		X=(data){l,r,0,-1};
    		if (l<r) bui(lx,l,Mid),bui(rx,Mid+1,r),X.sum=tr[lx].sum+tr[rx].sum;
    	}
    	
    	void addtag(int x,int v){
    		X.tag=v;
    		X.sum=sz(x)*v;
    	}
    	
    	void pushdown(int x){
    		if (X.tag!=-1){
    			addtag(lx,X.tag);
    			addtag(rx,X.tag);
    			X.tag=-1;
    		}
    	}
    	
    	void chg(int x,int l,int r,int v){	//区间 [l,r] 赋值 为 v 
    		if (l<=X.l && X.r<=r) addtag(x,v);
    		else{
    			pushdown(x);
    			if (l<=tr[lx].r) chg(lx,l,r,v);
    			if (tr[rx].l<=r) chg(rx,l,r,v);
    			X.sum=tr[lx].sum+tr[rx].sum;
    		}
    	}
    	
    	int que(int x,int l,int r){	//询问区间 [l,r] 的和 
    		int ret=0;
    		if (l<=X.l && X.r<=r) return X.sum;
    		pushdown(x);
    		if (l<=tr[lx].r) ret+=que(lx,l,r);
    		if (tr[rx].l<=r) ret+=que(rx,l,r);
    		return ret;
    	}
    } tree;
    
    void dfs(int x,int F){	//得出深搜序以及各个子树在序中的区间 
    	bg[x]=++cnt;
    	fa[x]=F;
    	For(x) if (o!=F) dfs(o,x);
    	ed[x]=cnt;
    }
    
    int Q,q,u,v;
    int main(){
    	scanf("%d",&n);
    	for (int i=1; i<n; i++) scanf("%d%d",&u,&v),add(u,v),add(v,u);
    	dfs(1,0);
    	tree.bui(1,1,n);
    	scanf("%d",&Q);
    	while (Q-- && scanf("%d%d",&q,&v)){
    		if (q==1){
    			if (v!=1 && !Full(v)) tree.chg(1,bg[fa[v]],bg[fa[v]],0);	//将 x 的父亲更新成 0 (因为下面有0,说明父亲肯定也是0, 相当于一个 "0" 的标记向上传了) 
    			tree.chg(1,bg[v],ed[v],1);	//把 x 为根的子树都更新为 1 
    		}
    		if (q==2){
    			tree.chg(1,bg[v],bg[v],0);
    		}
    		if (q==3){
    			if (!Full(v)) puts("0");
    			else puts("1");
    		}
    	}
    	return 0;
    }
    

    提示

    • 注意操作1,要是 v 没有父节点(及 v==1 ),那么就不用把 fa[v] 复制成 0 了。
    • 线段树的小细节在写程序的时候要注意。
    • 本质就是区间赋值,区间查询的一个线段树。
  • 相关阅读:
    C++ int与string的相互转换(含源码实现)
    二维数组名和二级指针
    一道算法题-从1到n整数中1出现的次数
    一道算法题-求三个矩形的交集矩形。
    位域
    计划
    Bigtable:一个分布式的结构化数据存储系统
    The Google File System 中文版
    HIVE和HBASE区别
    区分 hdfs hbase hive hbase适用场景
  • 原文地址:https://www.cnblogs.com/hehepig/p/6773781.html
Copyright © 2011-2022 走看看