Apple Tree POJ - 3321 (dfs序+线段树) dfs序初见讨伐战!

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. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won’t grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.

The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?

Input
The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
“C x” which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
“Q x” which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

Output
For every inquiry, output the correspond answer per line.
Sample Input
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output
3
2
题意:给一颗树。n个结点,和m次操作。操作分两种,第一种是另一个结点异或xor1(如果是1就变成0,如果是0就变成1),不影响子节点。第二种操作是查询一个结点和他所有的子节点(包括子节点的子节点……)之和是多少。
解题思路:使用dfs序,将这棵树转换呈线性区间。再用线段树或树状数组去维护。这道题卡vector,用向前星存边比较好。
dfs序过程:以
7
1 2
1 3
2 4
2 5
2 6
3 7为例dfs在这里插入图片描述
可以发现,用dfs序建立的区间比较好的继承了树的父子关系。以这再建立线段树
在这里插入图片描述

错误原因:卡我vector

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define LL long long
#define MT(a,b) memset(a,b,sizeof(a))
const int mod=10007;
const int maxn=1e5+5;
const int ONF=-0x3f3f3f3f;
const int INF=0x3f3f3f3f;
//vector<int>vec[maxn<<2];
int ls[maxn],rs[maxn];
struct node{
    int val;
}tree[maxn<<2];
struct node2{
    int to,next;
}edge[maxn<<2];
int head[maxn<<1],cnt=0;

void reset(){
    cnt=0;
    MT(head,-1);
}

void add(int from,int to){
    edge[++cnt].next=head[from];
    edge[cnt].to=to;
    head[from]=cnt;
}

void dfs(int x,int fa){
    ls[x]=++cnt;
    for (int i=head[x];~i;i=edge[i].next){
        int son=edge[i].to;
        if (son==fa) continue;
        dfs(son,x);
    }
    rs[x]=cnt;
}

void build_tree(int l,int r,int root){
    if (l==r){
        tree[root].val=1;
        return;
    }
    int mid=(l+r)>>1;
    build_tree(l,mid,root<<1);
    build_tree(mid+1,r,root<<1|1);
    tree[root].val=tree[root<<1].val+tree[root<<1|1].val;
}

void update(int l,int r,int pos,int root){
    if (l==pos&&r==pos){
        tree[root].val=tree[root].val^1;
        return;
    }
    int mid=(l+r)>>1;
    if (mid>=pos) update(l,mid,pos,root<<1);
    else update(mid+1,r,pos,root<<1|1);
    tree[root].val=tree[root<<1].val+tree[root<<1|1].val;
}

int query(int l,int r,int ql,int qr,int root){
    if (l==ql&&r==qr){
        return tree[root].val;
    }
    int mid=(l+r)>>1;
    if (qr<=mid) return query(l,mid,ql,qr,root<<1);
    else if (ql>mid) return query(mid+1,r,ql,qr,root<<1|1);
    else return query(l,mid,ql,mid,root<<1)+query(mid+1,r,mid+1,qr,root<<1|1);
}

int main (){
    int n,m;
    reset();
    scanf("%d",&n);
    for (int i=1;i<n;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
    }
    cnt=0;
    dfs(1,-1);
    scanf("%d",&m);
    build_tree(1,n,1);
    char str[5];
    int v;
    for (int i=1;i<=m;i++){
        scanf("%s",&str);
        scanf("%d",&v);
        if (str[0]=='Q'){
            printf("%d\n",query(1,n,ls[v],rs[v],1));
        }
        if (str[0]=='C'){
            update(1,n,ls[v],1);
        }
    }
    return 0;
}
发布了41 篇原创文章 · 获赞 20 · 访问量 3017

猜你喜欢

转载自blog.csdn.net/weixin_43925900/article/details/104009749
今日推荐