/*
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;
}
}
else
printf("%d\n", getSum(end[f])-getSum(start[f]-1));
}
}
return 0;
}
/*
这道题利用二维数组建图也可以过,不过数组的大小还真是难以捉摸....
*/
#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;
}
}
else
printf("%d\n", getSum(end[f])-getSum(start[f]-1));
}
}
return 0;
}

  

  

poj 3321 Apple Trie的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

    id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...

  3. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  4. POJ 3321 Apple Tree 【树状数组+建树】

    题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...

  5. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  6. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  7. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  8. poj 3321 Apple Tree(一维树状数组)

    题目:http://poj.org/problem?id=3321 题意: 苹果树上n个分叉,Q是询问,C是改变状态.... 开始的处理比较难,参考了一下大神的思路,构图成邻接表 并 用DFS编号 白 ...

  9. POJ 3321 Apple Tree dfs+二叉索引树

    题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...

随机推荐

  1. ORA-00060:等待资源时检测到死锁的一种处理方法

    先执行脚本: SELECT p.sipid, a.serial#, c.object_name, b.session_id, b.oracle_username, b.os_user_name FRO ...

  2. Ubuntu/Linux 下pdf阅读器Zathura(类vim操作)

    Ubuntu下源安装: sudo apt-get install zathura 操作总结: 基本操作与vim一致,对于熟悉vim快捷键的十分方便: 向下移动一页是J(Ctrl+f),向上移动一页是K ...

  3. 地图、定位 CLLocationManager CLGeocoder CLPlacemark

    地图.定位 一.基本知识点 定位: 1.info.plist文件设置 ios8以后,使用定位需要在info.plist文件中添加两个字段NSLocationAlwaysUsageDescription ...

  4. C++模板&泛型编程

    ---恢复内容开始--- 一.泛型编程 定义:编写与类型无关的逻辑代码,是代码复用的一种手段.模板是泛型编程的基础 模板分为:函数模板和类模板 函数模板:代表了一个函数家族,该函数与类型无关,在使用时 ...

  5. C++混合编程之idlcpp教程Python篇(5)

    上一篇在这  C++混合编程之idlcpp教程Python篇(4) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程PythonTutorial3中,同样加入了三个文件:Py ...

  6. 让ZenCoding提升编码速度

    日前写了一篇关于VS神级插件Web Essentials的系列博客,其中在HTML&CSS操作技巧一节简单提到了ZenCoding,今天来详细说一下这个东西. 摘要 Zen Coding是一种 ...

  7. STC12C5A60S2笔记5(省电模式)

    1. 基本特性 STC12C5A60S2系列单片机可运行三种省电模式以降低功能,STC正常工作电流是2mA~7mA,而掉电模式下<0.1uA,空闲模式下<0.1mA. 1) 空闲模式:由电 ...

  8. 简介Gulp, Grunt, Bower, 和 Npm 对Visual Studio的支持

    [原文发表地址]Introducing Gulp, Grunt, Bower, and npm support for Visual Studio Web 开发,特别是前端 Web 开发,正迅速变得像 ...

  9. Linux 数组

    200 ? "200px" : this.width)!important;} --> :介绍 在shell4.0之后支持普通数组和关联数组,普通数组只能使用整数作为索引,关 ...

  10. [专业名词·硬件] 2、DC\DC、LDO电源稳压基本常识(包含基本原理、高效率模块设计、常见问题、基于nRF51822电源管理模块分析等)·长文

    综述先看这里 第一节的1.1简单介绍了DC/DC是什么: 第二节是关于DC/DC的常见的疑问答疑,非常实用: 第三节是针对nRF51822这款芯片电源管理部分的DC/DC.LDO.1.8的详细分析,对 ...