Codeforces 343D Water Tree dfs序+线段树

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

题意:给定一棵树(给定图一定是树)

1、把v点及其子树灌上水

2、把v点及v到根的路径去掉水

3、询问v点是否有水

思路:

dfs序(不是欧拉序列)把树转成dfs_clock

那么对于点v 出现的时间in[v]和消失的时间out[v] ,一定会把v子树下所有节点都夹在[ in[v], out[v] ]之中,则操作1就是把 [in[v], out[v]]改成1


把路径去掉水,显然是不存在这样直接到达根部的链,所以单点更新 in[v] = 0,若询问[in[u], out[u]]时 区间内存在一个0,则u子树下存在0,即u是没有水的。


注意的是,此时如果把u这个区间灌上水,则会把v这个去水效果去掉,即在线段树上找不到痕迹,所以把 Father[u] 这个点单点更新成0。

AC1:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define N 510000
#define L(x) (x<<1)
#define R(x) (x<<1|1)
inline int Mid(int a,int b){return (a+b)>>1;}
struct Edge{
	int from, to, nex;
}edge[N<<1];
int head[N], edgenum;
void add(int u,int v){
	Edge E={u,v,head[u]};
	edge[edgenum] = E;
	head[u] = edgenum++;
}
struct node{
	int l,r,minn;
}tree[N<<2];
void push_up(int id){
	tree[id].minn = min(tree[id].minn,min(tree[L(id)].minn,tree[R(id)].minn));
}
void push_down(int id){
	if(tree[id].l==tree[id].r)return;
	if(tree[id].minn){
		tree[L(id)].minn = tree[R(id)].minn = 1;
	}
}
void build(int l,int r,int id){
	tree[id].l = l, tree[id].r = r;
	tree[id].minn = 0;
	if(l==r)return ;
	int mid = Mid(l,r);
	build(l,mid,L(id)); build(mid+1,r,R(id));
}
void updata_point(int pos,int id){
	push_down(id);
	if(tree[id].l==tree[id].r){
		tree[id].minn = 0;
		return;
	}
	int mid = Mid(tree[id].l,tree[id].r);
	if(pos<=mid)
		updata_point(pos,L(id));
	else
		updata_point(pos,R(id));
	push_up(id);
}
void updata_inter(int l,int r,int id){
	push_down(id);
	if(l == tree[id].l && tree[id].r == r){
		tree[id].minn = 1;
		return ;
	}
	int mid = Mid(tree[id].l, tree[id].r);
	if(mid<l)
		updata_inter(l,r,R(id));
	else if(r<=mid)
		updata_inter(l,r,L(id));
	else {
		updata_inter(l,mid,L(id));
		updata_inter(mid+1,r,R(id));
	}
	push_up(id);
}
int query(int l,int r,int id){
	push_down(id);
	if(l==tree[id].l && tree[id].r == r)
		return tree[id].minn;
	int mid = Mid(tree[id].l,tree[id].r);
	if(mid<l)
		return query(l,r,R(id));
	else if(r<=mid)
		return query(l,r,L(id));
	else return min(query(l,mid,L(id)),query(mid+1,r,R(id)));
}
int n;
int in[N], out[N], Time, Father[N];
void dfs(int u,int fa){
	Father[u] = fa;
	in[u] = ++Time;
	for(int i = head[u]; ~i; i = edge[i].nex){
		int v = edge[i].to; if(v==fa)continue;
		dfs(v,u);
	}
	out[u] = Time;
}
void init(){
	memset(head, -1, sizeof head);edgenum=0;
}
int main(){
	int i, j, u, v, que;
	while(~scanf("%d",&n)){
		init();
		for(i=1;i<n;i++){
			scanf("%d %d",&u,&v);
			add(u,v); add(v,u);
		}
		Time = 0;
		dfs(1,-1);
		build(1,Time,1);
		scanf("%d",&que);
		while(que--){
			scanf("%d %d",&u,&v);
			if(u==1){
				int tmp = query(in[v],out[v],1);
				updata_inter(in[v],out[v],1);
				if(Father[v]!=-1 && tmp==0)
					updata_point(in[Father[v]],1);
			}
			else if(u==2){
				updata_point(in[v],1);
			}
			else 
				printf("%d\n",query(in[v],out[v],1));
		}
	}
	return 0;
}
/*
5
1 2
5 1
4 2
2 3

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

*/

