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 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
随机推荐
- monodevelop 突然莫名其妙的将 warning 全部标记为 error
这是一个关于 monodevelop 的“坑” 我们在用 monodevelop 编译游戏脚本时, 通常会有一些警告,一般这些警告都是无害的, 不影响游戏运行.可是突然有一天, monodevelop ...
- Hyper-V初涉_共享式网络链接
任何一台计算机,如果不能与网络连通,可以说已经失去了大部分的功能,Windows 8尤是如此,虚拟机亦是如此. Hyper-V并不能对物理机的网卡进行识别,所以需要借助虚拟网卡通过物理机的网络共享实现 ...
- day9---paramiko ssh ftp
安装 paramiko模块 win下: 进入到\Python35\Scripts> 执行:pip install paramiko Linux: 先升级下pip : pip3.5 install ...
- Swap Swap,即交换分区
Swap,即交换区,除了安装Linux的时候,有多少人关心过它呢?其实,Swap的调整对Linux服务器,特别是Web服务器的性能至关重要.通过调整Swap,有时可以越过系统性能瓶颈,节省系统升级费用 ...
- MYSQL删除表的记录后如何使ID从1开始
MYSQL删除表的记录后如何使ID从1开始 MYSQL删除表的记录后如何使ID从1开始 http://hi.baidu.com/289766516/blog/item/a3f85500556e2c09 ...
- 逐帧动画(Frame-by-frame Animations)
1.这一类动画可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示. xml定义方法 <animation-list xmlns:android=" ...
- 对文本行进行排序,新增-d(目录排序),只对字母数字空格排序(TCPL 练习5-16)
文本行的排序用到了命令行参数以及多级指针,在要求只对字母数字空格进行排序时,关键的问题点是兼容-f命令参数,也就是排序的同时忽略大小写.由于在之前的练习中,我将忽略大小写的比较方法重新写了一个函数tr ...
- LINQ LINQ Operators and Lambda Expression - Syntax & Examples
LINQ is a cool feature in C# 3.0. Most of the developers are struggling for the syntax and examples. ...
- 通过cookie实现搜索框内容保存关闭浏览器之前的操作、jq js实现方法
jq实现的方法: jq需要在页面中引入JQ.cookie插件 这是一个超轻量级插件 要实现的效果: 下面是jq代码: $(function(){ var til=$("#orderInfoC ...
- Linux 安装Weblogic12 - copy
# groupadd weblogic# useradd -g weblogic weblogic# passwd weblogic# mkdir -p /var/bea# chown -R webl ...