POJ3221——Apple Tree(线段树,dfs序)
2016-07-25 21:20
393 查看
Apple Tree
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
题意:一棵树,每个节点上有一颗苹果。有查询和更改两种操作,查询就是查询该节点的字树上有几个苹果,更改就是更改某个节点上面的苹果树。0-1或者1-0.
思路:树上的查询和更改操作很容易想到dfs序,用vector建图会TML,用前向星建图。然后再求一下区间和就行。求区间和可以用树状数组线段树,个人习惯线段树,虽然代码要长一些。
有几个地方mark一下。
什么是dfs序?一棵树比如图上这颗,用dfs遍历,访问到的节点依次是,1 3 4 4 5 5 3 2 2 1(可能根据建图方式不同序列有所不同),但不难发现,因为我们分别记录下了两次访问到的节点位置。所以两次访问之间包含了子树所有节点(分别各两次)。
那么我们就可以通过线段树维护区间和,在每次查询的时候给出子树上的苹果数量。
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
#define MAXN 100010
#define LEN 200010
#define INF 1e9+7
#define pi acos(-1)
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int MODE=1000000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int sum[MAXN<<3];
int que[MAXN<<2];
int head[MAXN<<2];
int st[MAXN<<2];
int en[MAXN<<2];
int num=0;
int cnt=0;
int n,m;
struct edge{
int v,next;
};
edge G[MAXN];
void add(int u,int v)
{
G[cnt].v=v;
G[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int u)
{
st[u]=++num;
que[num]=u;
for(int k=head[u];k!=-1;k=G[k].next)
{
int v=G[k].v;
dfs(v);
}
en[u]=++num;
que[num]=u;
}
void PushUp(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
if(l==r){
sum[rt]=1;
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int p,int l,int r,int rt)
{
if(l==r){
if(sum[rt])
sum[rt]=0;
else
sum[rt]=1;
return;
}
int m=(l+r)>>1;
if(p<=m)
update(p,lson);
else
update(p,rson);
PushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return sum[rt];
int m=(l+r)>>1;
int ret=0;
if(L<=m)
ret+=query(L,R,lson);
if(R>m)
ret+=query(L,R,rson);
return ret;
}
int main()
{
memset(head,-1,sizeof(head));
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
scanf("%d",&m);
getchar();
dfs(1);
build(1,num,1);
while(m--)
{
char op[2];
scanf("%s",op);
if(op[0]=='C')
{
int t;
scanf("%d",&t);
update(st[t],1,num,1);
update(en[t],1,num,1);
}
if(op[0]=='Q')
{
int t;
scanf("%d",&t);
int res=query(st[t],en[t],1,num,1);
res/=2;
printf("%d\n",res);
}
}
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25851 | Accepted: 7665 |
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
题意:一棵树,每个节点上有一颗苹果。有查询和更改两种操作,查询就是查询该节点的字树上有几个苹果,更改就是更改某个节点上面的苹果树。0-1或者1-0.
思路:树上的查询和更改操作很容易想到dfs序,用vector建图会TML,用前向星建图。然后再求一下区间和就行。求区间和可以用树状数组线段树,个人习惯线段树,虽然代码要长一些。
有几个地方mark一下。
什么是dfs序?一棵树比如图上这颗,用dfs遍历,访问到的节点依次是,1 3 4 4 5 5 3 2 2 1(可能根据建图方式不同序列有所不同),但不难发现,因为我们分别记录下了两次访问到的节点位置。所以两次访问之间包含了子树所有节点(分别各两次)。
那么我们就可以通过线段树维护区间和,在每次查询的时候给出子树上的苹果数量。
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
#define MAXN 100010
#define LEN 200010
#define INF 1e9+7
#define pi acos(-1)
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int MODE=1000000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int sum[MAXN<<3];
int que[MAXN<<2];
int head[MAXN<<2];
int st[MAXN<<2];
int en[MAXN<<2];
int num=0;
int cnt=0;
int n,m;
struct edge{
int v,next;
};
edge G[MAXN];
void add(int u,int v)
{
G[cnt].v=v;
G[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int u)
{
st[u]=++num;
que[num]=u;
for(int k=head[u];k!=-1;k=G[k].next)
{
int v=G[k].v;
dfs(v);
}
en[u]=++num;
que[num]=u;
}
void PushUp(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
if(l==r){
sum[rt]=1;
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int p,int l,int r,int rt)
{
if(l==r){
if(sum[rt])
sum[rt]=0;
else
sum[rt]=1;
return;
}
int m=(l+r)>>1;
if(p<=m)
update(p,lson);
else
update(p,rson);
PushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return sum[rt];
int m=(l+r)>>1;
int ret=0;
if(L<=m)
ret+=query(L,R,lson);
if(R>m)
ret+=query(L,R,rson);
return ret;
}
int main()
{
memset(head,-1,sizeof(head));
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
scanf("%d",&m);
getchar();
dfs(1);
build(1,num,1);
while(m--)
{
char op[2];
scanf("%s",op);
if(op[0]=='C')
{
int t;
scanf("%d",&t);
update(st[t],1,num,1);
update(en[t],1,num,1);
}
if(op[0]=='Q')
{
int t;
scanf("%d",&t);
int res=query(st[t],en[t],1,num,1);
res/=2;
printf("%d\n",res);
}
}
}
相关文章推荐
- 42.在应用中打开别的应用
- CoordinatorLayout源码解析,探索Behavior机制的奥秘
- Android之用jadx进行反编译
- Android应用逆向——分析反编译代码之大神器
- Android初学之Activity(一)
- Android测试总结
- ART世界探险(7) - 数组
- Android 远程监控摄像头 移动端+PC端 旧手札变废为宝
- onStart和onResume,onPause和onStop的不同--Android开发艺术探索
- cocos2dx 在Xcode里面 resource 里面文件夹的搜索
- Android Developer Document——APP开发深度好文
- Android socket通信 readline方法阻塞
- Android Espresso浅谈
- Android monkeyrunner整理
- Android之mediarecorder中的方法以及工作流程的过程
- 关于Cocos2d-x中让主角运动的方法
- Toolbar:工具条,SearchView:搜索视图
- android基于zBar扫一扫案例
- Android事件分发机制
- cocos2d - JS 工具 - Rect可视化