/*
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. jquery datepicker 只显示年月

    首先修改默认日期赋值 <script type="text/javascript"> $(function(){ $('#searchDate').datepicker ...

  2. Lua与C++互相调用(上)

    int main1(int argc, const char * argv[]) { lua_State* L = luaL_newstate();//创建栈 luaopen_base(L); lua ...

  3. android IOC框架学习记录

    一.框架如下几种: 1.Roboguice   2.Spring for Android   3.afinal   4.xUtils   二.Roboguice说明 项目地址:https://gith ...

  4. 套题 codeforces 361

    A题((Mike and Cellphone) 看起来好像需要模拟数字键位的运动,可是,只要判断出那些必然YES的数字组合不就好了么 #include <cstdio> #include ...

  5. 关于Mysql查询带单引号及插入带单引号字符串问题

    1.转为带参数查询 String sql=""select id from student where name='?'; Connection connect = DriverM ...

  6. 解决web中的乱码

    统一使用utf-8进行编码数据库的编码格式也是utf-8 对于页面post传过来的不会出现乱码 对于页面get 传过来值解决乱码 方法一:在业务层:userName = new String(user ...

  7. 【腾讯Bugly干货分享】深入理解 ButterKnife,让你的程序学会写代码

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/578753c0c9da73584b025875 0.引子 话说我们做程序员的,都 ...

  8. Java虚拟机2:Java内存区域及对象

    几个计算机的概念 为以后写文章考虑,也为巩固自己的知识和一些基本概念,这里要理清楚几个计算机中的概念. 1.计算机存储单位 从小到大依次为位Bit.字节Byte.千字节KB.兆M.千兆GB.TB,相邻 ...

  9. AMD加载器实现笔记(二)

    AMD加载器实现笔记(一)中,我们实现了一个简易的模块加载器.但到目前为止这个加载器还并不能称为AMD加载器,原因很简单,我们还不支持AMD规范中的config配置.这篇文章中我们来添加对config ...

  10. ASP.NET-自定义HttpModule与HttpHandler

    在之前的ASP.NET是如何在IIS下工作的这篇文章中介绍了ASP.NET与IIS配合工作的机制,在http请求经过一系列处理后,最后到达ASP.NET管道中,这时,就是Http Modules和Ht ...