POJ 3321 Ultra-QuickSort(树状数组+DFS序)

    xiaoxiao2025-02-12  21

    思路:有两种操作,如果结点X有苹果那么拿掉,否则加一个苹果,操作二询问以某个结点为根的子树有多少个苹果,显然一开始给出的树的结点编号是无序的不利于维护,那么可以考虑DFS将结点都重新编号,那么一个子树的结点编号一定是连续的,然后用树状数组维护即可

    坑点:会卡STL

    #include<iostream> #include<cstring> #include<vector> #include<cstdio> using namespace std; const int maxn = 100000+5; int c[maxn],in[maxn],out[maxn],cnt=0,a[maxn]; struct Edge{int to,next;}e[maxn<<1]; int edges = 0; int head[maxn]; int lowbit(int x){return x&(-x);} //vector<int>e[maxn]; void addedge(int u,int v) { e[edges].to =v; e[edges].next = head[u]; head[u]=edges++; } void update(int i,int v) { while(i<=maxn) { c[i]+=v; i+=lowbit(i); } } int query(int i) { int ans = 0; while(i) { ans+=c[i]; i-=lowbit(i); } return ans; } void dfs(int u,int fa) { in[u]=++cnt; //for(int i = 0;i<e[u].size();i++) for(int i = head[u];i!=-1;i = e[i].next) { int v = e[i].to; if(v==fa)continue; dfs(v,u); } out[u]=cnt; } int main() { int n; while(scanf("%d",&n)!=EOF) { cnt = 0; edges = 0; memset(head,-1,sizeof(head)); memset(c,0,sizeof(c)); memset(a,0,sizeof(a)); for(int i = 1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } dfs(1,-1); int q; scanf("%d",&q); for(int i = 1;i<=n;i++) update(in[i],1),a[i]=1; while(q--) { char op[2]; int x; scanf("%s%d",op,&x); if(op[0]=='Q') printf("%d\n",query(out[x])-query(in[x]-1)); else if(op[0]=='C') { if(a[x]) { a[x]=0; update(in[x],-1); } else { a[x]=1; update(in[x],1); } } } } }

    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

    3 1 2 1 3 3 Q 1 C 2 Q 1

    Sample Output

    3 2

    转载请注明原文地址: https://ju.6miu.com/read-1296347.html
    最新回复(0)