poj 3321:Apple Tree(樹狀數組,提高題)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   strong   

Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18623   Accepted: 5629

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
"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
"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

POJ Monthly--2007.08.05, Huang, Jinsong

 

  樹狀數組,提高題

  有些難度,要轉一下彎。

  題意

  一開始給你n-1個邊,構成一顆蘋果樹,預設這個時候每個分叉上都有蘋果。接下來有q個操作,這些操作分兩種,要不有蘋果摘走蘋果,沒蘋果長出蘋果,要不求某一分叉上所有的蘋果個數。

  思路

  關鍵是在每一個分叉處記錄一個開始時間,和結束時間,表示dfs時經過這個分叉的時間和回到這個分叉的時間。這裡的時間其實是dfs遍曆次序的定位。這樣就相當於有了一個區間,用樹狀數組就可以求出這個分叉點上的所有蘋果的個數。

  注意

  給你的n-1條邊,是雙向的,即a指向b,b也指向a,是無向圖。

  測試資料(來自poj討論版)

102 13 13 44 74 57 95 86 510 610Q 1C 5Q 3Q 4C 5Q 6C 6Q 8C 9Q 1ans:1076218

 

  代碼

 1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4  5 #define MAXN 100010 6  7 int N; 8 int cnt=0; 9 int c[MAXN];10 int start[MAXN];11 int end[MAXN];12 13 struct Node{ 14     int num;15     Node* next;        //孩子節點16     Node()17     {18         next = NULL;19     }20 }tree[MAXN];    //臨界表21 22 int lowbit(int x)23 {24     return x & (-x);25 }26 27 void add(int d,int x)28 {29     while(d<=N){30         c[d] += x;31         d += lowbit(d);32     }33 }34 35 int sum(int d)36 {37     int ans =0;38     while(d>=1){39         ans += c[d];40         d -= lowbit(d);41     }42     return ans;43 }44 45 void dfs(int v)    //以r為根節點進行dfs遍曆,返整個遍曆之後的時間46 {47     start[v] = ++cnt;48     Node* p = tree[v].next;49     while(p){50         if(start[p->num]==0)51             dfs(p->num);52         p = p->next;53     }54     end[v] = cnt;55 }56 57 void addedge(int a,int b)    //在蘋果樹上加分支58 {    59     Node* p = new Node;60     p->num = b;61     p->next = tree[a].next;62     tree[a].next = p;63 }64 65 int main()66 {    67     int i,q;68     scanf("%d",&N);69     for(i=1;i<N;i++){70         int a,b;71         scanf("%d%d",&a,&b);72         addedge(a,b);73         addedge(b,a);74     }75     dfs(1);76 77     for(i=1;i<=N;i++)    //初始化c[]78         add(i,1);79 80     scanf("%d",&q);81     while(q--){    //q次操作82         char cmd[10];83         int d;84         scanf("%s%d",cmd,&d);85         if(cmd[0]==‘C‘){86             if(sum(start[d])-sum(start[d]-1)==1)87                 add(start[d],-1);88             else89                 add(start[d],1);90         }91         else if(cmd[0]==‘Q‘){92             printf("%d\n",sum(end[d])-sum(start[d]-1));93         }94     }95     return 0;96 }

 

Freecode : www.cnblogs.com/yym2013

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.