POJ

浪尽此生 提交于 2020-08-07 06:24:23

Apple Tree

  POJ - 3321


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 toN 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 forkx, 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

Source
POJ - 3321

My Solution
题意:初始时树上每个节点都有1个苹果,然后对一个节点操作,如果有苹果,就拿走,没苹果,就放上,然后询问以x为根的子树上有多少个苹果。
dfs序+线段树 简单题
POJ这题好像没有开O2,vector<int> sons[MAXN];一直TLE,换了邻接表就过了。
时间复杂度 O(n)
空间复杂度 O(4*n)
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 8;

struct Edge{
    int v,next;
}edge[2*MAXN];
int head[MAXN];
int tot;
//dfs序
int p1[MAXN], p2[MAXN], ti = 0;
//int dfsnum[MAXN];  //这个按情况是否需要。
void init(){
    memset(head, -1, sizeof head);
    tot = 0; ti = 0;
}

void add_edge(int a,int b){
    edge[tot] = (Edge){b, head[a]};
    head[a] = tot++;
}

void dfs(int u, int fa){
    p1[u] = ++ti;
    int i, v;
    for(i = head[u]; i != -1; i = edge[i].next){
        v = edge[i].v;
        if(v == fa) continue;
        dfs(v, u);
    }
    p2[u] = ti;
}
//线段树
int sum[4*MAXN];
int size;
inline void pushup(int Ind){
    sum[Ind] = sum[Ind<<1] + sum[(Ind<<1) + 1];
}

inline int _Query(int a, int b, int l, int r, int Ind){
    if(a <= l && b >= r) return sum[Ind];
    int mid = (l+r)>>1; int ret = 0;
    if(a <= mid) ret += _Query(a, b, l, mid, Ind<<1);
    if(b > mid) ret += _Query(a, b, mid + 1, r, (Ind<<1) + 1);
    return ret;
}

inline void _Modify(int a, int l, int r, int Ind){
    if(l == r && l == a){
        sum[Ind] ^= 1;
        return;
    }
    int mid = (l+r)>>1;
    if(a <= mid) _Modify(a, l, mid, Ind<<1);
    else _Modify(a, mid + 1, r, (Ind<<1) + 1);
    pushup(Ind);
}

inline void _build(int l, int r, int Ind){
    if(l == r){
        sum[Ind] = 1;
        return;
    }
    int mid = (l+r)>>1;
    _build(l, mid, Ind<<1);
    _build(mid + 1, r, (Ind<<1) + 1);
    pushup(Ind);
}

inline int Query(int a, int b) {return _Query(a, b, 1, size, 1);}
inline void Modify(int a){return _Modify(a, 1, size, 1);}


int main()
{
    #ifdef LOCAL
    freopen("b.txt", "r", stdin);
    //freopen("b.out", "w", stdout);
    int T = 1;
    while(T--){
    #endif // LOCAL
    //ios::sync_with_stdio(false); cin.tie(0);

    int n, m, i, u, v, x, root;
    char ch[6];
    scanf("%d", &n);
    init();
    for(i = 1; i < n; i++){
        scanf("%d%d", &u, &v);
        add_edge(u, v);
        add_edge(v, u);
    }
    dfs(1, -1);

    scanf("%d", &m);

    size = ti;
    _build(1, size, 1);
    while(m--){
        scanf("%s%d", ch, &x);
        if(ch[0] == 'C'){
            Modify(p1[x]);
        }
        else{
            printf("%d\n", Query(p1[x], p2[x]));
        }
    }


    #ifdef LOCAL
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}



                                                                                                                                             ------from ProLights

  Thank you!







标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!