博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Apple Tree
阅读量:4315 次
发布时间:2019-06-06

本文共 3263 字,大约阅读时间需要 10 分钟。

Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 28060   Accepted: 8343

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

31 21 33Q 1C 2Q 1

Sample Output

32

Source

, Huang, Jinsong
思路:dfs序+树状数组;
dfs序先将序列映射一下,然后树状数组维护下就可以了复杂度n×log(n);
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 using namespace std;11 typedef long long LL;12 int ans[100005];13 int id[100005];14 typedef vector
Ve;15 vector
vec(100005);16 bool flag[100005];17 int cn = 0;18 int l[100005];19 int r[100005];20 void dfs(int n);21 int tree[100005];22 void update(int x,int n,int c);23 int ask(int x);24 int main(void)25 {26 int n;27 memset(flag,0,sizeof(flag));28 scanf("%d",&n);29 for(int i = 0; i < n-1; i++)30 {31 int x,y;32 scanf("%d %d",&x,&y);33 vec[x].push_back(y);34 vec[y].push_back(x);35 }36 dfs(1);37 for(int i = 1; i <= n; i++)38 {39 id[ans[i]] = i;40 }41 for(int i = 1; i <= n; i++)42 update(i,n,1);43 int m;44 memset(flag,0,sizeof(flag));45 scanf("%d",&m);46 while(m--)47 {48 char c[10];49 scanf("%s",c);50 int x;51 scanf("%d",&x);52 if(c[0] == 'Q')53 {54 int ac = ask(r[x]) - ask(l[x]-1);55 printf("%d\n",ac);56 }57 else58 {59 if(!flag[x])60 update(id[x],n,-1),flag[x] = true;61 else update(id[x],n,1),flag[x] = false;62 }63 }64 return 0;65 }66 void dfs(int n)67 {68 flag[n] = true;69 ans[++cn] = n;70 l[n] = cn;71 for(int i = 0; i < vec[n].size(); i++)72 {73 int x = vec[n][i];74 if(!flag[x])75 dfs(x);76 }77 r[n] = cn;78 }79 int ask(int x)80 {81 int sum = 0;82 while(x > 0)83 {84 sum += tree[x];85 x -= x&(-x);86 }87 return sum;88 }89 void update(int x,int n,int c)90 {91 while(x <= n)92 {93 tree[x] +=c;94 x += x&(-x);95 }96 }

 

转载于:https://www.cnblogs.com/zzuli2sjy/p/6412840.html

你可能感兴趣的文章
Java如何在运行时识别类型信息?
查看>>
【环境】新建Maven工程步骤及报错解决方法
查看>>
CSS笔记
查看>>
洛谷 桶哥的问题——送桶——题解
查看>>
python基础语法(二)
查看>>
循环缓冲实现(ring buffer/circular buffer)
查看>>
我说过的那些话
查看>>
matplotlib函数理解
查看>>
java 学习日记---------简易学生信息管理系统
查看>>
jquery 中cache为true与false 的区别
查看>>
Jquery学习之DOM操作
查看>>
linux下ubuntu系统安装及开发环境配置
查看>>
使用混合云,要避开哪些坑?
查看>>
C#文件操作
查看>>
Spring SimpleJdbcTemplate查询示例
查看>>
java设计模式之外观模式(门面模式)
查看>>
经典排序算法
查看>>
给自己的网站加上robots.txt
查看>>
cobbler全自动批量安装部署linux
查看>>
MySQL中Index Merge简介
查看>>