POJ 3321 DFS序
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 30636 | Accepted: 9162 |
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
Sample Input
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output
3
2
Source
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; const int maxn = ;
vector<vector<int> > G(maxn); int tot;
int in[maxn];
int out[maxn]; void dfs(int u,int father) {
in[u] = ++tot;
for(int i = ; i < (int)G[u].size(); i ++) {
int v = G[u][i];
if(v==father) continue;
dfs(v,u);
}
out[u] = tot;
} int C[maxn];
int n;
int lowbit(int x) {
return x&-x;
} // A[1] + A[2] + ... + A[x]
int sum(int x) {
int ret = ;
while(x>) {
ret +=C[x];
x-=lowbit(x);
}
return ret;
} // A[x] +=d
void add(int x,int d) {
while(x<=n) {
C[x] +=d;
x +=lowbit(x);
}
} int A[maxn]; int main()
{
scanf("%d",&n); for(int i=; i < n; i++) {
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
} dfs(,); for(int i=; i <= n; i++)
{
A[i] = ;
add(i,);
} int m;
scanf("%d",&m);
char cmd[];
while(m--) {
scanf("%s",cmd);
if(cmd[]=='Q') {
int x;
scanf("%d",&x);
printf("%d\n",sum(out[x])-sum(in[x]-));
}
else {
int x;
scanf("%d",&x);
if(A[x]==) {
add(in[x],-);
A[x] = ;
}
else {
add(in[x],);
A[x] = ;
}
}
} return ;
}
POJ 3321 DFS序的更多相关文章
- POJ 3321 DFS序+线段树
单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4: 5: #include < ...
- Apple Tree POJ - 3321 dfs序列构造树状数组(好题)
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...
- 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序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- 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序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- POJ 3321 Apple Tree(dfs序树状数组)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10486 题意:一颗有n个分支的苹果树,根为1,每个分支只有一个苹果,给出n- ...
- POJ 3321 Apple Tree DFS序 + 树状数组
多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...
随机推荐
- Linux acpi off学习的必要
ACPI是Intel(i386,x86_64,IA64)平台的标准固件规范,绝大部分OS需要从BIOS得到的信息都可以从ACPI得到,并且现在的趋势是未来的任何新的特性相关的信息都只能从ACPI得到. ...
- html5使用local storage存储的数据在本地是以何种形式保存的
html5使用local storage存储的数据是如何保存在本地的?(我使用的是chrome浏览器,chrom浏览器是用sqlite来保存本地数据的) Html5 的local storage 是通 ...
- MongoChef
简介 开源且免费,有商业版 可自动化生成查询语句 使用 最下面的 _id 是自动生成的,手动指定 { .0, "_id" : ObjectId("58 ...
- java NIO之SelectedKey
SelectedKey是channel与Selector绑定的标记,每将一个channel注册到一个selector就会产生一个SelectedKey,并将这个SelectedKey放入到Select ...
- NSTimer_Block封装定时器的target-action成Block回调
前言 定时器NSTimer虽然简单易用,但是目标响应机制(target-action)这种方式很容易在代码中出现代码臃肿的情况,特别是在一个文件中有大量的代码,多个定时器的时候不方便调试,因此将NST ...
- Hashtable 键值对集合
// Hashtable 键值对集合 一个键对应一个值 Hashtable ht=new Hashtable(); ht.Add(,"张三"); ht.Add(,'男'); ht ...
- 系统更新后vs2012无法打开方案资源管理器
系统更新后vs2012无法打开方案资源管理器 vs调试报错: 未找到与约束 ContractName Microsoft.VisualStudio.Language.Intellisense.IGly ...
- 【学习笔记】HTML基础:使用html制作网页
一.初识HTML 1.什么是HTML? Hyper Text Markup Language(超文本标记语言) 扩展XML:Extendsible Markup Language(可扩展性标记语言) ...
- hdu 1394 逆序数(线段树)
http://acm.hust.edu.cn/vjudge/problem/15764 http://blog.csdn.net/libin56842/article/details/8531117 ...
- 多线程篇四:ThreadLocal实现线程范围内变量共享
1.static实现线程范围内变量共享 package com.test.shareData; import java.util.Random; /** * 多线程范围内的数据共享 * @author ...