zoukankan      html  css  js  c++  java
  • POJ 3321 Apple Tree(dfs序树状数组)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10486

    题意:一颗有n个分支的苹果树,根为1,每个分支只有一个苹果,给出n-1个分支的关系和给出m个操作,Q x表示询问x的子树(包括x)苹果的数量,C x表示若分支x上有苹果,则摘下来,若没有则会生出一个,输出每个询问的值。

    DFS序

    每个子树对应的孩子节点包括根用dfs序存在连续区间中,用树状数组维护区间

      1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cstdlib>
      5 #include <iostream>
      6 #include <queue>
      7 #include <stack>
      8 #include <cmath>
      9 #include <set>
     10 #include <algorithm>
     11 #include <vector>
     12 // #include<malloc.h>
     13 // #include <conio.h>
     14 using namespace std;
     15 #define clc(a,b) memset(a,b,sizeof(a))
     16 #define LL long long
     17 const int inf = 0x3f3f3f3f;
     18 const double eps = 1e-5;
     19 // const double pi = acos(-1);
     20 const LL MOD = 1e8;
     21 const int N=1<<13;
     22 // const LL p = 1e9+7;
     23 void fre() {
     24     freopen("in.txt","r",stdin);
     25 }
     26 // inline int r(){
     27 //     int x=0,f=1;char ch=getchar();
     28 //     while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
     29 //     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
     30 //     return x*f;
     31 // }
     32 const int maxn = 100005;
     33 
     34 struct Edge{
     35      int v,next;
     36 } e[maxn*4];
     37 
     38 int head[maxn],tot,n;
     39 bool vis[maxn];
     40 int sum[maxn*2];
     41 int st[maxn],ed[maxn];
     42 int inx;
     43 
     44 void init(){
     45      for(int i=0;i<maxn*2;i++){
     46          head[i>>1]=-1;
     47          sum[i]=0;
     48          vis[i>>1]=true;
     49      }
     50      tot=0;
     51      inx=0;
     52 }
     53 
     54 void add(int u,int v){
     55      e[tot].v=v;
     56      e[tot].next=head[u];
     57      head[u]=tot++;
     58 }
     59 
     60 void dfs(int rt,int fa){
     61      st[rt]=++inx;
     62      for(int i=head[rt];~i;i=e[i].next){
     63          int v=e[i].v;
     64          if(v==fa) continue;
     65          dfs(v,rt);
     66      }
     67      ed[rt]=++inx;
     68 }
     69 
     70 int lowbit(int x){
     71     return x & -x;
     72 }
     73 
     74 int query(int x){
     75      int ans=0;
     76      while(x>=1){
     77          ans+=sum[x];
     78          x -= lowbit(x);
     79      }
     80      return ans;
     81 }
     82 
     83 void update(int x,int val){
     84      while(x<=inx){
     85          sum[x]+=val;
     86          x+=lowbit(x);
     87      }
     88 }
     89 
     90 int main(){
     91      // fre();
     92      while(~scanf("%d",&n)){
     93          init();
     94          int u,v;
     95          for(int i=0;i<n-1;i++){
     96              scanf("%d%d",&u,&v);
     97              add(u,v);
     98              add(v,u);
     99          }
    100          dfs(1,-1);
    101          for(int i=1;i<=n;i++)
    102              update(st[i],1);
    103          int q;
    104          char c;
    105          int x;
    106          scanf("%d",&q);
    107          while(q--){
    108              getchar();
    109              scanf("%c%d",&c,&x);
    110              if(c=='Q'){
    111                  int ans=query(ed[x])-query(st[x]-1);
    112                  printf("%d
    ",ans);
    113              }
    114              else{
    115                  if(vis[x]){
    116                      update(st[x],-1);
    117                  }
    118                  else
    119                     update(st[x],1);
    120                  vis[x]=!vis[x];
    121              }
    122          }
    123      }
    124      return 0;
    125 }
  • 相关阅读:
    Caffe + Ubuntu 15.04 + CUDA 7.0 新手安装配置指南
    姚斌分布式作业一
    一个简单正则表达式引擎的实现
    学习编程的方法
    [Leetcode]012. Integer to Roman
    [Leetcode]011. Container With Most Water
    JOS lab1 part2 分析
    我的Android Studio配置
    [Leetcode]009.Palindrome Number
    [Leetcode]008.String to Integer (atoi)
  • 原文地址:https://www.cnblogs.com/ITUPC/p/5602271.html
Copyright © 2011-2022 走看看