poj 3321 Apple Trie
/*
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的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- POJ 3321 Apple Tree 【树状数组+建树】
题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- POJ 3321 Apple Tree (树状数组+dfs序)
题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...
- poj 3321 Apple Tree(一维树状数组)
题目:http://poj.org/problem?id=3321 题意: 苹果树上n个分叉,Q是询问,C是改变状态.... 开始的处理比较难,参考了一下大神的思路,构图成邻接表 并 用DFS编号 白 ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
随机推荐
- OpenMP之求和(用section分块完成)
// Sum_section.cpp : 定义控制台应用程序的入口点. //section功能:; //1.指定其内部的代码划分给线程中某个线程,不同的section由不同的线程执行; //2.将一个 ...
- Web项目的发布新手教程
ASP.NET服务器发布新手教程 ——本文仅赠予第一次做Web项目,需要发布的新手们,转载的请注明出处. 首先我们说一下我们的需要的一个环境.我使用的是Visual Studio 2010,版本.NE ...
- ASP.NET 导出gridview中的数据到Excel表中,并对指定单元格换行操作
1. 使用NPOI读取及生成excel表. (1)导出Click事件: 获取DataTable; 给文件加文件名: string xlsxName = "xxx_" + DateT ...
- linux定时执行任务crontab命令用法
linux系统的定时任务是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另外, 由于使用者自己也可以设置计划任务,所 ...
- spark on centos6.5 安装
第一步在centos 6.5上安装java 运行环境 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-18 ...
- markdown语法记录
换行:在结尾加两个空格后回车. 缩进:将输入法切换到全角,两个空格就是两个汉字的大小.
- 快速学习C语言二: 编译自动化, 静态分析, 单元测试,coredump调试,性能剖析
上次的Hello world算是入门了,现在学习一些相关工具的使用 编译自动化 写好程序,首先要编译,就用gcc就好了,基本用法如下 gcc helloworld.c -o helloworld.o ...
- 转帖:Python应用性能分析指南
原文:A guide to analyzing Python performance While it’s not always the case that every Python program ...
- JFinal 1.5 发布,JAVA极速WEB+ORM框架
JFinal 爱好者一直都在问 JFinal 何时再次升级?JFinal 1.5 何时发布?以往升级都保持在每月近两次的频率,为何本次五个月过去了新版本还不出?由于作者暂时阔别码坛已有半年时间,一直无 ...
- CodeIgniter2.2.0-在控制器里调用load失败报错的问题
报错如下: hello A PHP Error was encountered Severity: Notice Message: Undefined property: Test::$load Fi ...