/*poj 3321 Apple Trie这道题的关键是如何将一个树建成一个一维数组利用树状数组来解题!可以利用dfs()来搞定,我们在对一个节点深搜后,所经过的节点的数目就是该节点的子树的数目所以我们利用start[i]数组来记录 i 节点在一维数组的起始位置, 而end[i]则是记录i节点所有孩子节点最后一个孩子节点在数组的位置,那么end[i]-start[i]+1,就是 i 节点(包括自身)和其所有孩子节点的数目。数组建好了,那么最后就是套用树状数组模板进行求解了!
*/
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#define N 100005
using namespace std;
class node
{
public :int k;node *next;node(){next=NULL;}
};node trie[N];
//trie[i]记录的是所有是 i 节点 孩子节点组成的链表的头部
int C[N], num[N];
int start[N], end[N];
int cnt, n;void dfs(int cur)
{start[cur]=cnt;if(trie[cur].next==NULL){end[cur]=cnt;return;}for(node *p=trie[cur].next; p!=NULL; p=p->next)//遍历cur节点的所有孩子节点{++cnt;dfs(p->k);}end[cur]=cnt;//深搜之后得到的cnt值就是cur节点最后一个孩子在一维数组中的位置
}int lowbit(int x)
{return x&(-x);
}void init(int p, int k)
{int i;num[p]=k;for(i=p-lowbit(p)+1; i<=p; ++i)C[p]+=num[i];
}int getSum(int p)
{int s=0;   while(p>0){s+=C[p];p-=lowbit(p);}return s;
}void update(int p, int k)
{while(p<=n){C[p]+=k;p+=lowbit(p);}
}int main()
{int i, u, v, m;char ch[2];int f;while(scanf("%d", &n)!=EOF){cnt=1;memset(C, 0, sizeof(C));for(i=1; i<n; ++i){scanf("%d%d", &u, &v);node *p=new node();p->k=v;p->next=trie[u].next;trie[u].next=p;}dfs(1);for(i=1; i<=n; ++i)init(i, 1);scanf("%d", &m);while(m--){scanf("%s%d", ch, &f);if(ch[0]=='C'){if(num[f]==1){update(start[f], -1);num[f]=0;}else{update(start[f], 1);num[f]=1;}}elseprintf("%d\n", getSum(end[f])-getSum(start[f]-1));}}return 0;
}<br><br>
/*<br>   这道题利用二维数组建图也可以过,不过数组的大小还真是难以捉摸....<br>*/<br>#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#define N 100005
using namespace std;
int node[N][100];
int C[N], num[N];
int start[N], end[N];
int cnt, n;void dfs(int cur)
{int sz=node[cur][0];start[cur]=cnt;if(sz==0){end[cur]=cnt;return;}for(int i=1; i<=sz; ++i){++cnt;dfs(node[cur][i]);}end[cur]=cnt;
}int lowbit(int x)
{return x&(-x);
}void init(int p, int k)
{int i;num[p]=k;for(i=p-lowbit(p)+1; i<=p; ++i)C[p]+=num[i];
}int getSum(int p)
{int s=0;   while(p>0){s+=C[p];p-=lowbit(p);}return s;
}void update(int p, int k)
{while(p<=n){C[p]+=k;p+=lowbit(p);}
}int main()
{int i, u, v, m;char ch[2];int f;while(scanf("%d", &n)!=EOF){cnt=1;for(i=1; i<=n; ++i)node[i][0]=0;memset(C, 0, sizeof(C));for(i=1; i<n; ++i){scanf("%d%d", &u, &v);node[u][++node[u][0]]=v;}dfs(1);for(i=1; i<=n; ++i)init(i, 1);scanf("%d", &m);while(m--){scanf("%s%d", ch, &f);if(ch[0]=='C'){if(num[f]==1){update(start[f], -1);num[f]=0;}else{update(start[f], 1);num[f]=1;}}elseprintf("%d\n", getSum(end[f])-getSum(start[f]-1));}}return 0;
}