AC2:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define N 1010000
#define L(x) (x<<1)
#define R(x) (x<<1|1)
inline int Mid(int a,int b){return (a+b)>>1;}
struct Edge{
	int from, to, nex;
}edge[N<<1];
int head[N], edgenum;
void add(int u,int v){
	Edge E={u,v,head[u]};
	edge[edgenum] = E;
	head[u] = edgenum++;
}
struct node{
	int l,r,sum,lazy;
	int size(){return r-l+1;}
}tree[N<<2];
void push_up(int id){
	if(tree[id].l==tree[id].r)return;
	tree[id].sum = tree[L(id)].sum + tree[R(id)].sum;
}
void push_down(int id){
	if(tree[id].l==tree[id].r)return;
	if(tree[id].lazy){
		if(tree[id].sum)
		{
			tree[L(id)].sum = tree[L(id)].size();
			tree[R(id)].sum = tree[R(id)].size();
		}
		else
		{
			tree[L(id)].sum = tree[R(id)].sum = 0;
		}
		tree[L(id)].lazy = tree[R(id)].lazy = 1;
		tree[id].lazy = 0;
	}
}
void build(int l,int r,int id){
	tree[id].l = l, tree[id].r = r;
	tree[id].sum = tree[id].lazy = 0;
	if(l==r)return ;
	int mid = Mid(l,r);
	build(l,mid,L(id)); build(mid+1,r,R(id));
}
void updata(int l,int r,int val,int id){
	push_down(id);
	if(l == tree[id].l && tree[id].r == r){
		if(val)
		{
			tree[id].sum = tree[id].size();
			tree[id].lazy = 1;
		}
		else 
		{
			tree[id].sum = 0;
			tree[id].lazy = 0;
		}
		return ;
	}
	int mid = Mid(tree[id].l, tree[id].r);
	if(mid<l)
		updata(l,r,val,R(id));
	else if(r<=mid)
		updata(l,r,val,L(id));
	else {
		updata(l,mid,val,L(id));
		updata(mid+1,r,val,R(id));
	}
	push_up(id);
}
int query(int l,int r,int id){
	push_down(id);
	if(l==tree[id].l && tree[id].r == r)
		return tree[id].sum;
	int mid = Mid(tree[id].l,tree[id].r);
	if(mid<l)
		return query(l,r,R(id));
	else if(r<=mid)
		return query(l,r,L(id));
	else return query(l,mid,L(id)) + query(mid+1,r,R(id));
}
int n;
int in[N], out[N], Time, si[N], pa[N];
void dfs(int u,int fa){
	in[u] = ++Time;
	si[u] = 1;
	pa[u] = fa;
	for(int i = head[u]; ~i; i = edge[i].nex){
		int v = edge[i].to; if(v==fa)continue;
		dfs(v,u);
		si[u] += si[v];
	}
	out[u] = Time;
}
void init(){
	memset(head, -1, sizeof head);edgenum=0;
}
int main(){
	int i, j, u, v, que;
	while(~scanf("%d",&n)){
		init();
		for(i=1;i<n;i++){
			scanf("%d %d",&u,&v);
			add(u,v); add(v,u);
		}
		Time = 0;
		dfs(1,-1);
		build(1,Time,1);
		scanf("%d",&que);
		while(que--){
			scanf("%d %d",&u,&v);
			if(u==1){
				int tmp = query(in[v],out[v],1);
				updata(in[v],out[v],1,1);
				if(tmp!=si[v] && pa[v]!=-1)
					updata(in[pa[v]],in[pa[v]],0,1);
			}
			else if(u==2){
				updata(in[v],in[v],0,1);
			}
			else 
				printf("%d\n",query(in[v],out[v],1)==si[v]);
		}
	}
	return 0;
}
/*
5
1 2
5 1
4 2
2 3

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

*/


你可能感兴趣的:(Codeforces 343D Water Tree dfs序+线段树)