POJ3321 Apple Tree(DFS序)

题目,是对一颗树,单点修改、子树查询。典型的dfs序入门题。

DFS序可以将一颗树与子树们表示为一个连续的区间,然后用线段树来维护;感觉算是树链剖分的一种吧,和轻重链剖分不同的是这是对子树进行剖分的。

我用非递归方式求DFS序。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 #define MAXN 111111
 6 struct Edge{
 7     int v,nxt;
 8 }edge[MAXN<<1];
 9 int n,NE,head[MAXN];
10 void addEdge(int u,int v){
11     edge[NE].v=v; edge[NE].nxt=head[u]; head[u]=NE++;
12 }
13
14 int f[MAXN],l[MAXN],r[MAXN],stack[MAXN],odr;
15 void dfs(){
16     int top=0;
17     stack[++top]=1;
18     while(top){
19         int u=stack[top];
20         if(l[u]){
21             r[u]=++odr; --top;
22             continue;
23         }
24         l[u]=++odr;
25         for(int i=head[u]; i!=-1; i=edge[i].nxt){
26             int v=edge[i].v;
27             if(f[u]==v) continue;
28             f[v]=u; stack[++top]=v;
29         }
30     }
31 }
32
33 int N,tree[MAXN<<3],x,y;
34 void update(int i,int j,int k){
35     if(i==j){
36         tree[k]^=1;
37         return;
38     }
39     int mid=i+j>>1;
40     if(x<=mid) update(i,mid,k<<1);
41     else update(mid+1,j,k<<1|1);
42     tree[k]=tree[k<<1]+tree[k<<1|1];
43 }
44 int query(int i,int j,int k){
45     if(x<=i&&j<=y) return tree[k];
46     int mid=i+j>>1,res=0;
47     if(x<=mid) res=query(i,mid,k<<1);
48     if(y>mid) res+=query(mid+1,j,k<<1|1);
49     return res;
50 }
51
52 int main(){
53     int m,a,b;
54     char op[11];
55     memset(head,-1,sizeof(head));
56     scanf("%d",&n);
57     for(int i=1; i<n; ++i){
58         scanf("%d%d",&a,&b);
59         addEdge(a,b);
60         addEdge(b,a);
61     }
62     dfs();
63     for(N=1; N<odr; N<<=1);
64     for(int i=1; i<=n; ++i){
65         x=l[i];
66         update(1,N,1);
67     }
68     scanf("%d",&m);
69     while(m--){
70         scanf("%s%d",op,&a);
71         if(op[0]==‘Q‘){
72             x=l[a]; y=r[a];
73             printf("%d\n",query(1,N,1));
74         }else{
75             x=l[a];
76             update(1,N,1);
77         }
78     }
79     return 0;
80 }
时间: 2024-12-27 23:33:58

POJ3321 Apple Tree(DFS序)的相关文章

[poj3321]Apple Tree(dfs序+树状数组)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been

POJ3321 - Apple Tree DFS序 + 线段树或树状数组

Apple Tree:http://poj.org/problem?id=3321 题意: 告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这个点上的苹果,就是有就去掉,没有就加上. 思路: 先对树求一遍dfs序,每个点保存一个l,r.l是最早到这个点的时间戳,r是这个点子树中的最大时间戳,这样就转化为区间问题,可以用树状数组,或线段树维护区间的和. #include <algorithm> #include <iterator&

POJ 3321 Apple Tree DFS序 + 树状数组

多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照dfs序修改,因为你查询就是按照dfs查的,所以修改也要用dfs序修改 L[i]是唯一的. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <

POJ 3321 Apple Tree DFS序+fenwick

题目大意:有一颗长满苹果的苹果树,有两个操作. 1.询问以一个点为根的子树中有多少个苹果. 2.看看一个点有没有苹果,假设没有苹果.那么那里就立即长出一个苹果(= =!):否则就把那个苹果摘下来. 思路:进行一次深搜,将每一个节点最開始出现的时间和最后出现的时间记在一个数组里,那么这两点之间的点就是它以及它的子树的二倍,然后就用树状数组来维护区间和即可了. CODE: #include <cstdio> #include <cstring> #include <iostrea

POJ3321 Apple Tree 题解

POJ3321 Apple Tree 线段数例题解析合集 题意:给定一棵树,有两种操作:1.把某个节点上的数^1(若是1改为0,是0改为1) 2.查询以某个节点为根节点的子树中1的个数 在一棵树上进行单点修改和查询,只要进行一遍dfs,记录每个点的dfs序即可把问题转化到链上用线段树进行维护 更改和模板一样,查询时以每棵子树遍历时第一个节点的dfs序和最后一个节点的边界作为查询区间(每颗子树中节点的DFS序是连续的) #include <stdio.h> #include <iostre

【POJ 3321】 Apple Tree (dfs重标号设区间+树状数组求和)

[POJ 3321] Apple Tree (dfs重标号设区间+树状数组求和) Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21966   Accepted: 6654 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. K

POJ-3321 Apple Tree 【DFS序+树状数组】

Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by branches.

POJ3321 Apple Tree

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26989   Accepted: 8004 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been

ACM学习历程——POJ3321 Apple Tree(搜索,线段树)

      Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by bran