Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23212 | Accepted: 7050 |
Descriptionios
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.app
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?ide
Inputui
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 beginningspa
Outputcode
Sample Inputblog
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #define N 100010 using namespace std; int n,a[N],tot,lef[N],rig[N],vis[N]; vector<vector<int> >v(N); ///最開始用了vector,可是一直不過,百度了某大牛的代碼,才發現和本身有一樣的問題,改爲這樣就過了 int lowbit(int k) { return k&(-k); } int getSum(int i) { int res=0; while(i>0) { res+=a[i]; i-=lowbit(i); } return res; } void add(int i) { while(i<=n) { a[i]++; i+=lowbit(i); } } void eat(int i) { while(i<=n) { a[i]--; i+=lowbit(i); } } void dfs(int i) ///深搜對於每個節點進行編號 { lef[i]=tot; for(int j=0;j<v[i].size();j++) { tot++; dfs(v[i][j]); } rig[i]=tot; } int main() { int m; while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); memset(lef,0,sizeof(lef)); memset(rig,0,sizeof(rig)); memset(a,0,sizeof(a)); for(int i=0; i<N; i++) v[i].clear(); for(int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); v[x].push_back(y); } tot=1; dfs(1); for(int i=1;i<=n;i++) { vis[i]=1; add(i); } for(int i=1;i<=n;i++) { cout<<lef[i]<<" "<<rig[i]<<endl; } scanf("%d",&m); while(m--) { char op[5]; int q; scanf("%s%d",op,&q); if(!strcmp(op,"Q")) printf("%d\n",getSum(rig[q])-getSum(lef[q]-1)); if(!strcmp(op,"C")) { if(vis[q]) eat(lef[q]); else add(lef[q]); vis[q]=!vis[q]; } } } }
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Outputip
3 2
題目大意:一棵蘋果樹上有n個蘋果,每一個叉上都最多隻能有1個蘋果,1爲根節點。如今有2種操做,Q爲求以某一節點爲根節點的子樹上全部蘋果的數量和。C是更新某一點上的蘋果數量,若是原先爲0則長出來一個蘋果,不然則被吃掉。get
思路:咱們發現本題中的樹爲一個多叉樹,而樹的先序遍歷即dfs則能夠知道以某一節點爲根節點的子樹所包含的全部節點編號。因此咱們用一個left和right數組記錄i號蘋果在先序遍歷下的子樹中的最小編號以及最大編號,即它的祖先和本身子樹中的全部編號的最大值,而後求和時候用getSum(right[i])-getSum(left[i]-1)便可。
代碼: