【POJ 3321】【dfs序(讲解)+(树状数组或者线段树)】Apple Tree【给你一颗树,最初每个节点上都有一个苹果,有两种操作单点修改和查询子树的苹果个数】

来源:互联网 发布:怎么切换mac系统 编辑:程序博客网 时间:2024/06/06 05:11

传送门:POJ 3321

描述:

Apple Tree
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 27128 Accepted: 8048

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
"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
"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

31 21 33Q 1C 2Q 1

Sample Output

32

Source

POJ Monthly--2007.08.05, Huang, Jinsong

题意:

给你一颗树,最初每个节点上都有一个苹果,

有两种操作:修改(即修改某一个节点,修改时这一个节点苹果从有到无,或从无到有)和查询(查询某一个节点他的子树上有多少个苹果)


dfs序讲解:

由于此题数据比较大(N<=10^5),而且不是标准的二叉树,所以这里我们队每一个节点重新用dfs序编号,方便查询子树。


dfs序比较重要的性质:一棵子树的所有节点在dfs序里是连续一段,主要就是利用这个性质来解题

dfs序是处理树上问题很重要的一个武器,主要能够解决对于一个点,它的子树上的一些信息的维护。

下面给一些学习链接:

dfs序详解:http://blog.csdn.net/qq_24489717/article/details/50569644

dfs序基本类型:http://blog.csdn.net/qwe2434127/article/details/49819975

dfs专题小练:http://www.cnblogs.com/liyinggang/p/5922068.html 和 http://blog.csdn.net/Miracle_ma/article/details/51485118


思路一:

dfs序编号之后用线段树来维护,dfs序开的空间就是n,因为只在入的地方时间戳++,出来的地方时间戳不变,线段树的每个节点应该是时间戳

代码一:

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#define rep(i,k,n) for(int i=k;i<=n;i++)#define mst(ss,b) memset(ss,b,sizeof(ss)); #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 using  namespace  std;template<class T> void read(T&num) {    char CH; bool F=false;    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());    F && (num=-num);}int stk[70], tp;template<class T> inline void print(T p) {    if(!p) { puts("0"); return; }    while(p) stk[++ tp] = p%10, p/=10;    while(tp) putchar(stk[tp--] + '0');    putchar('\n');}const int N=1e5+10;struct Edge{  int to,nxt;}e[N << 1];int head[N], tol, c[N];int in[N], out[N];int sum[N<<2];int n, m, cnt;void init(){  cnt=0; tol=0;  mst(head, -1);/*  mst(c, 0);*/}inline void addedge(int u, int v){  e[tol].to = v;   e[tol].nxt = head[u];  head[u] = tol++;}void PushUp(int rt){  sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];}void build(int l, int r, int rt){  if(l == r){    sum[rt] = 1;    return ;   }  int m = (l + r) >> 1;  build(lson);  build(rson);  PushUp(rt);}void update(int p, int l, int r, int rt){  if(l == r){    sum[rt] = sum[rt] ^ 1;    return ;  }  int m = (l + r) >> 1;  if(p <= m) update(p, lson);  else update(p, rson);  PushUp(rt);}int query(int L, int R, int l, int r, int rt){  if(L <= l && r <= R){      return sum[rt];    }    int m=(l + r) >> 1;    int ret = 0;    if(L <= m) ret += query(L, R, lson);    if(R > m) ret += query(L, R, rson);    return ret; }void dfs(int u, int fa){  in[u] = ++cnt;  for(int i=head[u]; i!=-1; i=e[i].nxt){    int v = e[i].to;    if(v == fa)continue;    dfs(v, u);  }  out[u] = cnt;}int  main(){  while(~scanf("%d", &n)){    init();    rep(i, 1, n-1){      int u, v;      read(u),read(v);      addedge(u, v);      addedge(v, u);    }    dfs(1, -1);    build(1, n, 1);    read(m);    while(m--){      char op[2]; int x;      scanf("%s%d", op, &x);      if(op[0] == 'Q')        print(query(in[x], out[x], 1, n, 1));      else update(in[x], 1, n, 1);    }  }  return 0;}


思路二:

dfs序编号之后用树状数组来维护

树状数组直接在第一次出现的位置+1,-1好了,对其他兄弟树没有影响,因为兄弟树是求区间,前面的+1,-1已经抵消掉了 

代码二:

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#define rep(i,k,n) for(int i=k;i<=n;i++)#define mst(ss,b) memset(ss,b,sizeof(ss));  using  namespace  std;template<class T> void read(T&num) {    char CH; bool F=false;    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());    F && (num=-num);}int stk[70], tp;template<class T> inline void print(T p) {    if(!p) { puts("0"); return; }    while(p) stk[++ tp] = p%10, p/=10;    while(tp) putchar(stk[tp--] + '0');    putchar('\n');}const int N=1e5+10;struct Edge{  int to,nxt;}e[N << 1];int head[N], tol, c[N];int in[N], out[N];int have[N];int n, m, cnt;void init(){  cnt=0; tol=0;  mst(head, -1);  mst(c, 0);}inline void addedge(int u, int v){  e[tol].to = v;   e[tol].nxt = head[u];  head[u] = tol++;}void update(int id, int x){  for(int i=id; i<=cnt; i+=i&-i)    c[i] += x;}int query(int id){  int sum=0;  for(int i=id; i>=1; i-=i&-i)    sum += c[i];  return sum;}void dfs(int u, int fa){  in[u] = ++cnt;  for(int i=head[u]; i!=-1; i=e[i].nxt){    int v = e[i].to;    if(v == fa)continue;    dfs(v, u);  }  out[u] = cnt;}int  main(){  while(~scanf("%d", &n)){    init();    rep(i, 1, n-1){      int u, v;      read(u),read(v);      addedge(u, v);      addedge(v, u);    }    dfs(1, -1);/*    for(int i=1;i<=n;i++){      printf("%d %d\n",in[i],out[i]);    }*/    rep(i, 1, n){      have[i] = 1;//起初都有apple      update(in[i], 1);    }    read(m);    while(m--){      char op[2]; int x;      scanf("%s%d", op, &x);      if(op[0] == 'Q')        print(query(out[x]) - query(in[x] - 1));      else{        if(have[x])update(in[x], -1);        else update(in[x], 1);        have[x] = !have[x];      }    }  }  return 0;}



0 0