zoukankan      html  css  js  c++  java
  • codevs1228 苹果树

    题目描述 Description

    在卡卡的房子外面,有一棵苹果树。每年的春天,树上总会结出很多的苹果。卡卡非常喜欢吃苹果,所以他一直都精心的呵护这棵苹果树。我们知道树是有很多分叉点的,苹果会长在枝条的分叉点上面,且不会有两个苹果结在一起。卡卡很想知道一个分叉点所代表的子树上所结的苹果的数目,以便研究苹果树哪些枝条的结果能力比较强。

    卡卡所知道的是,每隔一些时间,某些分叉点上会结出一些苹果,但是卡卡所不知道的是,总会有一些调皮的小孩来树上摘走一些苹果。

    于是我们定义两种操作:

    C x

    表示编号为x的分叉点的状态被改变(原来有苹果的话,就被摘掉,原来没有的话,就结出一个苹果)

    G x

    查询编号为x的分叉点所代表的子树中有多少个苹果

    我们假定一开始的时候,树上全都是苹果,也包括作为根结点的分叉1。

    输入描述 Input Description

    第一行一个数N (n<=100000)

    接下来n-1行,每行2个数u,v,表示分叉点u和分叉点v是直接相连的。

    再接下来一行一个数M,(M<=100000)表示询问数

    接下来M行,表示询问,询问的格式如题目所述Q x或者C x

    输出描述 Output Description

    对于每个Q x的询问,请输出相应的结果,每行输出一个

    样例输入 Sample Input

    3

    1 2

    1 3

    3

    Q 1

    C 2

    Q 1

    样例输出 Sample Output

    3

    2

    /*
    通过dfs序把树映射成区间
    */
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn = 100500;
    struct edge{
        int v;
        int nxt;
    }e[maxn*2];
    int n,u,v,m,t,hh,num;
    int cnt,head[maxn];
    int dfn[maxn],low[maxn];
    int c[maxn],a[maxn];
    bool vis[maxn];
    char cmd;
    inline void ins(int u,int v){
        cnt++;
        e[cnt].v = v;
        e[cnt].nxt = head[u];
        head[u] = cnt;
    }
    inline int read(){
        char ch=getchar();
        int f=1,x=0;
        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;
    }
    void dfs(int x){
        dfn[x] = low[x] = ++t;
        vis[x] = true;
        for(int i = head[x];i;i = e[i].nxt){        
            if(!vis[e[i].v]){
                dfs(e[i].v);
                low[x] = max(low[x],low[e[i].v]);
            }
        }
    }
    inline int lowbit(int x){
        return x&-x;
    }
    inline void change(int x){
        int v;
        if(a[x]){
            v = -1;
            a[x] = 0;
        }
        else{
            v = 1;
            a[x] = 1;
        }
        for(int i = x;i <= n;i+=lowbit(i)) c[i] += v;
        
    }
    inline int query(int x){
        int ans = 0;
        for(int i = x;i;i-=lowbit(i)) ans += c[i];
        return ans;
    }
    int main(){
        n = read();
        for(int i = 1;i < n;i++){
            u = read();
            v = read();
            ins(u,v);
            ins(v,u);
            change(i);
        }
        change(n);
        dfs(1);
        m = read();
        for(int i = 1;i <= m;i++){
            cmd = getchar();
            while(cmd != 'C'&&cmd != 'Q') cmd = getchar();
            num = read();
            if(cmd == 'Q'){
                v = low[num];
                u = dfn[num];
                hh = query(v) - query(u-1);
                printf("%d
    ",hh);
            }else{
                change(dfn[num]);
            }
        }
        return 0;
    }
  • 相关阅读:
    Vue插件配置和 后台交互
    Vue项目环境搭建
    数据结构之链表
    数据结构之线性表顺序结构
    leetcode-- Longest Common Prefix
    数据结构之拓扑排序
    数据结构之shell排序
    数据结构之插入排序
    leetcode
    leetcode
  • 原文地址:https://www.cnblogs.com/hyfer/p/5901379.html
Copyright © 2011-2022 走看看