【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)
題干:
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 1Sample Output
3 2題目大意:
給出一個(gè)蘋果樹,每個(gè)節(jié)點(diǎn)一開始都有蘋果
C X,如果X點(diǎn)有蘋果,則拿掉,如果沒有,則新長出一個(gè)(即:可以用異或處理)
Q X,查詢X點(diǎn)與它的所有后代分支一共有幾個(gè)蘋果
解題報(bào)告:
? ? ?dfs序,然后用線段樹單點(diǎn)更新+區(qū)間查詢一下就可以了。(dfs序是專門用來處理一棵樹的子樹查詢的,需要用線段樹或樹狀數(shù)組維護(hù)查詢)
AC代碼:(用輸入外掛,用時(shí)少了200ms左右,不知道rank1的63ms是怎么做到的)
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; const int MAX = 100000 + 5; int n,m; int cnt,id; int head[MAX*10]; int q[MAX*4],in[MAX*4],out[MAX*4]; struct Edge {int fr;int to;int ne; } e[MAX * 4];//因?yàn)榭赡茈p向邊? struct TREE {int l,r;int val; } tree[MAX * 8]; void add(int u,int v) {e[++cnt].to = v;e[cnt].fr = u;e[cnt].ne = head[u];head[u] = cnt; } void pushup(int cur) {tree[cur].val = tree[cur*2].val + tree[cur*2+1].val; } void build(int l,int r,int cur) {tree[cur].l = l;tree[cur].r = r;if(l == r) {tree[cur].val = 1;return ;//又忘寫return了。。。 }int m = (l+r)/2;//剛開始沒寫build遞歸。。。 build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur); } void dfs(int cur,int root) //cur為當(dāng)前點(diǎn),root為根節(jié)點(diǎn) {q[++id] =cur;in[cur] = id;for(int i = head[cur]; i!=-1;i = e[i].ne) {dfs(e[i].to,cur);}out[cur] = id; } void update(int tar,int cur) {if(tree[cur].l == tree[cur].r) {tree[cur].val ^= 1;return ; }if(tar <= tree[cur*2].r) update(tar,cur*2);//剛開始寫成tree[cur*2].l了else update(tar,cur*2+1);pushup(cur); } int query(int pl,int pr,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) {return tree[cur].val;}int res = 0;if(pl <= tree[cur*2].r) res += query(pl,pr,cur*2);if(pr >= tree[cur*2+1].l) res += query(pl,pr,cur*2+1);return res; } int read() {int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f; } int main() {cin>>n;char op[10];int x;cnt = id = 0;memset(head,-1,sizeof head);int u,v;for(int i = 1; i<n; i++) {//scanf("%d%d",&u,&v);u = read();v = read();add(u,v);}dfs(1,0);//這個(gè)0 是沒用的 build(1,n,1); // printf("**********\n"); // for(int i = 1; i<=5; i++) { // printf("%d| ",tree[i].val); // } // printf("\n****************\n");scanf("%d",&m);while(m--) {scanf("%s",op);if(op[0] == 'C') {//scanf("%d",&x);x = read();update(in[x],1);}else {//scanf("%d",&x);x = read();int ans = query(in[x],out[x],1);printf("%d\n",ans);}} return 0 ;}總結(jié):
? ?小錯(cuò)誤還是很多啊。。。
總結(jié)
以上是生活随笔為你收集整理的【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日元汇率暴跌 iPhone 13手机便宜
- 下一篇: 招商英雄联盟信用卡积分 玩游戏赢积分