Apple Tree
| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total Submissions: 19160 |
|
Accepted: 5831 |
Description
There is a apple tree outside of Kaka ' s house. Every autumn, a lot of apples would 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 is connected by branches. Kaka numbers the forks by 1 to N and the root are always numbered by 1. Apples'll grow on the forks and both Apple won ' t grow on the same fork. Kaka wants to know how many apples is there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is, a new Apple may grow in an empty fork some time and Kaka could pick an apple from the tree for his Desse Rt. Can you help Kaka?
Input
The first line contains a integer n ( n ≤100,000), which is the number of the forks In the tree.
the following N -1 lines each contain the 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 be either
" c x strong> "which means the existence of the apple on fork x has been changed. i.e. if there is a apple on the fork and 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 F ork x , including the apple (if exists) on the fork x
Note The tree was full of apples at the beginning
Output
For every inquiry, the output of the correspond answer per line.
Sample Input
33Q 1C 2Q 1
Sample Output
32
———————————————————— Split Line ——————————
Main topic:
Given an apple tree with n nodes, there are 1 apples on each node in the tree. Then give the n-1 edge. There are two kinds of actions:
1. If the node x has an apple, take it off, or the apple will grow
2. Find the number of apples on the subtree with the X node as its root
Ideas:
Transforming a tree structure into a linear structure —————— method Dfs this tree, the node between the first access number of a node and the last accessed number is all child nodes of the subtree that are rooted at that point.
Then you can apply the pity dorado of the bit template to find segments.
Using a linked list to establish a bidirectional edge, node 1 is definitely the root, Dfs.
This question seems to have a bug, the topic gives the range of n is 100,000, but I originally opened 100,000 +10wa, and later changed to 1 million a. It's amazing.
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #define M 1000000+ 10using namespace std;struct pp{int v,w,next;} Edge[m];int tot,head[m],n,vis[m],c[m],app[m],cnt;inline void Addedge (int u,int v,int w,int *h) {edge[tot].v=v; Edge[tot].w=w; Edge[tot].next=h[u]; h[u]=tot++;} struct node{int s,e;} a[m];void dfs (int u) {a[u].s=cnt; Vis[u]=1; for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v; if (Vis[v]) continue; cnt++; DFS (v); } a[u].e=cnt;} void update (int x,int v) {for (int i=x;i<=n;i+=i&-i) {c[i]+=v; }}int getsum (int x) {int sum=0; for (int i=x;i>0;i-=i&-i) {sum+=c[i]; } return sum;} int main () {scanf ("%d", &n); if (n==0) return 0; memset (head,-1,sizeof (head)); memset (vis,0,sizeof (VIS)); tot=0; for (int i=0;i<n-1;++i) {int u,v; scanf ("%d%d", &u,&v); Addedge (U,v,0,head); Addedge (V,u,0,head); } cnt=1; DFS (1); for (int i=1;i<=n;++i) {app[i]=1; } for (int i=1;i<=n;++i) {c[i]=i&-i; } int m,x; scanf ("%d", &m); while (m--) {GetChar (); char Ch;int x; scanf ("%c%d", &ch,&x); if (ch== ' C ') {int v; if (App[x]) {v=-1;app[x]=0;} Else{app[x]=1;v=1;} printf ("\t%d....%d\n", a[x].s,v); Update (A[X].S,V); } if (ch== ' Q ') {printf ("%d\n", Getsum (A[X].E)-getsum (a[x].s-1)); }} return 0;}
POJ 3321--apple trees (tree-like array)