POJ3321-Apple Tree(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?

https://vj.ti12z.cn/95a992a75f75b7d92488d79284d3b4ef?v=1565064077

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

题意

一棵树上,节点处会有苹果,刚开始所有节点都有苹果。1永远是根。有查询和修改两种操作:查询某个结点的子树(包括该结点)有多少个苹果;修改某个结点的情况,如果有苹果则摘除,没有则生长出一个苹果。

DFS序

从根遍历一遍,记录节点i先序和后序的编号在in[i]和out[i],将树型结构重构成线性结构,节点i有了新的编号in[i],节点i的子树苹果树总和就是区间in[i]到out[i]。然后用线段树单点修改和区间和查询。

链式前向星

struct Edge
{
    int to,next;
}edge[Max*2];
int head[Max],cnt=0;
void addEdge(int from,int to)
{
    edge[cnt].to = to;
    edge[cnt].next = head[from];
    head[from] = cnt++;
}

for(int i=head[x];i!=-1;i=edge[i].next){
    edge[i].to,x
}

 

坑点:用链式前向星记录树,用vector会TLE。edge[Max*2],因为边双向都保存了,所以是Max*2;

 


#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int Max = 100010;

struct Edge
{
    int to,next;
}edge[Max*2];
int head[Max],cnt=0;
void addEdge(int from,int to)
{
    edge[cnt].to = to;
    edge[cnt].next = head[from];
    head[from] = cnt++;
}

struct node
{
    int l,r,w;
};
node tree[Max*4];
int n,m,x,y,in[Max],out[Max],vis[Max];
char ch;

void build(int k,int l,int r)
{
    tree[k].l = l; tree[k].r = r; tree[k].w = 1;
    if(l==r){
        tree[k].w = 1;
        return;
    }
    int mid = (l+r)/2;
    build(k*2,l,mid);
    build(k*2+1,mid+1,r);
    tree[k].w = tree[k*2].w + tree[k*2+1].w;
}

void update(int k,int p)
{
    if(p<tree[k].l||p>tree[k].r) return;
    if(tree[k].l==tree[k].r&&tree[k].r==p){
        tree[k].w ^= 1;
        return;
    }
    update(k*2,p);
    update(k*2+1,p);
    tree[k].w = tree[k*2].w + tree[k*2+1].w;
}

int query(int k,int l,int r)
{
    if(r<tree[k].l||l>tree[k].r) return 0;
    if(l<=tree[k].l&&tree[k].r<=r) return tree[k].w;
    return query(k*2,l,r) + query(k*2+1,l,r);
}

int tot=0;
void dfs(int x,int fa)
{

    tot++;
    in[x] = tot;
    for(int i=head[x];i!=-1;i=edge[i].next){
        if(fa!=edge[i].to) dfs(edge[i].to,x);
    }
    out[x] = tot;
}

int main()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(edge,0,sizeof(edge));
    memset(vis,0,sizeof(vis));

    scanf("%d",&n);
    for(int i=1;i<n;i++){
        scanf("%d%d",&x,&y);
        addEdge(x,y);
        addEdge(y,x);
    }
    tot=0;
    dfs(1,0);
    build(1,1,tot);
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        scanf(" %c%d",&ch,&x);
        if(ch=='Q'){
            printf("%d\n",query(1,in[x],out[x]));
        }
        else if(ch=='C'){
            update(1,in[x]);
        }
    }
    return 0;
}

更新,线段树update和query的写法,if判断是否进入左右子树

#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int N = 100010;

struct Edge
{
    int to,next;
}edge[N*2];
int head[N],cnt=0;
void addEdge(int from,int to)
{
    edge[cnt].to = to;
    edge[cnt].next = head[from];
    head[from] = cnt++;
}

struct node
{
    int l,r,w;
};
node tr[N*4];
int n,m,x,y,in[N],out[N],vis[N];
char ch;

void build(int k,int l,int r)
{
    tr[k].l = l; tr[k].r = r; tr[k].w = 1;
    if(l==r){
        tr[k].w = 1;
        return;
    }
    int mid = (l+r)/2;
    build(k*2,l,mid);
    build(k*2+1,mid+1,r);
    tr[k].w = tr[k*2].w + tr[k*2+1].w;
}

void update(int k,int p)
{
    if(tr[k].l==tr[k].r&&tr[k].r==p){
        tr[k].w ^= 1;
        return;
    }
    int mid = (tr[k].l + tr[k].r)/2;
    if(p<=mid) update(k*2,p);
    if(p>mid) update(k*2+1,p);
    tr[k].w = tr[k*2].w + tr[k*2+1].w;
}

int query(int k,int l,int r)
{
    if(l<=tr[k].l&&tr[k].r<=r) return tr[k].w;
    int sum = 0;
    int mid = (tr[k].l + tr[k].r)/2;
    if(l<=mid) sum += query(k*2,l,r);
    if(r>mid) sum += query(k*2+1,l,r);
    return sum;
}

int tot=0;
void dfs(int x,int fa)
{

    tot++;
    in[x] = tot;
    for(int i=head[x];i!=-1;i=edge[i].next){
        if(fa!=edge[i].to) dfs(edge[i].to,x);
    }
    out[x] = tot;
}

int main()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(edge,0,sizeof(edge));
    memset(vis,0,sizeof(vis));

    scanf("%d",&n);
    for(int i=1;i<n;i++){
        scanf("%d%d",&x,&y);
        addEdge(x,y);
        addEdge(y,x);
    }
    tot=0;
    dfs(1,0);
    build(1,1,tot);
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        scanf(" %c%d",&ch,&x);
        if(ch=='Q'){
            printf("%d\n",query(1,in[x],out[x]));
        }
        else if(ch=='C'){
            update(1,in[x]);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值