/*
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. JavaWeb的学习之Servlet(转载自孤傲苍狼)

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  2. AndroidStudio学习笔记-第一个安卓程序

    要带一个本科生做一部分跟安卓有点关系的项目,于是趁着机会学习一下编写安卓程序. 第一篇材料来自谷歌官方,传送门:https://developer.android.com/training/basic ...

  3. Link To Sql简单

    Linq及其扩展 Linq是一种数据查询语言(它能够从多种数据源中查询数据). 现在基于Linq的扩展有: Linq To Object:主要是从内存对象中查询数据 Linq To Sql:主要是从M ...

  4. mobaxterm ssh command

    ssh -qTfnNg -D 7070 demouser@echo.supportedns.com -p 2233

  5. fallacies of distributed computing

    The network is reliable. Latency is zero. Bandwidth is infinite. The network is secure. Topology doe ...

  6. Http规范

    1. 关于 HTTP Basic Authentication http://blog.itpub.net/23071790/viewspace-709367/ 通过以下代码,提示用户登录 Respo ...

  7. 团队项目——站立会议DAY12

    第十二次站立会议记录: 参会人员:张靖颜,钟灵毓秀,何玥,赵莹,王梓萱 项目进展: 1.张靖颜:已经将部分代码完成,对一些模块化的功能进行扩展,对已具备的功能进行完善. 2.钟灵毓秀:对代码进行了修改 ...

  8. 何必苦等VS2015?来看看VS2013下实现移动端的跨平台开发

    前一天准备下载VS2015预览版,到VisualStudio官网一看,发现微软发布了VisualStudio2013的插件——Visual Studio Tools for Apache Cordov ...

  9. ubuntu git 使用

    apt-get install git//ubuntu安装git mkdir -p /var/www/gitProj //创建文件夹 cd /var/www/gitProj //进入文件夹 git i ...

  10. AlwaysON 故障处理之辅助副本磁盘空间不足

    用户反馈AlwaysON辅助副本数据库查询的结果与主库不一致, 远程到服务器后发现数据库的状态显示为“未同步/可疑”, 查看数据库的日志,定位到出现错误的时间点,可以看到提示日志文件所在磁盘的“磁盘空 ...