POJ 3321 Apple Tree(树状数组)
题意 : 大概是说一颗树有n个分岔,然后给你n-1对关系,标明分岔u和分岔v是有边连着的,然后给你两个指令,让你在Q出现的时候按照要求输出。
思路 :典型的树状数组。但是因为没有弄好数组,所以要用DFS先映射一下,好吧我承认我说不下去了,六级没过,CF又掉了100多分,脑子完全不转转了。。。。。。
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std; const int maxn = ;
int head[maxn],start[maxn] ,num[maxn],data[maxn];
int m,n,cnt ,cntt;
bool vis[maxn] ; struct node
{
int l,r ;
int next ;
}Node[maxn] ; void addegde(int u,int v)
{
Node[cnt].l = u ;
Node[cnt].r = v ;
Node[cnt].next = head[u] ;
head[u] = cnt++ ;
} void dfs(int u)
{
start[u] = ++ cntt ;
vis[u] = true ;
for(int i = head[u] ; i+ ; i = Node[i].next)
{
int v = Node[i].r ;
if(!vis[v])
dfs(v) ;
}
num[u] = cntt ;
}
int lowbit(int x)
{
return x&(-x) ;
}
int sum(int i )
{
int summ = ;
while(i > )
{
summ += data[i] ;
i -= lowbit(i) ;
}
return summ ;
} void update(int i,int val)
{
while(i <= n)
{
data[i] += val ;
i += lowbit(i) ;
}
} int main()
{
while(~scanf("%d",&n))
{
cnt = cntt = ;
memset(head,-,sizeof(head)) ;
memset(num,,sizeof(num)) ;
memset(start,,sizeof(start)) ;
memset(data,,sizeof(data)) ;
memset(vis,false,sizeof(vis)) ;
int x,y ;
for(int i = ; i <= n- ; i++ )
{
scanf("%d %d",&x,&y) ;
addegde(x,y) ;
}
dfs() ;
for(int i = ; i <= n ; i++)
update(i,) ;
scanf("%d",&m) ;
getchar() ;
for(int j = ; j <= m ; j++)
{
char ch ;
scanf("%c",&ch) ;
if(ch == 'Q')
{
scanf("%d",&x) ;
printf("%d\n",sum(num[x])-sum(start[x]-)) ;
}
else if(ch == 'C')
{
scanf("%d",&x) ;
if(sum(start[x])-sum(start[x]-))
update(start[x],-) ;
else
update(start[x],) ;
}
getchar() ;
}
}
return ;
}
POJ 3321 Apple Tree(树状数组)的更多相关文章
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- 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 树状数组+DFS
题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...
- POJ 3321 Apple Tree 树状数组 第一题
第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
- 3321 Apple Tree 树状数组
LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
- E - Apple Tree(树状数组+DFS序)
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...
- POJ3321 Apple Tree(树状数组)
先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...
- POJ 2486 Apple Tree [树状DP]
题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...
随机推荐
- 深入解析hasOwnProperty与isPrototypeOf
这里采用一个实例来说明: function Person(name) { //以下都是Person的OwnProperty this.name = name; this.showMe = functi ...
- HTTP和HTTPS详解
http://blog.csdn.net/mingli198611/article/details/8055261/ 转自:http://www.cnblogs.com/ok-lanyan/archi ...
- iOS-学习路线图(推荐)
在学习一个新的知识时,除了保持积极的态度.对知识的渴望,学习路线以及方法也是很重要的.在学习iOS的时候,遇到这样的情况,非常想去学习,提高,但是没有一个学习路线,不知道从哪里入手,该先学什么.在学什 ...
- asp.net 邮件发送类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 转:EF调用存储过程、函数
EF调用存储过程.函数 2014-04-02 09:12:20| 分类: ORM框架|举报|字号 订阅 一.ef4.1 codeFirst 修改表结构 增加字段等 EF code ...
- 在 Eclipse 中使用 JSHint 检查 JavaScript 代码
JSHint Home: http://www.jshint.com/ JSHint Options: http://www.jshint.com/options/ JSHint For Eclips ...
- QT/C++ 智能指针
什么是智能指针? 为什么用智能指针? 还有哪些关于内存管理方面的知识点,需要注意的?
- 创建Unity新项目并编译成游戏程序
注:本人所使用的Unity版本为:Unity5.3.5f1,所使用的VS版本为:Visual.Studio.2013.Ultimate 折腾了快一个月了,终于有时间做自己的啦,哈哈: ) 步骤一:启动 ...
- DataGridView如何快速导出Excel
从DataGridView或DataTable导出Excel文件,为了按照数据类型设置单元格格式,导出Excel时速度都比较慢,一直找不到好的办法. 最后从外文网站上找到解决办法,使用ws.get_R ...
- centos 6.4 samba 权限 selinux权限配置
http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/26/3100444.html(参考) SELINUX 策略 配置好samba后, 输入 ...