poj 3321 Apple Tree(树状数组+dfs)中等难度

2023-11-08 11:48

本文主要是介绍poj 3321 Apple Tree(树状数组+dfs)中等难度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、http://poj.org/problem?id=3321

2、题目大意:

有一棵树,有n个叉,由n-1条枝条连接,每个叉上最多有一个苹果,假设原来每个叉上都有苹果,现在要对这棵苹果树进行m个操作,其中这m个操作由两种操作组成,C x表示如果x点有苹果就摘掉,没有苹果就长一个,Q x是查询以x结点为根的树有多少个苹果,这道题目看上去好像是普通的树状数组的题目,但是没有顺序,我们需要先按照根搜索一遍,确定每棵子树的起始点和结束点,这样就可以转换成树状数组的题目了,

这道题目有个问题,用vector建树会超时,需要静态建树

3、题目:

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17308 Accepted: 5255

Description

There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.

The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.

The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?

Input

The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

Output

For every inquiry, output the correspond answer per line.

Sample Input

3
1 2
1 3
3
Q 1
C 2
Q 1

Sample Output

3
2

Source

POJ Monthly--2007.08.05, Huang, Jinsong

 

4、Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100005
int s[N];
int e[N];
int cnt,n,edge=0;
int visit[N];
int c[N];
int v[N],next[N],head[N];
void add(int x,int y)
{v[edge]=y;next[edge]=head[x];head[x]=edge++;
}
void dfs(int u)
{s[u]=++cnt;for(int i=head[u];i!=-1;i=next[i]){int vv=v[i];dfs(vv);}e[u]=cnt;
}
int lowbit(int i)
{return i&(-i);
}
void update(int x,int v)
{for(int i=x; i<=n; i+=lowbit(i)){c[i]+=v;}
}
int getsum(int x)
{int sum=0;for(int i=x; i>0; i-=lowbit(i)){sum+=c[i];}return sum;
}
int main()
{int a,b,m;char ch[3];while(scanf("%d",&n)!=EOF){cnt=0;edge=0;memset(head,-1,sizeof(head));memset(c,0,sizeof(c));memset(visit,0,sizeof(visit));for(int i=1;i<=n;i++){update(i,1);}for(int i=1;i<n;i++){scanf("%d%d",&a,&b);add(a,b);}dfs(1);scanf("%d",&m);for(int i=1; i<=m; i++){scanf("%s%d",ch,&a);if(ch[0]=='Q'){int ans=getsum(e[a])-getsum(s[a]-1);printf("%d\n",ans);}else if(ch[0]=='C'){int v=s[a];if(visit[v]==1){update(v,1);visit[v]=0;}else{update(v,-1);visit[v]=1;}}}}return 0;
}
/*
3
1 2
1 3
3
Q 1
C 2
Q 3
3
1 2
1 3
3
Q 1
C 2
Q 1*/


 

链表建树的超时代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<vector>
#define N 100005
vector<int> adj[N];
int s[N];
int e[N];
int cnt,n;
int visit[N];
int c[N];
void dfs(int v,int f)
{//printf("v=%d\n",v);s[v]=++cnt;for(int i=0; i<adj[v].size(); i++){int vv=adj[v][i];if(vv!=f)dfs(vv,v);}e[v]=cnt;return ;
}
int lowbit(int i)
{return i&(-i);
}
void update(int x,int v)
{for(int i=x; i<=n; i+=lowbit(i)){c[i]+=v;}
}
int getsum(int x)
{int sum=0;for(int i=x; i>0; i-=lowbit(i)){sum+=c[i];// printf("i=%d %d %d\n",i,c[i],sum);}return sum;
}
int main()
{int a,b,m;char ch[3];while(scanf("%d",&n)!=EOF){cnt=0;memset(c,0,sizeof(c));memset(visit,0,sizeof(visit));for(int i=0;i<=n;i++)adj[i].clear();for(int i=1; i<n; i++){scanf("%d%d",&a,&b);//printf("ab=%d %d \n",a,b);adj[a].push_back(b);adj[b].push_back(a);}memset(s,0,sizeof(s));memset(e,0,sizeof(e));dfs(1,-1);scanf("%d",&m);for(int i=1; i<=n; i++){update(i,1);}for(int i=1; i<=m; i++){scanf("%s%d",ch,&a);if(ch[0]=='Q'){//printf("a=%d\n",a);int ans=getsum(e[a])-getsum(s[a]-1);printf("%d\n",ans);}else if(ch[0]=='C'){int v=s[a];if(visit[v]==1){update(v,1);visit[v]=0;}else{update(v,-1);visit[v]=1;}}}}return 0;
}


 

这篇关于poj 3321 Apple Tree(树状数组+dfs)中等难度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/369648

相关文章

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

JAVA数组中五种常见排序方法整理汇总

《JAVA数组中五种常见排序方法整理汇总》本文给大家分享五种常用的Java数组排序方法整理,每种方法结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录前言:法一:Arrays.sort()法二:冒泡排序法三:选择排序法四:反转排序法五:直接插入排序前言:几种常用的Java数组排序

Java数组初始化的五种方式

《Java数组初始化的五种方式》数组是Java中最基础且常用的数据结构之一,其初始化方式多样且各具特点,本文详细讲解Java数组初始化的五种方式,分析其适用场景、优劣势对比及注意事项,帮助避免常见陷阱... 目录1. 静态初始化:简洁但固定代码示例核心特点适用场景注意事项2. 动态初始化:灵活但需手动管理代

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.