poj 3321 Apple Trie相关推荐

  1. 【线段树_DFS序】POJ 3321 Apple Tree

    POJ 3321 Apple Tree 题意:给出一个N个结点的无根树.但是要将1看作根,也相当于是一个有根树.最初每个结点都有一个苹果.有两种操作,Q x: 以x为根的子树上有多少个苹果:C x: ...

  2. POJ 3321 Apple Tree 题解

    POJ 3321 Apple Tree 题解 POJ 3321 题目 There is an apple tree outside of kaka's house. Every autumn, a l ...

  3. poj 3321 Apple Tree(dfs序+树状数组求和模型)

    题目链接:http://poj.org/problem?id=3321 解题思路: 先dfs求出序列,将子树转化到dfs序列的区间内,接下来就是简单的树状数组求和模型了.水题. #include< ...

  4. poj 3321 Apple Tree 树状数组

    http://poj.org/problem?id=3321 一棵树,开始时每个结点都有一个苹果,输入C x表示更新x结点,若x结点有苹果,把该结点苹果摘掉,若该节点无苹果,在该节点上增加一个新的苹果 ...

  5. POJ 3321 Apple Tree【树状数组】

    题目 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tr ...

  6. poj 3321 Apple Tree

    树状数组 题意:一个树,以树枝连接两个点的形式给出,固定以1为整棵树的根.苹果长在树的节点上,节点上只可能0或1个苹果,一开始每个节点都有1个苹果 有两种操作,C表示更改某个节点的苹果数,0变1,1变 ...

  7. 【poj 3321】Apple Tree 题意&题解&代码(C++)

    题目链接: http://poj.org/problem?id=3321 题意: 给你一颗n个节点的树,每个节点开始有一个苹果,然后m次修改,每次修改使得某个节点的苹果改变,有变成没有,没有变成有.询 ...

  8. 【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)

    题干: There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the t ...

  9. 【POJ 3321】Apple Tree(树的dfs序+树状数组)

    传送门 Solution: 我们只需要采用和树链剖分近似的思想--把整个树的dfs序整理出来,排成线型. 这样一个节点的子树肯定是连续的一段,于是乎就可以用树状数组维护单点修改+区间查询的任务了. # ...

最新文章

  1. 如何使用Transformer来做物体检测?
  2. QT Creator 版本大全及下载地址
  3. UVA11462年龄排序
  4. jqgrid mvc_将JQGrid与Spring MVC和Gson集成
  5. mysql 缓解竞争热点_MySQL优化之缓存优化
  6. 【mysql】显式加锁
  7. 网页不显示样式的解决方向之一
  8. JAVA遇见HTML——JSP篇:JSP内置对象(上)
  9. JSON在线序列化网站
  10. 2.4.4 Profile基本参数
  11. CSS写一个实心小圆点的样式
  12. 阿里云gpu服务器计算性能,gpu服务器价格(最新收费标准)
  13. 深入浅出系列之——并查集详解【武侠版】【简单有趣】
  14. gmac网卡驱动1-------mac与phy基础知识
  15. resnet50结构图
  16. Mysql数据库MMM实现高可用架构
  17. 常用hadoop dfs命令
  18. 基于存储的C语言文件操作常规问题分析(文本文件与二进制文件)
  19. 通过虚拟磁盘技术给电脑安装双系统或多系统
  20. 第十四章 使用SQL Shell界面(三)

热门文章

  1. Jboss启动报错之8083 already in use
  2. 树莓派安装oepncv3.43
  3. 支付宝发布黑科技“如影计划”,这真的不是愚人节的玩笑
  4. 补8.python之面相对象part.7(类相关函数的补充)
  5. 【selenium 3】 Mac 下测试环境搭建 Firefox 47+ gecko driver Mac
  6. SQLServer 扫盲
  7. Essential Studio for mobile MVC如何创建一个Razor应用程序平台
  8. 在windows下安装concurrentlua
  9. 云监控状态调查:公有云和混合云的监控成熟度落后于传统数据中心
  10. Eclipse 代码风格配置