您的位置:首页 > 其它

ACM: 树状数组 poj 3321 图论+树状…

2016-05-19 23:24 489 查看
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 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

题意: 一棵苹果树上每个分叉点都最多一个苹果或则没有, 现在有两种操作:(节点编码1~n)

       Q
x: 访问以编号x为根的子树包括x的所有苹果和;

      
C x: 改变节点x的状态(反转, 0->1, 1->0);

解题思路:

     
1. 这题想了整整一天, 题目的难点: 每次修改树上的节点就遍历这棵树, 这样不实际. 还有统计

        
某子树的苹果和时, 同样遍历的方法, 效率太低了. 用树状数组可以解决线性序列的求和问题.

        
但是这次题目的是以树形式.

     
2. 状态改变, 因为每个节点用深搜遍历的时候, 都会出现2次, 一来一回2次可以统计出它们的序号

        
这个序号相当于这棵子树的范围, 这样一来重新对树上的每个子树编码, 符合树状数组.

代码:

#include <cstdio>

#include <iostream>

#include <cstring>

using namespace std;

#define MAX 100005

struct node

{

 int v;

 int next;

}edges[MAX*2];

int n, m;

int a[MAX], b[MAX]; //记录2次dfs得到范围

int c[MAX*2], g[MAX*2], count; //c树状数组, g树杈状态1 or -1

int first[MAX*2], num;

char ch[2];

int temp;

inline void addedges(int u, int v)

{

 edges[num].v = v;

 edges[num].next = first[u];

 first[u] = num++;

}

inline int lowbit(int x)

{

 return x & (x^(x-1));

}

void read_graph()

{

 num = 0;

 memset(first, -1, sizeof(first));

 int u, v;

 for(int i = 1; i < n; ++i)

 {

  scanf("%d
%d",&u, &v);

  addedges(u, v);

  addedges(v, u);

 }

}

void update(int x, int val)

{

 for(int i = x; i <= 2*n; i +=
lowbit(i))

  c[i] += val;

}

int getSum(int x)

{

 int sum = 0;

 for(int i = x; i > 0; i -=
lowbit(i))

  sum += c[i];

 return sum;

}

void dfs(int root)

{

 if( !a[root] )

 {

  a[root] = ++count;

  for(int i = first[root]; i !=
-1; i = edges[i].next)

   dfs(edges[i].v);

  b[root] = ++count;

 }

}

int main()

{

 int i;

// freopen("input.txt", "r", stdin);

 while(scanf("%d", &n) !=
EOF)

 {

  read_graph();

  for(i = 1; i <=
n; ++i)

  {

   g[i] =
1;

   update(i,
1);

   update(i+n,
1);

  }

  count = 0;

  memset(a, 0, sizeof(a));

  memset(b, 0, sizeof(b));

  dfs(1);

  

  scanf("%d",&m);

  for(i = 1; i <=
m; ++i)

  {

   scanf("%s",ch);

   if(ch[0] ==
'Q')

   {

    scanf("%d",&temp);

    int
result = getSum(b[temp])-getSum(a[temp])+((1+g[temp])/2);

    printf("%d\n",result/2);

   }

   else if(ch[0]
== 'C')

   {

    scanf("%d",&temp);

    update(a[temp],
-g[temp]);

    update(b[temp],
-g[temp]);

    g[temp]
= -g[temp];

   }

  }

 }

 return 0;

}

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: