POJ3321(dfs序+树状数组)

来源:互联网 发布:编程游戏软件 编辑:程序博客网 时间:2024/05/17 03:06

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 30177 Accepted: 9013
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. 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
Source

POJ Monthly–2007.08.05, Huang, Jinsong

这是一道经典的dfs序问题。

题意:给你一颗树,树上的每个节点有个权值0或1,然后给你m个操作,第一种为修改操作, 把节点v上的权值改变(0变成1,1变成0),第二种操作为询问操作,询问以节点v为根的子树上所有节点上的权值之和(默认整棵树的根为节点1)。

解体思路:先用dfs序把这树映射到一个数组上,然后区间求和用树状数组维护一下就行.

简单说下dfs
dfs就是把一棵树的每个节点重新编号,然后就可以放在一个数据结构中维护了,那么怎样编号呢?,用dfs序实现编号的过程代码如下:

void dfs(int u, int f){    L[u] = cnt++;    for(int i = head[u]; i != -1; i = Edge[i].last)    {        int v = Edge[i].v;        if(v == f) continue;        dfs(v, u);    }    R[u] = cnt - 1;}

L[i]表示节点i进入的时间戳,R[i]表示节点i离开的时间戳,所以这样处理之后,以i为根的子树的子节点都在L[i], R[i]之中,然后也就是一棵子树上所有的节点在编号之后是连续的一段区间,根节点在最前,然后就可以区间维护了。

#include<stdio.h>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<vector>using namespace std;const int maxn = 1e5 + 10;int N, q;int Tree[maxn];int a[maxn];int L[maxn], R[maxn];int head[maxn];int cnt;//vector<int> g[maxn];int res;struct edge{    int u, v;    int last;}Edge[2 * maxn];void add(int u, int v){    Edge[res].u = u;    Edge[res].v = v;    Edge[res].last = head[u];    head[u] = res++;}int lowbit(int x){    return x&(-x);}void update(int loc, int value){    for(int i = loc; i <= N; i += lowbit(i))    {        Tree[i] += value;    }}int sum(int loc){    int ans = 0;    for(int i = loc; i >= 1; i -= lowbit(i))    {        ans += Tree[i];    }    return ans;}void init(){    memset(Tree, 0, sizeof(Tree));    //memset(a, 1, sizeof(a));    memset(L, 0, sizeof(L));    memset(R, 0, sizeof(R));    memset(head, -1, sizeof(head));    cnt = 1;    res = 0;    for(int i = 1; i <= N; i++)    {        a[i] = 1;        update(i, 1);    }//    for(int i = 1; i <= N; i++)//    {//        g[i].clear();//    }}void dfs(int u, int f){    L[u] = cnt++;    for(int i = head[u]; i != -1; i = Edge[i].last)    {        int v = Edge[i].v;        if(v == f) continue;        dfs(v, u);    }    R[u] = cnt - 1;}int main(){    scanf("%d", &N);    int u, v;    init();    for(int i = 1; i < N; i++)    {        scanf("%d%d", &u, &v);        add(u, v);        //g[v].push_back(u);    }    dfs(1, 1);    scanf("%d", &q);    char op[10];    for(int i = 1; i <= q; i++)    {        scanf("%s %d", op, &v);        if(op[0] == 'C')        {            if(a[L[v]] == 1)            {                a[L[v]] = 0;                update(L[v], -1);            }            else            {                a[L[v]] = 1;                update(L[v], 1);            }        }        else        {            int s1 = sum(R[v]);            int s2 = sum(L[v] - 1);            printf("%d\n", s1 - s2);        }    }    return 0;}
原创粉丝点击