POJ 3321 Apple Tree dfs序+线段树

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36404449/article/details/53455442

题目地址POJ3321
题目描述:
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?这里写图片描述

题目大意:
是给出N个点,并且这N个点有N-1条边相连,呈树结构。1总是树根。每个点的的权值总是1。

输入:
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.

输入大意:
第一行一个N,表示有N个点。
下面N-1行有N-1条边将N个点相连。
下一行输入一个M,代表有M次询问。
M行给出一个字符和一个点。
当输入C X时表示将X这个点上的苹果摘下,如果这个点没有苹果就放上一个苹果。
如果输入Q X时查询以X点为根的树有几个苹果。
样例输入:
3
1 2
1 3
3
Q 1
C 2
Q 1
样例输出:
3
2
这道题,是学习DFS序的裸题。DFS序就是在DFS的时候记录下进入每个点的时间戳和出每个点的时间戳即可。附上代码:

void dfs(int x)
{
    t++;//遍历到一个点时,time++,不要用time变量noip易CE(谢谢瑞神犇指导)
    in[x]=t;//记录进入这个点的时间戳
    vv[x]=1;
    int pp=h[x];
    while(pp)
    {
        if(vv[v[pp]]==0)
        {
            dfs(v[pp]); //如果这个点没有被遍历过就遍历这个点
        }
        pp=next[pp];
    } 
    out[x]=t;//当这个点所有边,所有儿子都被遍历过后,记录出这个点的时间戳
}

因为DFS序后,每一颗子树都是连续的一段那么这个题就可以DFS序后,就变为简单的线段树单点修改,区间查询。单点修改时修改的区间应是给出的这个点的入时间戳。这里写图片描述
就好像图中的第三个点,修改是不能修改3,而是in[3]即4。
区间查询时,查询给出根的入时间戳和出时间戳的区间即可。
因为数据范围很大,虽然给我们续了一秒,但是2s也还是会T的,所以要加读入优化,字符不用读入优化(瑞学长好像错过?)。
附上代码:

#include<iostream>
#include<cstdio>
using namespace std;
inline int get_num()
{
    int num=0;
    char c;
    bool flag=0;
    while((c=getchar())==' '||c=='\n'||c=='\r');
    if(c=='-')
    flag=1;
    else
    num=c-'0';
    while(isdigit(c=getchar()))
    num=num*10+c-'0';
    return (flag?-1:1)*num;
}//读入优化
int n,m,p=0,t=0;
long long in[200010],out[200010],h[200010],v[200010],next[200010];
bool vv[200010];
struct re
{
    long long l,r,sum;
}tree[900010];
void add(int a,int b)
{
    p++;
    v[p]=b;
    next[p]=h[a];
    h[a]=p;
}//邻接表存图
void dfs(int x)
{
    t++;
    in[x]=t;
    vv[x]=1;
    int pp=h[x];
    while(pp)
    {
        if(vv[v[pp]]==0)
        {
            dfs(v[pp]); 
        }
        pp=next[pp];
    } 
    out[x]=t;
}//DFS序
void build(int d,int l,int r)
{
    tree[d].l=l;
    tree[d].r=r;
    if(tree[d].l==tree[d].r)
    {
        tree[d].sum=1;
        return;
    }
    build(d*2,l,(l+r)/2);
    build(d*2+1,(l+r)/2+1,r);
    tree[d].sum=tree[d*2].sum+tree[d*2+1].sum;
} //建树
void update(int d,int x)
{
    if(tree[d].l==x&&tree[d].r==x)
    {
        if(tree[d].sum==0)
        tree[d].sum=1;
        else
        if(tree[d].sum==1)
        tree[d].sum=0;
        return;
    }
    int mid=(tree[d].l+tree[d].r)/2;
    if(x>mid)
    {
        update(d*2+1,x);
    }
    else
    {
        if(x<=mid)
        {
            update(d*2,x);
        }
    }
    tree[d].sum=tree[d*2+1].sum+tree[d*2].sum;//不要忘记修改父亲区间的值
}//单点查询
long long find(int d,int l,int r)
{
    if(tree[d].l==l&&tree[d].r==r)
    {
        return tree[d].sum;
    }
    int mid=(tree[d].r+tree[d].l)/2;
    if(l>mid)
    {
        return find(d*2+1,l,r);
    }
    else
    {
        if(r<=mid)
        {
            return find(d*2,l,r);
        }
        else
        {
            return find(d*2,l,mid)+find(d*2+1,mid+1,r);
        }
    }
}//区间查询
int main()
{
    n=get_num();
    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        a=get_num();
        b=get_num();
        add(a,b);
        add(b,a);
    }
    dfs(1);
    /*for(int i=1;i<=n;i++)
    {
        cout<<in[i]<<" "<<out[i]<<endl;
    }用来检验DFS序的正确性,把入时间戳和出时间戳输出一下*/
    build(1,1,n);
    m=get_num(); 
    for(int i=1;i<=m;i++)
    {
        char c;
        int d;
        cin>>c;
        d=get_num();
        if(c=='C')
        update(1,in[d]);
        else
        cout<<find(1,in[d],out[d])<<'\n';
    }
} 

猜你喜欢

转载自blog.csdn.net/qq_36404449/article/details/53455442